2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
class ReviewableFlaggedPost < Reviewable
|
2022-10-26 18:13:21 -04:00
|
|
|
scope :pending_and_default_visible, -> { pending.default_visible }
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-08-01 11:23:23 -04:00
|
|
|
# Penalties are handled by the modal after the action is performed
|
|
|
|
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,
|
2023-03-02 10:40:53 -05:00
|
|
|
ignore_and_do_nothing: :ignore,
|
2019-08-01 11:23:23 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def self.counts_for(posts)
|
|
|
|
result = {}
|
|
|
|
|
2021-12-08 12:12:24 -05:00
|
|
|
counts = DB.query(<<~SQL, pending: statuses[:pending])
|
2019-01-03 12:03:01 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
def post
|
|
|
|
@post ||= (target || Post.with_deleted.find_by(id: target_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_actions(actions, guardian, args)
|
|
|
|
return unless pending?
|
|
|
|
return if post.blank?
|
|
|
|
|
2023-08-24 22:53:56 -04:00
|
|
|
agree_bundle =
|
2019-01-03 12:03:01 -05:00
|
|
|
actions.add_bundle("#{id}-agree", icon: "thumbs-up", label: "reviewables.actions.agree.title")
|
|
|
|
|
2023-08-24 22:53:56 -04:00
|
|
|
if potential_spam? && guardian.can_delete_user?(target_created_by)
|
|
|
|
delete_user_actions(actions, agree_bundle)
|
|
|
|
end
|
|
|
|
|
2019-06-26 11:35:59 -04:00
|
|
|
if !post.user_deleted? && !post.hidden?
|
2023-08-24 22:53:56 -04:00
|
|
|
build_action(actions, :agree_and_hide, icon: "far-eye-slash", bundle: agree_bundle)
|
2019-06-26 11:35:59 -04:00
|
|
|
end
|
|
|
|
|
2019-08-01 11:23:23 -04:00
|
|
|
if post.hidden?
|
2023-08-24 22:53:56 -04:00
|
|
|
build_action(actions, :agree_and_keep_hidden, icon: "thumbs-up", bundle: agree_bundle)
|
2019-08-01 11:23:23 -04:00
|
|
|
else
|
2023-08-24 22:53:56 -04:00
|
|
|
build_action(actions, :agree_and_keep, icon: "thumbs-up", bundle: agree_bundle)
|
2019-08-01 11:23:23 -04:00
|
|
|
end
|
|
|
|
|
2023-03-02 10:40:53 -05:00
|
|
|
if guardian.can_delete_post_or_topic?(post)
|
2023-08-24 22:53:56 -04:00
|
|
|
build_action(actions, :delete_and_agree, icon: "far-trash-alt", bundle: agree_bundle)
|
2023-03-02 10:40:53 -05:00
|
|
|
|
|
|
|
if post.reply_count > 0
|
|
|
|
build_action(
|
|
|
|
actions,
|
|
|
|
:delete_and_agree_replies,
|
|
|
|
icon: "far-trash-alt",
|
2023-08-24 22:53:56 -04:00
|
|
|
bundle: agree_bundle,
|
2023-03-02 10:40:53 -05:00
|
|
|
confirm: true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
if guardian.can_suspend?(target_created_by)
|
|
|
|
build_action(
|
|
|
|
actions,
|
|
|
|
:agree_and_suspend,
|
|
|
|
icon: "ban",
|
2023-08-24 22:53:56 -04:00
|
|
|
bundle: agree_bundle,
|
2019-01-03 12:03:01 -05:00
|
|
|
client_action: "suspend",
|
|
|
|
)
|
|
|
|
build_action(
|
|
|
|
actions,
|
|
|
|
:agree_and_silence,
|
|
|
|
icon: "microphone-slash",
|
2023-08-24 22:53:56 -04:00
|
|
|
bundle: agree_bundle,
|
2019-01-03 12:03:01 -05:00
|
|
|
client_action: "silence",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-08-24 22:53:56 -04:00
|
|
|
if post.user_deleted?
|
|
|
|
build_action(actions, :agree_and_restore, icon: "far-eye", bundle: agree_bundle)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
if post.hidden?
|
|
|
|
build_action(actions, :disagree_and_restore, icon: "thumbs-down")
|
|
|
|
else
|
|
|
|
build_action(actions, :disagree, icon: "thumbs-down")
|
|
|
|
end
|
|
|
|
|
2023-03-02 10:40:53 -05:00
|
|
|
ignore =
|
|
|
|
actions.add_bundle(
|
|
|
|
"#{id}-ignore",
|
|
|
|
icon: "thumbs-up",
|
|
|
|
label: "reviewables.actions.ignore.title",
|
|
|
|
)
|
2021-06-15 11:35:45 -04:00
|
|
|
|
2023-03-02 10:40:53 -05:00
|
|
|
if !post.hidden?
|
|
|
|
build_action(actions, :ignore_and_do_nothing, icon: "external-link-alt", bundle: ignore)
|
|
|
|
end
|
2019-05-30 16:42:59 -04:00
|
|
|
if guardian.can_delete_post_or_topic?(post)
|
2023-03-02 10:40:53 -05:00
|
|
|
build_action(actions, :delete_and_ignore, icon: "far-trash-alt", bundle: ignore)
|
2019-04-04 15:51:36 -04:00
|
|
|
if post.reply_count > 0
|
|
|
|
build_action(
|
|
|
|
actions,
|
|
|
|
:delete_and_ignore_replies,
|
2023-03-02 10:40:53 -05:00
|
|
|
icon: "far-trash-alt",
|
2019-04-04 15:51:36 -04:00
|
|
|
confirm: true,
|
2023-03-02 10:40:53 -05:00
|
|
|
bundle: ignore,
|
2019-04-04 15:51:36 -04:00
|
|
|
)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_ignore(performed_by, args)
|
2023-03-02 10:40:53 -05:00
|
|
|
perform_ignore_and_do_nothing(performed_by, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_ignore_and_do_nothing(performed_by, args)
|
2019-01-03 12:03:01 -05:00
|
|
|
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
|
2019-05-27 17:22:39 -04:00
|
|
|
unless args[:expired]
|
|
|
|
action.add_moderator_post_if_needed(performed_by, :ignored, args[:post_was_deleted])
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if actions.first.present?
|
2020-03-24 17:57:44 -04:00
|
|
|
unassign_topic performed_by, post
|
2019-01-03 12:03:01 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
def perform_agree_and_keep(performed_by, args)
|
|
|
|
agree(performed_by, args)
|
|
|
|
end
|
|
|
|
|
2021-06-15 11:35:45 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
agree(performed_by, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_agree_and_hide(performed_by, args)
|
|
|
|
agree(performed_by, args) { |pa| post.hide!(pa.post_action_type_id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_agree_and_restore(performed_by, args)
|
|
|
|
agree(performed_by, args) { PostDestroyer.new(performed_by, post).recover }
|
|
|
|
end
|
|
|
|
|
|
|
|
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?
|
2020-03-24 17:57:44 -04:00
|
|
|
unassign_topic performed_by, post
|
2019-01-03 12:03:01 -05:00
|
|
|
DiscourseEvent.trigger(:flag_reviewed, post)
|
|
|
|
DiscourseEvent.trigger(:flag_disagreed, actions.first)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Undo hide/silence if applicable
|
|
|
|
if post&.hidden?
|
2019-08-30 09:27:52 -04:00
|
|
|
notify_poster(performed_by)
|
2019-01-03 12:03:01 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
def perform_delete_and_ignore(performed_by, args)
|
2023-03-02 10:40:53 -05:00
|
|
|
result = perform_ignore_and_do_nothing(performed_by, args)
|
2019-08-30 09:27:52 -04:00
|
|
|
destroyer(performed_by, post).destroy
|
2019-01-03 12:03:01 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2019-04-04 15:51:36 -04:00
|
|
|
def perform_delete_and_ignore_replies(performed_by, args)
|
2023-03-02 10:40:53 -05:00
|
|
|
result = perform_ignore_and_do_nothing(performed_by, args)
|
2019-08-30 09:27:52 -04:00
|
|
|
PostDestroyer.delete_with_replies(performed_by, post, self)
|
2019-04-04 15:51:36 -04:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def perform_delete_and_agree(performed_by, args)
|
|
|
|
result = agree(performed_by, args)
|
2019-08-30 09:27:52 -04:00
|
|
|
destroyer(performed_by, post).destroy
|
2019-01-03 12:03:01 -05:00
|
|
|
result
|
|
|
|
end
|
2019-04-04 15:51:36 -04:00
|
|
|
|
|
|
|
def perform_delete_and_agree_replies(performed_by, args)
|
|
|
|
result = agree(performed_by, args)
|
2020-01-06 11:37:54 -05:00
|
|
|
PostDestroyer.delete_with_replies(performed_by, post, self)
|
2019-04-04 15:51:36 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
def agree(performed_by, args)
|
|
|
|
actions =
|
|
|
|
PostAction
|
|
|
|
.active
|
|
|
|
.where(post_id: target_id)
|
|
|
|
.where(post_action_type_id: PostActionType.notify_flag_types.values)
|
|
|
|
|
|
|
|
trigger_spam = false
|
|
|
|
actions.each do |action|
|
2019-04-25 05:15:13 -04:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
action.agreed_at = Time.zone.now
|
|
|
|
action.agreed_by_id = performed_by.id
|
|
|
|
# so callback is called
|
|
|
|
action.save
|
|
|
|
DB.after_commit do
|
|
|
|
action.add_moderator_post_if_needed(performed_by, :agreed, args[:post_was_deleted])
|
|
|
|
trigger_spam = true if action.post_action_type_id == PostActionType.types[:spam]
|
|
|
|
end
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
DiscourseEvent.trigger(:confirmed_spam_post, post) if trigger_spam
|
|
|
|
|
|
|
|
if actions.first.present?
|
2020-03-24 17:57:44 -04:00
|
|
|
unassign_topic performed_by, post
|
2019-01-03 12:03:01 -05:00
|
|
|
DiscourseEvent.trigger(:flag_reviewed, post)
|
|
|
|
DiscourseEvent.trigger(:flag_agreed, actions.first)
|
|
|
|
yield(actions.first) if block_given?
|
|
|
|
end
|
|
|
|
|
|
|
|
create_result(:success, :approved) do |result|
|
|
|
|
result.update_flag_stats = { status: :agreed, user_ids: actions.map(&:user_id) }
|
|
|
|
result.recalculate_score = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-18 06:28:59 -04:00
|
|
|
def build_action(
|
|
|
|
actions,
|
|
|
|
id,
|
|
|
|
icon:,
|
|
|
|
button_class: nil,
|
|
|
|
bundle: nil,
|
|
|
|
client_action: nil,
|
|
|
|
confirm: false
|
|
|
|
)
|
2019-01-03 12:03:01 -05:00
|
|
|
actions.add(id, bundle: bundle) do |action|
|
|
|
|
prefix = "reviewables.actions.#{id}"
|
|
|
|
action.icon = icon
|
2019-09-18 06:28:59 -04:00
|
|
|
action.button_class = button_class
|
2019-01-03 12:03:01 -05:00
|
|
|
action.label = "#{prefix}.title"
|
|
|
|
action.description = "#{prefix}.description"
|
|
|
|
action.client_action = client_action
|
|
|
|
action.confirm_message = "#{prefix}.confirm" if confirm
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-24 17:57:44 -04:00
|
|
|
def unassign_topic(performed_by, post)
|
|
|
|
topic = post.topic
|
|
|
|
return unless topic && performed_by && SiteSetting.reviewable_claiming != "disabled"
|
|
|
|
ReviewableClaimedTopic.where(topic_id: topic.id).delete_all
|
|
|
|
topic.reviewables.find_each { |reviewable| reviewable.log_history(:unclaimed, performed_by) }
|
|
|
|
|
|
|
|
user_ids = User.staff.pluck(:id)
|
|
|
|
|
2020-07-14 12:36:19 -04:00
|
|
|
if SiteSetting.enable_category_group_moderation? &&
|
|
|
|
group_id = topic.category&.reviewable_by_group_id.presence
|
2020-03-24 17:57:44 -04:00
|
|
|
user_ids.concat(GroupUser.where(group_id: group_id).pluck(:user_id))
|
|
|
|
user_ids.uniq!
|
|
|
|
end
|
|
|
|
|
|
|
|
data = { topic_id: topic.id }
|
|
|
|
|
|
|
|
MessageBus.publish("/reviewable_claimed", data, user_ids: user_ids)
|
|
|
|
end
|
|
|
|
|
2019-08-30 09:27:52 -04:00
|
|
|
private
|
|
|
|
|
2021-06-15 11:35:45 -04:00
|
|
|
def delete_opts
|
|
|
|
{
|
|
|
|
delete_posts: true,
|
|
|
|
prepare_for_destroy: true,
|
|
|
|
block_urls: true,
|
|
|
|
delete_as_spammer: true,
|
|
|
|
context: "review",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-08-30 09:27:52 -04:00
|
|
|
def destroyer(performed_by, post)
|
|
|
|
PostDestroyer.new(performed_by, post, reviewable: self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_poster(performed_by)
|
|
|
|
return unless performed_by.human? && performed_by.staff?
|
|
|
|
|
|
|
|
Jobs.enqueue(
|
|
|
|
:send_system_message,
|
|
|
|
user_id: post.user_id,
|
2022-02-07 15:18:17 -05:00
|
|
|
message_type: "flags_disagreed",
|
2019-08-30 09:27:52 -04:00
|
|
|
message_options: {
|
|
|
|
flagged_post_raw_content: post.raw,
|
|
|
|
url: post.url,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: reviewables
|
|
|
|
#
|
2019-05-02 18:34:12 -04:00
|
|
|
# id :bigint not null, primary key
|
2019-01-03 12:03:01 -05:00
|
|
|
# type :string not null
|
2021-12-08 12:12:24 -05:00
|
|
|
# status :integer default("pending"), not null
|
2019-01-03 12:03:01 -05:00
|
|
|
# created_by_id :integer not null
|
|
|
|
# reviewable_by_moderator :boolean default(FALSE), not null
|
|
|
|
# reviewable_by_group_id :integer
|
|
|
|
# category_id :integer
|
|
|
|
# topic_id :integer
|
|
|
|
# score :float default(0.0), not null
|
|
|
|
# potential_spam :boolean default(FALSE), not null
|
|
|
|
# target_id :integer
|
|
|
|
# target_type :string
|
|
|
|
# target_created_by_id :integer
|
|
|
|
# payload :json
|
|
|
|
# version :integer default(0), not null
|
|
|
|
# latest_score :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2021-07-05 18:14:15 -04:00
|
|
|
# force_review :boolean default(FALSE), not null
|
|
|
|
# reject_reason :text
|
2019-01-03 12:03:01 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2022-07-28 04:16:33 -04:00
|
|
|
# idx_reviewables_score_desc_created_at_desc (score,created_at)
|
2019-05-02 18:34:12 -04:00
|
|
|
# index_reviewables_on_reviewable_by_group_id (reviewable_by_group_id)
|
2019-04-12 09:55:27 -04:00
|
|
|
# index_reviewables_on_status_and_created_at (status,created_at)
|
|
|
|
# index_reviewables_on_status_and_score (status,score)
|
|
|
|
# index_reviewables_on_status_and_type (status,type)
|
2020-09-01 04:00:36 -04:00
|
|
|
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
|
2019-04-12 09:55:27 -04:00
|
|
|
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
|
|
|
|
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
|
2019-01-03 12:03:01 -05:00
|
|
|
#
|