Class: ReviewableFlaggedPost

Inherits:
Reviewable
  • Object
show all
Defined in:
app/models/reviewable_flagged_post.rb

Constant Summary

Constants inherited from Reviewable

Reviewable::TYPE_TO_BASIC_SERIALIZER

Instance Attribute Summary

Attributes inherited from Reviewable

#created_new

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reviewable

#actions_for, add_custom_filter, #add_score, #apply_review_group, #basic_serializer, basic_serializers_for_list, #build_editable_fields, bulk_perform_targets, clear_custom_filters!, count_by_date, #create_result, #created_new!, custom_filters, default_visible, #delete_user_actions, #editable_for, #explain_score, #history, list_for, #log_history, lookup_serializer_for, min_score_for_priority, needs_review!, pending_count, #perform, #recalculate_score, score_required_to_hide_post, score_to_auto_close_topic, scores_with_topics, sensitivity_score, sensitivity_score_value, #serializer, serializer_for, set_priorities, spam_score_to_silence_new_user, #transition_to, types, typical_sensitivity, unseen_list_for, unseen_reviewable_count, #updatable_reviewable_scores, #update_fields, user_menu_list_for, valid_type?, viewable_by

Class Method Details

.action_aliasesObject

Penalties are handled by the modal after the action is performed



7
8
9
10
11
12
13
14
15
# File 'app/models/reviewable_flagged_post.rb', line 7

def self.action_aliases
  {
    agree_and_keep_hidden: :agree_and_keep,
    agree_and_silence: :agree_and_keep,
    agree_and_suspend: :agree_and_keep,
    disagree_and_restore: :disagree,
    ignore_and_do_nothing: :ignore,
  }
end

.counts_for(posts) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/reviewable_flagged_post.rb', line 17

def self.counts_for(posts)
  result = {}

  counts = DB.query(<<~SQL, pending: statuses[:pending])
    SELECT r.target_id AS post_id,
      rs.reviewable_score_type,
      count(*) as total
    FROM reviewables AS r
    INNER JOIN reviewable_scores AS rs ON rs.reviewable_id = r.id
    WHERE r.type = 'ReviewableFlaggedPost'
      AND r.status = :pending
    GROUP BY r.target_id, rs.reviewable_score_type
  SQL

  counts.each do |c|
    result[c.post_id] ||= {}
    result[c.post_id][c.reviewable_score_type] = c.total
  end

  result
end

Instance Method Details

#build_actions(actions, guardian, args) ⇒ Object



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
# File 'app/models/reviewable_flagged_post.rb', line 43

def build_actions(actions, guardian, args)
  return unless pending?
  return if post.blank?

  agree_bundle =
    actions.add_bundle("#{id}-agree", icon: "thumbs-up", label: "reviewables.actions.agree.title")

  if potential_spam? && guardian.can_delete_user?(target_created_by)
    (actions, agree_bundle)
  end

  if !post.user_deleted? && !post.hidden?
    build_action(actions, :agree_and_hide, icon: "far-eye-slash", bundle: agree_bundle)
  end

  if post.hidden?
    build_action(actions, :agree_and_keep_hidden, icon: "thumbs-up", bundle: agree_bundle)
  else
    build_action(actions, :agree_and_keep, icon: "thumbs-up", bundle: agree_bundle)
  end

  if guardian.can_delete_post_or_topic?(post)
    build_action(actions, :delete_and_agree, icon: "far-trash-alt", bundle: agree_bundle)

    if post.reply_count > 0
      build_action(
        actions,
        :delete_and_agree_replies,
        icon: "far-trash-alt",
        bundle: agree_bundle,
        confirm: true,
      )
    end
  end

  if guardian.can_suspend?(target_created_by)
    build_action(
      actions,
      :agree_and_suspend,
      icon: "ban",
      bundle: agree_bundle,
      client_action: "suspend",
    )
    build_action(
      actions,
      :agree_and_silence,
      icon: "microphone-slash",
      bundle: agree_bundle,
      client_action: "silence",
    )
  end

  if post.user_deleted?
    build_action(actions, :agree_and_restore, icon: "far-eye", bundle: agree_bundle)
  end
  if post.hidden?
    build_action(actions, :disagree_and_restore, icon: "thumbs-down")
  else
    build_action(actions, :disagree, icon: "thumbs-down")
  end

  ignore =
    actions.add_bundle(
      "#{id}-ignore",
      icon: "thumbs-up",
      label: "reviewables.actions.ignore.title",
    )

  if !post.hidden?
    build_action(actions, :ignore_and_do_nothing, icon: "external-link-alt", bundle: ignore)
  end
  if guardian.can_delete_post_or_topic?(post)
    build_action(actions, :delete_and_ignore, icon: "far-trash-alt", bundle: ignore)
    if post.reply_count > 0
      build_action(
        actions,
        :delete_and_ignore_replies,
        icon: "far-trash-alt",
        confirm: true,
        bundle: ignore,
      )
    end
  end
end

#perform_agree_and_hide(performed_by, args) ⇒ Object



182
183
184
# File 'app/models/reviewable_flagged_post.rb', line 182

def perform_agree_and_hide(performed_by, args)
  agree(performed_by, args) { |pa| post.hide!(pa.post_action_type_id) }
end

#perform_agree_and_keep(performed_by, args) ⇒ Object



160
161
162
# File 'app/models/reviewable_flagged_post.rb', line 160

