-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
156 lines (133 loc) · 6.06 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# frozen_string_literal: true
# name: discourse-subscription-server
# about: Use Discourse as a subscription server
# version: 0.3.3
# url: https://github.com/paviliondev/discourse-subscription-server
# authors: Angus McLeod
# contact_emails: development@pavilion.tech
gem "aws-sdk-iam", "1.96.0"
enabled_site_setting :subscription_server_enabled
DiscourseEvent.on(:after_plugin_activation) do
server_plugin, plugins = Discourse.plugins.partition do |p|
p&.name == 'discourse-subscription-server'
end
Discourse.instance_variable_set(:@plugins, plugins + server_plugin)
end
after_initialize do
%w[
../lib/subscription_server/engine.rb
../lib/subscription_server/subscription.rb
../lib/subscription_server/provider.rb
../lib/subscription_server/message.rb
../lib/subscription_server/providers/stripe.rb
../lib/subscription_server/user_subscriptions.rb
../lib/subscription_server/extensions/user_api_key_client.rb
../lib/subscription_server/extensions/user_api_keys_controller.rb
../lib/subscription_server/aws.rb
../config/routes.rb
../app/models/subscription_server/user_resource.rb
../app/controllers/subscription_server/user_subscriptions_controller.rb
../app/controllers/subscription_server/user_authorizations_controller.rb
../app/controllers/subscription_server/messages_controller.rb
../app/controllers/subscription_server/server_controller.rb
../app/serializers/subscription_server/message_serializer.rb
../app/serializers/subscription_server/user_subscription_serializer.rb
../app/serializers/subscription_server/user_resource_serializer.rb
../app/jobs/scheduled/subscription_server/expire_stale_keys.rb
].each do |path|
load File.expand_path(path, __FILE__)
end
UserApiKeyClient.singleton_class.prepend SubscriptionServer::Extensions::UserApiKeyClient
UserApiKeysController.prepend SubscriptionServer::Extensions::UserApiKeysController
add_user_api_key_scope(:user_subscription,
methods: :get,
actions: "subscription_server/user_subscriptions#index",
params: :client_name
)
add_to_class(:user, :subscription_product_domain_key) do |resource_name, provider_name, product_id|
"#{SubscriptionServer::UserSubscriptions::DOMAINS_KEY_PREFIX}:#{resource_name}:#{provider_name}:#{product_id}"
end
add_to_class(:user, :add_subscription_product_domain) do |domain, resource_name, provider_name, product_id|
key = subscription_product_domain_key(resource_name, provider_name, product_id)
value_str = (custom_fields[key] || "")
value_arr = value_str.split('|')
value_arr << domain unless value_arr.include?(domain)
custom_fields[key] = value_arr.join('|')
save_custom_fields(true)
end
add_to_class(:user, :remove_subscription_product_domain) do |domain, resource_name, provider_name, product_id|
key = subscription_product_domain_key(resource_name, provider_name, product_id)
value_str = (custom_fields[key] || "")
value_arr = value_str.split('|')
value_arr = value_arr.reject { |d| d === domain }
custom_fields[key] = value_arr.join('|')
save_custom_fields(true)
end
add_to_class(:user, :remove_subscription_domain) do |domain|
self._custom_fields.where(
"name LIKE '#{SubscriptionServer::UserSubscriptions::DOMAINS_KEY_PREFIX}%'"
).each do |field|
value_arr = field.value.split('|')
value_arr = value_arr.reject { |d| d === domain }
field.value = value_arr.join('|')
field.save!
end
end
add_to_class(:user, :subscription_product_domains) do |resource_name, provider_name, product_id|
key = subscription_product_domain_key(resource_name, provider_name, product_id)
product_domains = custom_fields[key]
(product_domains || "").split('|').uniq
end
add_to_class(:user, :subscription_domains) do
subscriptions_map = SubscriptionServer::Subscription.subscription_map
subscription_domains = {}
custom_fields
.select { |key, _| key.include?(SubscriptionServer::UserSubscriptions::DOMAINS_KEY_PREFIX) }
.each do |key, value|
key_parts = key.split(':')
subscription_domains[key_parts[1]] ||= { products: [], domains: [] }
subscription_domains[key_parts[1]][:products] << key_parts[3]
subscription_domains[key_parts[1]][:domains].concat value.split('|')
end
subscription_domains.each do |resource, data|
data[:domain_limit] = subscriptions_map[resource][:domain_limits]
.select { |limit| data[:products].include?(limit[:product_id]) }
.sum { |limit| limit[:domain_limit] }
end
subscription_domains.reduce([]) do |result, (resource, data)|
data[:resource] = resource
result << data
result
end
end
add_to_serializer(:user, :subscription_domains) { user.subscription_domains }
if SubscriptionServer::Stripe.discourse_subscriptions_installed?
%w[
../extensions/discourse_subscriptions_coupons_controller_extension.rb
../extensions/discourse_subscriptions_products_controller_extension.rb
../extensions/discourse_subscriptions_subscriber_controller_extension.rb
].each do |path|
load File.expand_path(path, __FILE__)
end
DiscourseSubscriptions::SubscribeController.prepend DiscourseSubscriptionsSubscribeControllerExtension
DiscourseSubscriptions::Admin::CouponsController.prepend DiscourseSubscriptionsCouponsControllerExtension
DiscourseSubscriptions::Admin::ProductsController.prepend DiscourseSubscriptionsProductsControllerExtension
require 'csv'
module ::DiscourseSubscriptions
def self.class_update_subscriptions_from_csv(path)
rows = CSV.read(path)
return unless rows.present?
rows.each do |row|
DiscourseSubscriptions::Subscription
.joins(:customer)
.where("discourse_subscriptions_customers.customer_id = :customer_id AND discourse_subscriptions_subscriptions.external_id = :subscription_id",
customer_id: row[0].strip,
subscription_id: row[1].strip
).update_all(
external_id: row[2].strip
)
end
end
end
end
end