-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathplugin.rb
449 lines (382 loc) · 16.6 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# frozen_string_literal: true
# name: discourse-reactions
# about: Allows users to react to a post with emojis.
# meta_topic_id: 183261
# version: 0.5
# author: Ahmed Gagan, Rafael dos Santos Silva, Kris Aubuchon, Joffrey Jaffeux, Kris Kotlarek, Jordan Vidrine
# url: https://github.com/discourse/discourse-reactions
enabled_site_setting :discourse_reactions_enabled
register_asset "stylesheets/common/discourse-reactions.scss"
register_asset "stylesheets/desktop/discourse-reactions.scss", :desktop
register_asset "stylesheets/mobile/discourse-reactions.scss", :mobile
register_svg_icon "star"
register_svg_icon "far-star"
require_relative "lib/reaction_for_like_site_setting_enum.rb"
require_relative "lib/reactions_excluded_from_like_site_setting_validator.rb"
after_initialize do
SeedFu.fixture_paths << Rails.root.join("plugins", "discourse-reactions", "db", "fixtures").to_s
module ::DiscourseReactions
PLUGIN_NAME ||= "discourse-reactions"
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscourseReactions
end
end
%w[
app/controllers/discourse_reactions/custom_reactions_controller.rb
app/models/discourse_reactions/reaction_user.rb
app/models/discourse_reactions/reaction.rb
app/serializers/reaction_serializer.rb
app/serializers/user_reaction_serializer.rb
app/services/discourse_reactions/reaction_manager.rb
app/services/discourse_reactions/reaction_notification.rb
app/services/discourse_reactions/reaction_like_synchronizer.rb
lib/discourse_reactions/guardian_extension.rb
lib/discourse_reactions/notification_extension.rb
lib/discourse_reactions/post_alerter_extension.rb
lib/discourse_reactions/post_extension.rb
lib/discourse_reactions/post_action_extension.rb
lib/discourse_reactions/topic_view_serializer_extension.rb
lib/discourse_reactions/migration_report.rb
app/jobs/regular/discourse_reactions/like_synchronizer.rb
app/jobs/scheduled/discourse_reactions/scheduled_like_synchronizer.rb
].each { |path| require_relative path }
reloadable_patch do |plugin|
Post.prepend DiscourseReactions::PostExtension
PostAction.prepend DiscourseReactions::PostActionExtension
TopicViewSerializer.prepend DiscourseReactions::TopicViewSerializerExtension
PostAlerter.prepend DiscourseReactions::PostAlerterExtension
Guardian.prepend DiscourseReactions::GuardianExtension
Notification.singleton_class.prepend DiscourseReactions::NotificationExtension
end
Discourse::Application.routes.append { mount ::DiscourseReactions::Engine, at: "/" }
DiscourseReactions::Engine.routes.draw do
get "/discourse-reactions/custom-reactions" => "custom_reactions#index",
:constraints => {
format: :json,
}
put "/discourse-reactions/posts/:post_id/custom-reactions/:reaction/toggle" =>
"custom_reactions#toggle",
:constraints => {
format: :json,
}
get "/discourse-reactions/posts/reactions" => "custom_reactions#reactions_given",
:as => "reactions_given"
get "/discourse-reactions/posts/reactions-received" => "custom_reactions#reactions_received",
:as => "reactions_received"
get "/discourse-reactions/posts/:id/reactions-users" => "custom_reactions#post_reactions_users",
:as => "post_reactions_users"
end
add_to_serializer(:post, :reactions) do
reactions = []
reaction_users_counting_as_like = Set.new
object
.emoji_reactions
.select { |reaction| reaction[:reaction_users_count] }
.each do |reaction|
reactions << {
id: reaction.reaction_value,
type: reaction.reaction_type.to_sym,
count: reaction.reaction_users_count,
}
# NOTE: It does not matter if the reaction is currently an enabled one,
# we need to handle historical data here too so we don't see double-ups in the UI.
if !DiscourseReactions::Reaction.reactions_excluded_from_like.include?(
reaction.reaction_value,
) && reaction.reaction_value != DiscourseReactions::Reaction.main_reaction_id
reaction_users_counting_as_like.merge(reaction.reaction_users.pluck(:user_id))
end
end
likes =
object.post_actions.reject do |post_action|
# Get rid of any PostAction records that match up to a ReactionUser
# that is NOT main_reaction_id and is NOT excluded, otherwise we double
# up on the count/reaction shown in the UI.
next true if reaction_users_counting_as_like.include?(post_action.user_id)
# Also get rid of any PostAction records that match up to a ReactionUser
# that is now the main_reaction_id and has historical data.
object
.post_actions_with_reaction_users
&.dig(post_action.id)
&.reaction_user
&.reaction
&.reaction_value == DiscourseReactions::Reaction.main_reaction_id
end
# Likes will only be blank if there are only reactions where the reaction is in
# discourse_reactions_excluded_from_like. All other reactions will have a `PostAction` record.
return reactions.sort_by { |reaction| [-reaction[:count].to_i, reaction[:id]] } if likes.blank?
# Reactions using main_reaction_id only have a `PostAction` record,
# not any `ReactionUser` records, as long as the main_reaction_id was never
# changed -- if it was then we could have a ReactionUser as well.
reaction_likes, reactions =
reactions.partition { |r| r[:id] == DiscourseReactions::Reaction.main_reaction_id }
reactions << {
id: DiscourseReactions::Reaction.main_reaction_id,
type: :emoji,
count: likes.size + reaction_likes.sum { |r| r[:count] },
}
reactions.sort_by { |reaction| [-reaction[:count].to_i, reaction[:id]] }
end
add_to_serializer(:post, :current_user_reaction) do
return nil if scope.is_anonymous?
object.emoji_reactions.each do |reaction|
reaction_user = reaction.reaction_users.find { |ru| ru.user_id == scope.user.id }
next if reaction_user.blank?
if reaction.reaction_users_count
return(
{
id: reaction.reaction_value,
type: reaction.reaction_type.to_sym,
can_undo: reaction_user.can_undo?,
}
)
end
end
# Any PostAction Like that doesn't have a matching ReactionUser record
# will count as the main_reaction_id.
like =
object.post_actions.find do |post_action|
post_action.post_action_type_id == PostActionType::LIKE_POST_ACTION_ID &&
!post_action.trashed? && post_action.user_id == scope.user.id
end
return nil if like.blank?
{
id: DiscourseReactions::Reaction.main_reaction_id,
type: :emoji,
can_undo: scope.can_delete_post_action?(like),
}
end
add_to_serializer(:post, :reaction_users_count) do
return object.reaction_users_count unless object.reaction_users_count.nil?
TopicViewSerializer.posts_reaction_users_count(object.id)[object.id]
end
add_to_serializer(:post, :current_user_used_main_reaction) do
return false if scope.is_anonymous?
like_post_action =
object.post_actions.find do |post_action|
post_action.post_action_type_id == PostActionType::LIKE_POST_ACTION_ID &&
post_action.user_id == scope.user.id && !post_action.trashed?
end
has_matching_reaction_user =
object.emoji_reactions.any? do |reaction|
DiscourseReactions::Reaction.reactions_counting_as_like.include?(reaction.reaction_value) &&
reaction.reaction_users.find { |ru| ru.user_id == scope.user.id }.present?
end
like_post_action.present? && !has_matching_reaction_user
end
add_to_serializer(:topic_view, :valid_reactions) { DiscourseReactions::Reaction.valid_reactions }
add_model_callback(User, :before_destroy) do
DiscourseReactions::ReactionUser.where(user_id: self.id).delete_all
end
add_report("reactions") do |report|
main_id = DiscourseReactions::Reaction.main_reaction_id
report.icon = "discourse-emojis"
report.modes = [:table]
report.data = []
report.labels = [
{ type: :date, property: :day, title: I18n.t("reports.reactions.labels.day") },
{
type: :number,
property: :like_count,
html_title: PrettyText.unescape_emoji(CGI.escapeHTML(":#{main_id}:")),
},
]
reactions = SiteSetting.discourse_reactions_enabled_reactions.split("|") - [main_id]
reactions.each do |reaction|
report.labels << {
type: :number,
property: "#{reaction}_count",
html_title: PrettyText.unescape_emoji(CGI.escapeHTML(":#{reaction}:")),
}
end
reactions_results =
DB.query(<<~SQL, start_date: report.start_date.to_date, end_date: report.end_date.to_date)
SELECT
reactions.reaction_value,
count(reaction_users.id) as reactions_count,
date_trunc('day', reaction_users.created_at)::date as day
FROM discourse_reactions_reactions as reactions
LEFT OUTER JOIN discourse_reactions_reaction_users as reaction_users on reactions.id = reaction_users.reaction_id
WHERE reactions.reaction_users_count IS NOT NULL
AND reaction_users.created_at::DATE >= :start_date::DATE AND reaction_users.created_at::DATE <= :end_date::DATE
GROUP BY reactions.reaction_value, day
SQL
likes_results =
DB.query(
<<~SQL,
SELECT
count(post_actions.id) as likes_count,
date_trunc('day', post_actions.created_at)::date as day
FROM post_actions as post_actions
WHERE post_actions.created_at::DATE >= :start_date::DATE AND post_actions.created_at::DATE <= :end_date::DATE
AND #{DiscourseReactions::PostActionExtension.filter_reaction_likes_sql}
GROUP BY day
SQL
start_date: report.start_date.to_date,
end_date: report.end_date.to_date,
like: PostActionType::LIKE_POST_ACTION_ID,
valid_reactions: DiscourseReactions::Reaction.valid_reactions.to_a,
)
(report.start_date.to_date..report.end_date.to_date).each do |date|
data = { day: date }
like_count = 0
like_reaction_count = 0
likes_results.select { |r| r.day == date }.each { |result| like_count += result.likes_count }
reactions_results
.select { |r| r.day == date }
.each do |result|
if result.reaction_value == main_id
like_reaction_count += result.reactions_count
else
data["#{result.reaction_value}_count"] ||= 0
data["#{result.reaction_value}_count"] += result.reactions_count
end
end
data[:like_count] = like_reaction_count + like_count
report.data << data
end
end
field_key = "display_username"
consolidated_reactions =
Notifications::ConsolidateNotifications
.new(
from: Notification.types[:reaction],
to: Notification.types[:reaction],
threshold: -> { SiteSetting.notification_consolidation_threshold },
consolidation_window: SiteSetting.likes_notification_consolidation_window_mins.minutes,
unconsolidated_query_blk:
Proc.new do |notifications, data|
notifications.where(
"data::json ->> 'username2' IS NULL AND data::json ->> 'consolidated' IS NULL",
).where("data::json ->> '#{field_key}' = ?", data[field_key.to_sym].to_s)
end,
consolidated_query_blk:
Proc.new do |notifications, data|
notifications.where("(data::json ->> 'consolidated')::bool").where(
"data::json ->> '#{field_key}' = ?",
data[field_key.to_sym].to_s,
)
end,
)
.set_mutations(
set_data_blk:
Proc.new do |notification|
data = notification.data_hash
data.merge(username: data[:display_username], consolidated: true)
end,
)
.set_precondition(precondition_blk: Proc.new { |data| data[:username2].blank? })
consolidated_reactions.before_consolidation_callbacks(
before_consolidation_blk:
Proc.new do |notifications, data|
new_icon = data[:reaction_icon]
if new_icon
icons = notifications.pluck("data::json ->> 'reaction_icon'")
data.delete(:reaction_icon) if icons.any? { |i| i != new_icon }
end
end,
before_update_blk:
Proc.new do |consolidated, updated_data, notification|
if consolidated.data_hash[:reaction_icon] != notification.data_hash[:reaction_icon]
updated_data.delete(:reaction_icon)
end
end,
)
reacted_by_two_users =
Notifications::DeletePreviousNotifications
.new(
type: Notification.types[:reaction],
previous_query_blk:
Proc.new do |notifications, data|
notifications.where(id: data[:previous_notification_id])
end,
)
.set_mutations(
set_data_blk:
Proc.new do |notification|
existing_notification_of_same_type =
Notification
.where(user: notification.user)
.order("notifications.id DESC")
.where(topic_id: notification.topic_id, post_number: notification.post_number)
.where(notification_type: notification.notification_type)
.where("created_at > ?", 1.day.ago)
.first
data = notification.data_hash
if existing_notification_of_same_type
same_type_data = existing_notification_of_same_type.data_hash
new_data =
data.merge(
previous_notification_id: existing_notification_of_same_type.id,
username2: same_type_data[:display_username],
count: (same_type_data[:count] || 1).to_i + 1,
)
new_data
else
data
end
end,
)
.set_precondition(
precondition_blk:
Proc.new do |data, notification|
always_freq = UserOption.like_notification_frequency_type[:always]
notification.user&.user_option&.like_notification_frequency == always_freq &&
data[:previous_notification_id].present?
end,
)
register_notification_consolidation_plan(reacted_by_two_users)
register_notification_consolidation_plan(consolidated_reactions)
# Filter out Likes that are also Reactions, for the user likes-received page.
register_modifier(:user_action_stream_builder) do |builder|
builder.left_join(<<~SQL)
discourse_reactions_reaction_users ON discourse_reactions_reaction_users.post_id = a.target_post_id
AND discourse_reactions_reaction_users.user_id = a.acting_user_id
SQL
builder.where("discourse_reactions_reaction_users.id IS NULL")
end
# Filter out the users who Liked as well as Reacted to the post, for the
# user avatars that show beneath the post when you click the "show more actions"
# [...] button.
register_modifier(:post_action_users_list) do |query, post|
where_clause = <<~SQL
post_actions.id NOT IN (
SELECT post_actions.id
FROM post_actions
INNER JOIN discourse_reactions_reaction_users ON discourse_reactions_reaction_users.post_id = post_actions.post_id
AND discourse_reactions_reaction_users.user_id = post_actions.user_id
WHERE post_actions.post_id = #{post.id}
)
SQL
query.where(where_clause)
end
on(:first_post_moved) do |target_post, original_post|
id_map = {}
ActiveRecord::Base.transaction do
reactions = DiscourseReactions::Reaction.where(post_id: original_post.id)
next if !reactions.any?
reactions_attributes =
reactions.map { |reaction| reaction.attributes.except("id").merge(post_id: target_post.id) }
DiscourseReactions::Reaction
.insert_all(reactions_attributes)
.each_with_index { |entry, index| id_map[reactions[index].id] = entry["id"] }
reaction_users = DiscourseReactions::ReactionUser.where(post_id: original_post.id)
next if !reaction_users.any?
reaction_users_attributes =
reaction_users.map do |reaction_user|
reaction_user
.attributes
.except("id")
.merge(post_id: target_post.id, reaction_id: id_map[reaction_user.reaction_id])
end
DiscourseReactions::ReactionUser.insert_all(reaction_users_attributes)
end
end
on(:site_setting_changed) do |name, old_value, new_value|
if name == :discourse_reactions_excluded_from_like &&
SiteSetting.discourse_reactions_like_sync_enabled
::Jobs.cancel_scheduled_job(Jobs::DiscourseReactions::LikeSynchronizer)
::Jobs.enqueue_at(5.minutes.from_now, Jobs::DiscourseReactions::LikeSynchronizer)
end
end
end