-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.rb
executable file
·190 lines (163 loc) · 5.03 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# name: Discourse Premium BT
# about: Adds a membership section to Discourse
# version: 0.1
# author: Joe Buhlig joebuhlig.com
# url: https://www.github.com/joebuhlig/discourse-premium-bt
register_asset "stylesheets/premium_bt.scss"
register_asset "javascripts/premium_bt.js"
register_asset "javascripts/braintree.js"
enabled_site_setting :premium_bt_enabled
gem 'braintree', '2.48.1'
# load the engine
load File.expand_path('../lib/discourse_premium_bt.rb', __FILE__)
load File.expand_path('../lib/discourse_premium_bt/engine.rb', __FILE__)
after_initialize do
load File.expand_path("../app/jobs/expiration_warning.rb", __FILE__)
load File.expand_path("../app/jobs/free_month_expiration.rb", __FILE__)
load File.expand_path("../app/jobs/validate_subscriptions.rb", __FILE__)
require_dependency "application_controller"
class ::ApplicationController
before_filter :ref_to_cookie
def ref_to_cookie
if SiteSetting.premium_bt_affiliate and params[:ref] and !User.find_by_id(params[:ref]).nil?
cookies[:discourse_prem_aff] = { :value => params[:ref], :expires => 30.days.from_now }
end
end
end
require_dependency 'user'
class ::User
def grant_free_month
# If affiliate program is in use
if SiteSetting.premium_bt_affiliate
# If they have a current subscription
if !self.custom_fields['subscription_id'].nil?
DiscoursePremiumBt.validate
subscription = Braintree::Subscription.find(self.custom_fields["subscription_id"])
discounts = subscription.discounts
number_of_billing_cycles = 1
if discounts.empty?
subscription = Braintree::Subscription.update(
self.custom_fields["subscription_id"],
:discounts => {
:add => [
{
:inherited_from_id => SiteSetting.premium_bt_plan_discount
}
]
}
)
else
discounts.each do |discount|
if discount.id == SiteSetting.premium_bt_plan_discount
number_of_billing_cycles = discount.number_of_billing_cycles + 1
end
end
subscription = Braintree::Subscription.update(
self.custom_fields["subscription_id"],
:discounts => {
:update => [
{
:existing_id => SiteSetting.premium_bt_plan_discount,
:number_of_billing_cycles => number_of_billing_cycles
}
]
}
)
end
if subscription.success?
title = I18n.t("premium_bt_messages.pm_free_month_title")
raw = I18n.t("premium_bt_messages.pm_free_month_text")
send_premium_pm(title, raw)
end
# If no current subscription exists
else
if !(premium_for_life)
if self.custom_fields["premium_exp_date"].nil?
expiration = Time.now + 1.month
else
expiration = self.custom_fields["premium_exp_date"] + 1.month
end
premium_group_grant
self.custom_fields["premium_exp_date"] = expiration
self.custom_fields["premium_exp_pm_sent"] = false
self.save
title = I18n.t("premium_bt_messages.pm_free_month_title")
raw = I18n.t("premium_bt_messages.pm_free_month_text")
send_premium_pm(title, raw)
end
end
end
end
def premium_group_grant
group = Group.find_by_name(SiteSetting.premium_bt_group_name)
if !group.users.include?(self)
group.add(self)
end
group.save
end
def premium_group_revoke
group = Group.find_by_name(SiteSetting.premium_bt_group_name)
self.primary_group_id = nil if self.primary_group_id == group.id
group.users.delete(self.id)
group.save
self.save
end
def premium_group_check
groups = self.groups
groups.each do |group|
group = Group.where(id: group.id).first
if group.name == SiteSetting.premium_bt_group_name
return true
end
end
return false
end
def premium_subscriber
if !self.custom_fields['subscription_id'].nil?
return true
else
return false
end
end
def premium_for_life
if premium_group_check and !premium_subscriber and self.custom_fields["premium_exp_date"].nil?
return true
else
return false
end
end
def send_premium_pm(title, raw)
PostCreator.create(
Discourse.system_user,
target_usernames: self.username,
archetype: Archetype.private_message,
subtype: TopicSubtype.system_message,
title: title,
raw: raw
)
end
end
require_dependency 'users_controller'
class ::UsersController
after_filter :referral_check, only: [:perform_account_activation]
def referral_check
if SiteSetting.premium_bt_enabled and SiteSetting.premium_bt_affiliate and cookies[:discourse_prem_aff] and !User.find_by_id(cookies[:discourse_prem_aff]).nil?
user = User.find_by_id(cookies[:discourse_prem_aff])
user.grant_free_month
end
end
end
require_dependency 'current_user_serializer'
class ::CurrentUserSerializer
attributes :premium, :subscriber
def premium
premium = object.premium_group_check
end
def subscriber
subscriber = object.premium_subscriber
end
end
Discourse::Application.routes.append do
mount ::DiscoursePremiumBt::Engine, at: "/premium"
end
end