-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugin.rb
executable file
·399 lines (311 loc) · 14.8 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# name: signups
# about: Create signup options for events
# version: 0.4
# authors: Noah Lackstein, based on work by Vikhyat Korrapati (vikhyat), Régis Hanol (zogstrip)
# url: https://github.com/lackstein/discourse_signups
enabled_site_setting :signup_enabled
register_asset "stylesheets/common/signup.scss"
register_asset "stylesheets/desktop/signup.scss", :desktop
register_asset "stylesheets/mobile/signup.scss", :mobile
PLUGIN_NAME ||= "discourse_signups".freeze
SIGNUPS_CUSTOM_FIELD ||= "signups".freeze
VOTES_CUSTOM_FIELD ||= "signups-votes".freeze
after_initialize do
# remove "Vote Now!" & "Show Results" links in emails
Email::Styles.register_plugin_style do |fragment|
fragment.css(".signup a.cast-votes, .signup a.toggle-results").each(&:remove)
end
module ::DiscourseSignups
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscourseSignups
end
end
class DiscourseSignups::Signup
class << self
def vote(post_id, signup_name, options, user_id, logger)
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post_id}") do
post = Post.find_by(id: post_id)
user = User.find_by(id: user_id)
# post must not be deleted
if post.nil? || post.trashed?
raise StandardError.new I18n.t("signup.post_is_deleted")
end
# topic must be open
if post.topic.try(:closed) || post.topic.try(:archived)
raise StandardError.new I18n.t("signup.topic_must_be_open_to_vote")
end
signups = post.custom_fields[SIGNUPS_CUSTOM_FIELD]
raise StandardError.new I18n.t("signup.no_signups_associated_with_this_post") if signups.blank?
signup = signups[signup_name]
raise StandardError.new I18n.t("signup.no_signup_with_this_name", name: signup_name) if signup.blank?
raise StandardError.new I18n.t("signup.signup_must_be_open_to_vote") if signup["status"] != "open"
# remove options that aren't available in the signup
available_options = signup["options"].map { |o| o["id"] }.to_set
options.select! { |o| available_options.include?(o) }
#raise StandardError.new I18n.t("signup.requires_at_least_1_valid_option") if options.empty?
votes = post.custom_fields["#{VOTES_CUSTOM_FIELD}-#{user_id}"] || {}
vote = votes[signup_name] || []
# increment counters only when the user hasn't casted a vote yet
signup["voters"] += 1 if vote.size == 0 && !options.empty?
# Decrement when cancelling a vote
signup["voters"] -= 1 if vote.size != 0 && options.empty?
votes[signup_name] = options
post.custom_fields["#{VOTES_CUSTOM_FIELD}-#{user_id}"] = votes
all_votes = post.custom_fields.select { |field| field =~ /^#{VOTES_CUSTOM_FIELD}-\d+/ }
signup_votes = all_votes.map { |voter, signup_sheets| { user: User.find(voter.split("-").last), votes: signup_sheets[signup_name] } }
signup["options"].each do |option|
# Decrement counter if user had previously chosen this option
option["votes"] -= 1 if vote.include?(option["id"])
# (Re)increment counter if user (still) chose this option
option["votes"] += 1 if options.include?(option["id"])
# Rebuild list of users that have voted for this option
option["voters"] = signup_votes
.select { |ballot| ballot[:votes].include? option["id"] }
.map { |ballot| ballot[:user].username } rescue []
end
# Remove vote if empty
votes.delete_if { |key, value| value.empty? }
post.custom_fields.delete("#{VOTES_CUSTOM_FIELD}-#{user_id}") if !post.custom_fields["#{VOTES_CUSTOM_FIELD}-#{user_id}"].nil? && votes.empty?
post.custom_fields[SIGNUPS_CUSTOM_FIELD] = signups
post.save_custom_fields(true)
# Automatically subscribe the voter to notifications about the event
TopicUser.change(user, post.topic.id, notification_level: TopicUser.notification_levels[:watching])
MessageBus.publish("/signups/#{post.topic_id}", { post_id: post_id, signups: signups })
return [signup, options]
end
end
def toggle_status(post_id, signup_name, status, user_id)
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post_id}") do
post = Post.find_by(id: post_id)
# post must not be deleted
if post.nil? || post.trashed?
raise StandardError.new I18n.t("signup.post_is_deleted")
end
# topic must be open
if post.topic.try(:closed) || post.topic.try(:archived)
raise StandardError.new I18n.t("signup.topic_must_be_open_to_toggle_status")
end
user = User.find_by(id: user_id)
# either staff member or OP
unless user_id == post.user_id || user.try(:staff?)
raise StandardError.new I18n.t("signup.only_staff_or_op_can_toggle_status")
end
signups = post.custom_fields[SIGNUPS_CUSTOM_FIELD]
raise StandardError.new I18n.t("signup.no_signups_associated_with_this_post") if signups.blank?
raise StandardError.new I18n.t("signup.no_signup_with_this_name", name: signup_name) if signups[signup_name].blank?
signups[signup_name]["status"] = status
post.save_custom_fields(true)
MessageBus.publish("/signups/#{post.topic_id}", { post_id: post_id, signups: signups })
signups[signup_name]
end
end
def extract(raw, topic_id)
# TODO: we should fix the callback mess so that the cooked version is available
# in the validators instead of cooking twice
cooked = PrettyText.cook(raw, topic_id: topic_id)
parsed = Nokogiri::HTML(cooked)
extracted_signups = []
# extract signups
parsed.css("div.signup").each do |p|
signup = { "options" => [], "voters" => 0 }
# extract attributes
p.attributes.values.each do |attribute|
if attribute.name.start_with?(DATA_PREFIX)
signup[attribute.name[DATA_PREFIX.length..-1]] = attribute.value
end
end
# extract options
p.css("li[#{DATA_PREFIX}option-id]").each do |o|
option_id = o.attributes[DATA_PREFIX + "option-id"].value
signup["options"] << { "id" => option_id, "html" => o.inner_html, "votes" => 0, "voters" => [] }
end
# add the signup
extracted_signups << signup
end
extracted_signups
end
end
end
require_dependency "application_controller"
class DiscourseSignups::SignupsController < ::ApplicationController
requires_plugin PLUGIN_NAME
before_filter :ensure_logged_in
def vote
post_id = params.require(:post_id)
signup_name = params.require(:signup_name)
options = params.permit(options: [])
options = options.empty? ? [] : options["options"]
user_id = current_user.id
begin
signup, options = DiscourseSignups::Signup.vote(post_id, signup_name, options, user_id, logger)
render json: { signup: signup, vote: options }
rescue StandardError => e
logger.error "SIGNUP ERROR: #{e.message}"
render_json_error e.message
end
end
def toggle_status
post_id = params.require(:post_id)
signup_name = params.require(:signup_name)
status = params.require(:status)
user_id = current_user.id
begin
signup = DiscourseSignups::Signup.toggle_status(post_id, signup_name, status, user_id)
render json: { signup: signup }
rescue StandardError => e
render_json_error e.message
end
end
def voters
usernames = params.require(:usernames)
users = User.where(username: usernames).map do |user|
UserNameSerializer.new(user).serializable_hash
end
render json: { users: users }
end
end
DiscourseSignups::Engine.routes.draw do
put "/vote" => "signups#vote"
put "/toggle_status" => "signups#toggle_status"
get "/voters" => 'signups#voters'
end
Discourse::Application.routes.append do
mount ::DiscourseSignups::Engine, at: "/signups"
end
Post.class_eval do
attr_accessor :signups
after_save do
next if self.signups.blank? || !self.signups.is_a?(Hash)
post = self
signups = self.signups
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post.id}") do
post.custom_fields[SIGNUPS_CUSTOM_FIELD] = signups
post.save_custom_fields(true)
end
end
end
DATA_PREFIX ||= "data-signup-".freeze
DEFAULT_SIGNUP_NAME ||= "signup".freeze
validate(:post, :validate_signups) do
# only care when raw has changed!
return unless self.raw_changed?
signups = {}
extracted_signups = DiscourseSignups::Signup::extract(self.raw, self.topic_id)
extracted_signups.each do |signup|
# signups should have a unique name
if signups.has_key?(signup["name"])
signup["name"] == DEFAULT_SIGNUP_NAME ?
self.errors.add(:base, I18n.t("signup.multiple_signups_without_name")) :
self.errors.add(:base, I18n.t("signup.multiple_signups_with_same_name", name: signup["name"]))
return
end
# options must be unique
if signup["options"].map { |o| o["id"] }.uniq.size != signup["options"].size
signup["name"] == DEFAULT_SIGNUP_NAME ?
self.errors.add(:base, I18n.t("signup.default_signup_must_have_different_options")) :
self.errors.add(:base, I18n.t("signup.named_signup_must_have_different_options", name: signup["name"]))
return
end
# maximum # of options
if signup["options"].size > SiteSetting.signup_maximum_options
signup["name"] == DEFAULT_SIGNUP_NAME ?
self.errors.add(:base, I18n.t("signup.default_signup_must_have_less_options", max: SiteSetting.signup_maximum_options)) :
self.errors.add(:base, I18n.t("signup.named_signup_must_have_less_options", name: signup["name"], max: SiteSetting.signup_maximum_options))
return
end
# signup with multiple choices
if signup["type"] == "multiple"
min = (signup["min"].presence || 1).to_i
max = (signup["max"].presence || signup["options"].size).to_i
if min > max || max <= 0 || max > signup["options"].size || min >= signup["options"].size
signup["name"] == DEFAULT_SIGNUP_NAME ?
self.errors.add(:base, I18n.t("signup.default_signup_with_multiple_choices_has_invalid_parameters")) :
self.errors.add(:base, I18n.t("signup.named_signup_with_multiple_choices_has_invalid_parameters", name: signup["name"]))
return
end
end
# store the valid signup
signups[signup["name"]] = signup
end
# are we updating a post?
if self.id.present?
post = self
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post.id}") do
# load previous signups
previous_signups = post.custom_fields[SIGNUPS_CUSTOM_FIELD] || {}
# extract options
current_options = signups.values.map { |p| p["options"].map { |o| o["id"] } }.flatten.sort
previous_options = previous_signups.values.map { |p| p["options"].map { |o| o["id"] } }.flatten.sort
# are the signups different?
if signups.keys != previous_signups.keys || current_options != previous_options
has_votes = previous_signups.keys.map { |p| previous_signups[p]["voters"].to_i }.sum > 0
# outside of the 5-minute edit window?
if post.created_at < 5.minutes.ago && has_votes
# cannot add/remove/rename signups
if signups.keys.sort != previous_signups.keys.sort
post.errors.add(:base, I18n.t("signup.cannot_change_signups_after_5_minutes"))
return
end
# deal with option changes
if User.staff.pluck(:id).include?(post.last_editor_id)
# staff can only edit options
signups.each_key do |signup_name|
if signups[signup_name]["options"].size != previous_signups[signup_name]["options"].size && previous_signups[signup_name]["voters"].to_i > 0
post.errors.add(:base, I18n.t("signup.staff_cannot_add_or_remove_options_after_5_minutes"))
return
end
end
else
# OP cannot edit signup options
post.errors.add(:base, I18n.t("signup.op_cannot_edit_options_after_5_minutes"))
return
end
end
# try to merge votes
signups.each_key do |signup_name|
next unless previous_signups.has_key?(signup_name)
# when the # of options has changed, reset all the votes
if signups[signup_name]["options"].size != previous_signups[signup_name]["options"].size
PostCustomField.where(post_id: post.id)
.where("name LIKE '#{VOTES_CUSTOM_FIELD}-%'")
.destroy_all
post.clear_custom_fields
next
end
signups[signup_name]["voters"] = previous_signups[signup_name]["voters"]
for o in 0...signups[signup_name]["options"].size
signups[signup_name]["options"][o]["votes"] = previous_signups[signup_name]["options"][o]["votes"]
end
end
# immediately store the signups
post.custom_fields[SIGNUPS_CUSTOM_FIELD] = signups
post.save_custom_fields(true)
# publish the changes
MessageBus.publish("/signups/#{post.topic_id}", { post_id: post_id, signups: signups })
end
end
else
self.signups = signups
end
true
end
Post.register_custom_field_type(SIGNUPS_CUSTOM_FIELD, :json)
Post.register_custom_field_type("#{VOTES_CUSTOM_FIELD}-*", :json)
TopicView.add_post_custom_fields_whitelister do |user|
whitelisted = [SIGNUPS_CUSTOM_FIELD]
whitelisted << "#{VOTES_CUSTOM_FIELD}-#{user.id}" if user
whitelisted
end
# tells the front-end we have a signup for that post
on(:post_created) do |post|
next if post.is_first_post? || post.custom_fields[SIGNUPS_CUSTOM_FIELD].blank?
MessageBus.publish("/signups/#{post.topic_id}", {
post_id: post.id,
signups: post.custom_fields[SIGNUPS_CUSTOM_FIELD] })
end
add_to_serializer(:post, :signups, false) { post_custom_fields[SIGNUPS_CUSTOM_FIELD] }
add_to_serializer(:post, :include_signups?) { post_custom_fields.present? && post_custom_fields[SIGNUPS_CUSTOM_FIELD].present? }
add_to_serializer(:post, :signups_votes, false) { post_custom_fields["#{VOTES_CUSTOM_FIELD}-#{scope.user.id}"] }
add_to_serializer(:post, :include_signups_votes?) { scope.user && post_custom_fields.present? && post_custom_fields["#{VOTES_CUSTOM_FIELD}-#{scope.user.id}"].present? }
end