def perform_agree_and_keep(performed_by, args)
  agree(performed_by, args)
end

#perform_agree_and_restore(performed_by, args) ⇒ Object



186
187
188
# File 'app/models/reviewable_flagged_post.rb', line 186

def perform_agree_and_restore(performed_by, args)
  agree(performed_by, args) { PostDestroyer.new(performed_by, post).recover }
end

#perform_delete_and_agree(performed_by, args) ⇒ Object



250
251
252
253
254
# File 'app/models/reviewable_flagged_post.rb', line 250

def perform_delete_and_agree(performed_by, args)
  result = agree(performed_by, args)
  destroyer(performed_by, post).destroy
  result
end

#perform_delete_and_agree_replies(performed_by, args) ⇒ Object



256
257
258
259
260
# File 'app/models/reviewable_flagged_post.rb', line 256

def perform_delete_and_agree_replies(performed_by, args)
  result = agree(performed_by, args)
  PostDestroyer.delete_with_replies(performed_by, post, self)
  result
end

#perform_delete_and_ignore(performed_by, args) ⇒ Object



237
238
239
240
241
# File 'app/models/reviewable_flagged_post.rb', line 237

def perform_delete_and_ignore(performed_by, args)
  result = perform_ignore_and_do_nothing(performed_by, args)
  destroyer(performed_by, post).destroy
  result
end

#perform_delete_and_ignore_replies(performed_by, args) ⇒ Object



243
244
245
246
247
248
# File 'app/models/reviewable_flagged_post.rb', line 243

def perform_delete_and_ignore_replies(performed_by, args)
  result = perform_ignore_and_do_nothing(performed_by, args)
  PostDestroyer.delete_with_replies(performed_by, post, self)

  result
end

#perform_delete_user(performed_by, args) ⇒ Object



164
165
166
167
168
169
170
# File 'app/models/reviewable_flagged_post.rb', line 164

def perform_delete_user(performed_by, args)
  delete_options = delete_opts

  UserDestroyer.new(performed_by).destroy(post.user, delete_options)

  agree(performed_by, args)
end

#perform_delete_user_block(performed_by, args) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'app/models/reviewable_flagged_post.rb', line 172

def perform_delete_user_block(performed_by, args)
  delete_options = delete_opts

  delete_options.merge!(block_email: true, block_ip: true) if Rails.env.production?

  UserDestroyer.new(performed_by).destroy(post.user, delete_options)

  agree(performed_by, args)
end

#perform_disagree(performed_by, args) ⇒ Object



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
# File 'app/models/reviewable_flagged_post.rb', line 190

def perform_disagree(performed_by, args)
  # -1 is the automatic system clear
  action_type_ids =
    if performed_by.id == Discourse::SYSTEM_USER_ID
      PostActionType.auto_action_flag_types.values
    else
      PostActionType.notify_flag_type_ids
    end

  actions =
    PostAction.active.where(post_id: target_id).where(post_action_type_id: action_type_ids)

  actions.each do |action|
    action.disagreed_at = Time.zone.now
    action.disagreed_by_id = performed_by.id
    # so callback is called
    action.save
    action.add_moderator_post_if_needed(performed_by, :disagreed)
  end

  # reset all cached counters
  cached = {}
  action_type_ids.each do |atid|
    column = "#{PostActionType.types[atid]}_count"
    cached[column] = 0 if ActiveRecord::Base.connection.column_exists?(:posts, column)
  end

  Post.with_deleted.where(id: target_id).update_all(cached)

  if actions.first.present?
    unassign_topic performed_by, post
    DiscourseEvent.trigger(:flag_reviewed, post)
    DiscourseEvent.trigger(:flag_disagreed, actions.first)
  end

  # Undo hide/silence if applicable
  if post&.hidden?
    notify_poster(performed_by)
    post.unhide!
    UserSilencer.unsilence(post.user) if UserSilencer.was_silenced_for?(post)
  end

  create_result(:success, :rejected) do |result|
    result.update_flag_stats = { status: :disagreed, user_ids: actions.map(&:user_id) }
  end
end

#perform_ignore(performed_by, args) ⇒ Object



128
129
130
# File 'app/models/reviewable_flagged_post.rb', line 128

def perform_ignore(performed_by, args)
  perform_ignore_and_do_nothing(performed_by, args)
end

#perform_ignore_and_do_nothing(performed_by, args) ⇒ Object



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
# File 'app/models/reviewable_flagged_post.rb', line 132

def perform_ignore_and_do_nothing(performed_by, args)
  actions =
    PostAction
      .active
      .where(post_id: target_id)
      .where(post_action_type_id: PostActionType.notify_flag_type_ids)

  actions.each do |action|
    action.deferred_at = Time.zone.now
    action.deferred_by_id = performed_by.id
    # so callback is called
    action.save
    unless args[:expired]
      action.add_moderator_post_if_needed(performed_by, :ignored, args[:post_was_deleted])
    end
  end

  if actions.first.present?
    unassign_topic performed_by, post
    DiscourseEvent.trigger(:flag_reviewed, post)
    DiscourseEvent.trigger(:flag_deferred, actions.first)
  end

  create_result(:success, :ignored) do |result|
    result.update_flag_stats = { status: :ignored, user_ids: actions.map(&:user_id) }
  end
end

#postObject



39
40
41
# File 'app/models/reviewable_flagged_post.rb', line 39

def post
  @post ||= (target || Post.with_deleted.find_by(id: target_id))
end