-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathassigner.rb
416 lines (350 loc) · 13.1 KB
/
assigner.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
# frozen_string_literal: true
require 'email/sender'
require 'nokogiri'
class ::Assigner
ASSIGNMENTS_PER_TOPIC_LIMIT = 5
def self.backfill_auto_assign
staff_mention = User
.assign_allowed
.pluck('username')
.map { |name| "p.cooked ILIKE '%mention%@#{name}%'" }
.join(' OR ')
sql = <<~SQL
SELECT p.topic_id, MAX(post_number) post_number
FROM posts p
JOIN topics t ON t.id = p.topic_id
LEFT JOIN assignments a ON a.target_id = p.topic_id AND a.target_type = 'Topic'
WHERE p.user_id IN (SELECT id FROM users WHERE moderator OR admin)
AND (#{staff_mention})
AND a.assigned_to_id IS NULL
AND NOT t.closed
AND t.deleted_at IS NULL
GROUP BY p.topic_id
SQL
puts
assigned = 0
ActiveRecord::Base.connection.raw_connection.exec(sql).to_a.each do |row|
post = Post.find_by(post_number: row["post_number"].to_i, topic_id: row["topic_id"].to_i)
assigned += 1 if post && auto_assign(post)
putc "."
end
puts
puts "#{assigned} topics where automatically assigned to staff members"
end
def self.assigned_self?(text)
return false if text.blank? || SiteSetting.assign_self_regex.blank?
regex = Regexp.new(SiteSetting.assign_self_regex) rescue nil
!!(regex && text[regex])
end
def self.assigned_other?(text)
return false if text.blank? || SiteSetting.assign_other_regex.blank?
regex = Regexp.new(SiteSetting.assign_other_regex) rescue nil
!!(regex && text[regex])
end
def self.auto_assign(post, force: false)
return unless SiteSetting.assigns_by_staff_mention
if post.user && post.topic && post.user.can_assign?
return if post.topic.assignment.present? && !force
# remove quotes, oneboxes and code blocks
doc = Nokogiri::HTML5.fragment(post.cooked)
doc.css(".quote, .onebox, pre, code").remove
text = doc.text.strip
assign_other = assigned_other?(text) && mentioned_staff(post)
assign_self = assigned_self?(text) && post.user
return unless assign_other || assign_self
if is_last_staff_post?(post)
assigner = new(post.topic, post.user)
if assign_other
assigner.assign(assign_other, silent: true)
elsif assign_self
assigner.assign(assign_self, silent: true)
end
end
end
end
def self.is_last_staff_post?(post)
allowed_user_ids = User.assign_allowed.pluck(:id).join(',')
sql = <<~SQL
SELECT 1
FROM posts p
JOIN users u ON u.id = p.user_id
WHERE p.deleted_at IS NULL
AND p.topic_id = :topic_id
AND u.id IN (#{allowed_user_ids})
HAVING MAX(post_number) = :post_number
SQL
args = {
topic_id: post.topic_id,
post_number: post.post_number
}
DB.exec(sql, args) == 1
end
def self.mentioned_staff(post)
mentions = post.raw_mentions
if mentions.present?
User.human_users
.assign_allowed
.where('username_lower IN (?)', mentions.map(&:downcase))
.first
end
end
def self.publish_topic_tracking_state(topic, user_id)
if topic.private_message?
MessageBus.publish(
"/private-messages/assigned",
{ topic_id: topic.id },
user_ids: [user_id]
)
end
end
def initialize(target, user)
@assigned_by = user
@target = target
end
def allowed_user_ids
@allowed_user_ids ||= User.assign_allowed.pluck(:id)
end
def allowed_group_ids
@allowed_group_ids ||= Group.assignable(@assigned_by).pluck(:id)
end
def can_assign_to?(assign_to)
return true if assign_to.is_a?(Group)
return true if @assigned_by.id == assign_to.id
assigned_total = Assignment
.joins_with_topics
.where(topics: { deleted_at: nil })
.where(assigned_to_id: assign_to.id, active: true)
.count
assigned_total < SiteSetting.max_assigned_topics
end
def can_be_assigned?(assign_to)
if assign_to.is_a?(User)
allowed_user_ids.include?(assign_to.id)
else
allowed_group_ids.include?(assign_to.id)
end
end
def topic_target?
@topic_target ||= @target.is_a?(Topic)
end
def post_target?
@post_target ||= @target.is_a?(Post)
end
def can_assignee_see_target?(assignee)
return Guardian.new(assignee).can_see_topic?(@target) if topic_target?
return Guardian.new(assignee).can_see_post?(@target) if post_target?
raise Discourse::InvalidAccess
end
def topic
return @topic if @topic
@topic = @target if topic_target?
@topic = @target.topic if post_target?
raise Discourse::InvalidParameters if !@topic
@topic
end
def first_post
topic.posts.where(post_number: 1).first
end
def forbidden_reasons(assign_to:, type:)
case
when assign_to.is_a?(User) && !can_assignee_see_target?(assign_to)
topic.private_message? ? :forbidden_assignee_not_pm_participant : :forbidden_assignee_cant_see_topic
when assign_to.is_a?(Group) && assign_to.users.any? { |user| !can_assignee_see_target?(user) }
topic.private_message? ? :forbidden_group_assignee_not_pm_participant : :forbidden_group_assignee_cant_see_topic
when !can_be_assigned?(assign_to)
assign_to.is_a?(User) ? :forbidden_assign_to : :forbidden_group_assign_to
when topic.assignment&.assigned_to_id == assign_to.id && topic.assignment&.assigned_to_type == type && topic.assignment.active == true
assign_to.is_a?(User) ? :already_assigned : :group_already_assigned
when @target.is_a?(Topic) && Assignment.where(topic_id: topic.id, target_type: "Post", active: true).any? { |assignment| assignment.assigned_to_id == assign_to.id && assignment.assigned_to_type == type }
assign_to.is_a?(User) ? :already_assigned : :group_already_assigned
when Assignment.where(topic: topic).count >= ASSIGNMENTS_PER_TOPIC_LIMIT
:too_many_assigns_for_topic
when !can_assign_to?(assign_to)
:too_many_assigns
end
end
def assign(assign_to, priority: nil, silent: false)
type = assign_to.is_a?(User) ? "User" : "Group"
forbidden_reason = forbidden_reasons(assign_to: assign_to, type: type)
return { success: false, reason: forbidden_reason } if forbidden_reason
action_code = {}
action_code[:user] = topic.assignment.present? ? "reassigned" : "assigned"
action_code[:group] = topic.assignment.present? ? "reassigned_group" : "assigned_group"
@target.assignment&.destroy!
assignment = @target.create_assignment!(assigned_to_id: assign_to.id, assigned_to_type: type, assigned_by_user_id: @assigned_by.id, topic_id: topic.id, priority: priority)
first_post.publish_change_to_clients!(:revised, reload_topic: true)
serializer = assignment.assigned_to_user? ? BasicUserSerializer : BasicGroupSerializer
Jobs.enqueue(:assign_notification,
topic_id: topic.id,
post_id: topic_target? ? first_post.id : @target.id,
assigned_to_id: assign_to.id,
assigned_to_type: type,
assigned_by_id: @assigned_by.id,
silent: silent)
MessageBus.publish(
"/staff/topic-assignment",
{
type: "assigned",
topic_id: topic.id,
post_id: post_target? && @target.id,
post_number: post_target? && @target.post_number,
assigned_type: type,
assigned_to: serializer.new(assign_to, scope: Guardian.new, root: false).as_json
},
user_ids: allowed_user_ids
)
if assignment.assigned_to_user?
if !TopicUser.exists?(
user_id: assign_to.id,
topic_id: topic.id,
notification_level: TopicUser.notification_levels[:watching]
)
TopicUser.change(
assign_to.id,
topic.id,
notification_level: TopicUser.notification_levels[:watching],
notifications_reason_id: TopicUser.notification_reasons[:plugin_changed]
)
end
if SiteSetting.assign_mailer == AssignMailer.levels[:always] || (SiteSetting.assign_mailer == AssignMailer.levels[:different_users] && @assigned_by.id != assign_to.id)
if !topic.muted?(assign_to)
message = AssignMailer.send_assignment(assign_to.email, topic, @assigned_by)
Email::Sender.new(message, :assign_message).send
end
end
end
if !silent
custom_fields = { "action_code_who" => assign_to.is_a?(User) ? assign_to.username : assign_to.name }
if post_target?
custom_fields.merge!({ "action_code_path" => "/p/#{@target.id}", "action_code_post_id" => @target.id })
end
topic.add_moderator_post(
@assigned_by,
nil,
bump: false,
post_type: SiteSetting.assigns_public ? Post.types[:small_action] : Post.types[:whisper],
action_code: moderator_post_assign_action_code(assignment, action_code),
custom_fields: custom_fields
)
end
# Create a webhook event
if WebHook.active_web_hooks(:assign).exists?
type = :assigned
payload = {
type: type,
topic_id: topic.id,
topic_title: topic.title,
assigned_by_id: @assigned_by.id,
assigned_by_username: @assigned_by.username
}
if assignment.assigned_to_user?
payload.merge!({
assigned_to_id: assign_to.id,
assigned_to_username: assign_to.username,
})
else
payload.merge!({
assigned_to_group_id: assign_to.id,
assigned_to_group_name: assign_to.name,
})
end
WebHook.enqueue_assign_hooks(type, payload.to_json)
end
{ success: true }
end
def unassign(silent: false, deactivate: false)
if assignment = @target.assignment
deactivate ? assignment.update!(active: false) : assignment.destroy!
return if first_post.blank?
first_post.publish_change_to_clients!(:revised, reload_topic: true)
Jobs.enqueue(:unassign_notification,
topic_id: topic.id,
assigned_to_id: assignment.assigned_to.id,
assigned_to_type: assignment.assigned_to_type)
if assignment.assigned_to_user?
if TopicUser.exists?(
user_id: assignment.assigned_to_id,
topic: topic,
notification_level: TopicUser.notification_levels[:watching],
notifications_reason_id: TopicUser.notification_reasons[:plugin_changed]
)
TopicUser.change(
assignment.assigned_to_id,
topic.id,
notification_level: TopicUser.notification_levels[:tracking],
notifications_reason_id: TopicUser.notification_reasons[:plugin_changed]
)
end
end
assigned_to = assignment.assigned_to
if SiteSetting.unassign_creates_tracking_post && !silent
post_type = SiteSetting.assigns_public ? Post.types[:small_action] : Post.types[:whisper]
custom_fields = { "action_code_who" => assigned_to.is_a?(User) ? assigned_to.username : assigned_to.name }
if post_target?
custom_fields.merge!("action_code_path" => "/p/#{@target.id}")
custom_fields.merge!("action_code_post_id" => @target.id)
end
topic.add_moderator_post(
@assigned_by, nil,
bump: false,
post_type: post_type,
custom_fields: custom_fields,
action_code: moderator_post_unassign_action_code(assignment),
)
end
# Create a webhook event
if WebHook.active_web_hooks(:assign).exists?
type = :unassigned
payload = {
type: type,
topic_id: topic.id,
topic_title: topic.title,
unassigned_by_id: @assigned_by.id,
unassigned_by_username: @assigned_by.username
}
if assignment.assigned_to_user?
payload.merge!({
unassigned_to_id: assigned_to.id,
unassigned_to_username: assigned_to.username,
})
else
payload.merge!({
unassigned_to_group_id: assigned_to.id,
unassigned_to_group_name: assigned_to.name,
})
end
WebHook.enqueue_assign_hooks(type, payload.to_json)
end
MessageBus.publish(
"/staff/topic-assignment",
{
type: 'unassigned',
topic_id: topic.id,
post_id: post_target? && @target.id,
post_number: post_target? && @target.post_number,
assigned_type: assignment.assigned_to.is_a?(User) ? "User" : "Group"
},
user_ids: allowed_user_ids
)
end
end
private
def moderator_post_assign_action_code(assignment, action_code)
if assignment.target.is_a?(Post)
# posts do not have to handle conditions of 'assign' or 'reassign'
assignment.assigned_to_user? ? "assigned_to_post" : "assigned_group_to_post"
elsif assignment.target.is_a?(Topic)
assignment.assigned_to_user? ? "#{action_code[:user]}" : "#{action_code[:group]}"
end
end
def moderator_post_unassign_action_code(assignment)
suffix =
if assignment.target.is_a?(Post)
"_from_post"
elsif assignment.target.is_a?(Topic)
""
end
return "unassigned#{suffix}" if assignment.assigned_to_user?
return "unassigned_group#{suffix}" if assignment.assigned_to_group?
end
end