forked from pingcap/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_action_creator.rb
300 lines (253 loc) · 8.44 KB
/
post_action_creator.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
# frozen_string_literal: true
require_dependency 'post_action_result'
class PostActionCreator
class CreateResult < PostActionResult
attr_accessor :post_action, :reviewable, :reviewable_score
end
# Shortcut methods for easier invocation
class << self
def create(created_by, post, action_key, message: nil, created_at: nil)
new(created_by, post, PostActionType.types[action_key], message: message, created_at: created_at).perform
end
[:like, :off_topic, :spam, :inappropriate, :bookmark].each do |action|
define_method(action) do |created_by, post|
create(created_by, post, action)
end
end
[:notify_moderators, :notify_user].each do |action|
define_method(action) do |created_by, post, message = nil|
create(created_by, post, action, message: message)
end
end
end
def initialize(
created_by,
post,
post_action_type_id,
is_warning: false,
message: nil,
take_action: false,
flag_topic: false,
created_at: nil
)
@created_by = created_by
@created_at = created_at || Time.zone.now
@post = post
@post_action_type_id = post_action_type_id
@post_action_name = PostActionType.types[@post_action_type_id]
@is_warning = is_warning
@take_action = take_action && guardian.is_staff?
@message = message
@flag_topic = flag_topic
@meta_post = nil
end
def perform
result = CreateResult.new
unless guardian.post_can_act?(
@post,
@post_action_name,
opts: {
is_warning: @is_warning,
taken_actions: PostAction.counts_for([@post].compact, @created_by)[@post&.id]
}
)
result.forbidden = true
result.add_error(I18n.t("invalid_access"))
return result
end
PostAction.limit_action!(@created_by, @post, @post_action_type_id)
# create meta topic / post if needed
if @message.present? && [:notify_moderators, :notify_user, :spam].include?(@post_action_name)
creator = create_message_creator
post = creator.create
if creator.errors.present?
result.add_errors_from(creator)
return result
end
@meta_post = post
end
begin
post_action = create_post_action
if post_action.blank? || post_action.errors.present?
result.add_errors_from(post_action)
else
create_reviewable(result)
enforce_rules
UserActionManager.post_action_created(post_action)
PostActionNotifier.post_action_created(post_action)
notify_subscribers
# agree with other flags
if @take_action && reviewable = @post.reviewable_flag
result.reviewable.perform(@created_by, :agree_and_keep)
post_action.try(:update_counters)
end
result.success = true
result.post_action = post_action
end
rescue ActiveRecord::RecordNotUnique
# If the user already performed this action, it's proably due to a different browser tab
# or non-debounced clicking. We can ignore.
result.success = true
result.post_action = PostAction.find_by(
user: @created_by,
post: @post,
post_action_type_id: @post_action_type_id
)
end
result
end
private
def notify_subscribers
if self.class.notify_types.include?(@post_action_name)
@post.publish_change_to_clients! :acted
end
end
def self.notify_types
@notify_types ||= ([:like] + PostActionType.notify_flag_types.keys)
end
def enforce_rules
auto_close_if_threshold_reached
auto_hide_if_needed
SpamRule::AutoSilence.new(@post.user, @post).perform
end
def auto_close_if_threshold_reached
return if topic.nil? || topic.closed?
return unless topic.auto_close_threshold_reached?
# the threshold has been reached, we will close the topic waiting for intervention
topic.update_status("closed", true, Discourse.system_user,
message: I18n.t(
"temporarily_closed_due_to_flags",
count: SiteSetting.num_hours_to_close_topic
)
)
topic.set_or_create_timer(
TopicTimer.types[:open],
SiteSetting.num_hours_to_close_topic,
by_user: Discourse.system_user
)
end
def auto_hide_if_needed
return if @post.hidden?
return if !@created_by.staff? && @post.user&.staff?
if @post_action_name == :spam &&
@created_by.has_trust_level?(TrustLevel[3]) &&
@post.user&.trust_level == TrustLevel[0]
@post.hide!(@post_action_type_id, Post.hidden_reasons[:flagged_by_tl3_user])
elsif PostActionType.auto_action_flag_types.include?(@post_action_name)
if @created_by.has_trust_level?(TrustLevel[4]) &&
!@created_by.staff? &&
@post.user&.trust_level != TrustLevel[4]
@post.hide!(@post_action_type_id, Post.hidden_reasons[:flagged_by_tl4_user])
else
score = ReviewableFlaggedPost.find_by(target: @post)&.score || 0
if score >= Reviewable.score_required_to_hide_post
@post.hide!(@post_action_type_id)
end
end
end
end
def create_post_action
@targets_topic = !!(
if @flag_topic && @post.topic
@post.topic.reload.posts_count != 1
end
)
where_attrs = {
post_id: @post.id,
user_id: @created_by.id,
post_action_type_id: @post_action_type_id
}
action_attrs = {
staff_took_action: @take_action,
related_post_id: @meta_post&.id,
targets_topic: @targets_topic,
created_at: @created_at
}
# First try to revive a trashed record
post_action = PostAction.where(where_attrs)
.with_deleted
.where("deleted_at IS NOT NULL")
.first
if post_action
post_action.recover!
action_attrs.each { |attr, val| post_action.public_send("#{attr}=", val) }
post_action.save
PostActionNotifier.post_action_created(post_action)
else
post_action = PostAction.create(where_attrs.merge(action_attrs))
if post_action && post_action.errors.count == 0
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: post_action)
end
end
if post_action && PostActionType.notify_flag_type_ids.include?(@post_action_type_id)
DiscourseEvent.trigger(:flag_created, post_action)
end
GivenDailyLike.increment_for(@created_by.id) if @post_action_type_id == PostActionType.types[:like]
post_action
rescue ActiveRecord::RecordNotUnique
# can happen despite being .create
# since already bookmarked
PostAction.where(where_attrs).first
end
def create_message_creator
title = I18n.t(
"post_action_types.#{@post_action_name}.email_title",
title: @post.topic.title,
locale: SiteSetting.default_locale
)
body = I18n.t(
"post_action_types.#{@post_action_name}.email_body",
message: @message,
link: "#{Discourse.base_url}#{@post.url}",
locale: SiteSetting.default_locale
)
create_args = {
archetype: Archetype.private_message,
is_warning: @is_warning,
title: title.truncate(SiteSetting.max_topic_title_length, separator: /\s/),
raw: body
}
if [:notify_moderators, :spam].include?(@post_action_name)
create_args[:subtype] = TopicSubtype.notify_moderators
create_args[:target_group_names] = Group[:moderators].name
else
create_args[:subtype] = TopicSubtype.notify_user
create_args[:target_usernames] =
if @post_action_name == :notify_user
@post.user.username
elsif @post_action_name != :notify_moderators
# this is a hack to allow a PM with no recipients, we should think through
# a cleaner technique, a PM with myself is valid for flagging
'x'
end
end
PostCreator.new(@created_by, create_args)
end
def create_reviewable(result)
return unless PostActionType.notify_flag_type_ids.include?(@post_action_type_id)
return if @post.user_id.to_i < 0
result.reviewable = ReviewableFlaggedPost.needs_review!(
created_by: @created_by,
target: @post,
topic: @post.topic,
reviewable_by_moderator: true,
potential_spam: @post_action_type_id == PostActionType.types[:spam],
payload: {
targets_topic: @targets_topic
}
)
result.reviewable_score = result.reviewable.add_score(
@created_by,
@post_action_type_id,
created_at: @created_at,
take_action: @take_action,
meta_topic_id: @meta_post&.topic_id,
)
end
def guardian
@guardian ||= Guardian.new(@created_by)
end
def topic
@post.topic
end
end