Fix #4669: Track moderation report status.

* Add ability to mark moderation reports as 'handled' or 'rejected'.
* Automatically mark reports as handled when the comment or forum post
  is deleted.
* Send a dmail to the reporter when their report is handled.
* Don't show the report notice on comments or forum posts when all
  reports against it have been handled or rejected.
* Add a fix script to mark all existing reports for deleted comments,
  forum posts, or dmails as handled.
This commit is contained in:
evazion
2022-01-20 17:41:05 -06:00
parent 98aee048f2
commit c8d27c2719
20 changed files with 160 additions and 16 deletions

View File

@@ -6,11 +6,13 @@ class Comment < ApplicationRecord
belongs_to_updater
has_many :moderation_reports, as: :model, dependent: :destroy
has_many :pending_moderation_reports, -> { pending }, as: :model, class_name: "ModerationReport"
has_many :votes, class_name: "CommentVote", dependent: :destroy
validates :body, presence: true, length: { maximum: 15_000 }, if: :body_changed?
before_create :autoreport_spam
before_save :handle_reports_on_deletion
after_create :update_last_commented_at_on_create
after_update(:if => ->(rec) {(!rec.is_deleted? || !rec.saved_change_to_is_deleted?) && CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log("comment ##{rec.id} updated by #{CurrentUser.user.name}", :comment_update)
@@ -78,6 +80,13 @@ class Comment < ApplicationRecord
end
end
def handle_reports_on_deletion
return unless Pundit.policy!(updater, ModerationReport).update?
return unless moderation_reports.pending.present? && is_deleted_change == [false, true]
moderation_reports.pending.update!(status: :handled)
end
def quoted_response
DText.quote(body, creator.name)
end

View File

@@ -8,6 +8,7 @@ class ForumPost < ApplicationRecord
belongs_to :topic, class_name: "ForumTopic", inverse_of: :forum_posts
has_many :moderation_reports, as: :model
has_many :pending_moderation_reports, -> { pending }, as: :model, class_name: "ModerationReport"
has_many :votes, class_name: "ForumPostVote"
has_one :tag_alias
has_one :tag_implication
@@ -16,6 +17,7 @@ class ForumPost < ApplicationRecord
validates :body, presence: true, length: { maximum: 200_000 }, if: :body_changed?
before_create :autoreport_spam
before_save :handle_reports_on_deletion
after_create :update_topic_updated_at_on_create
after_update :update_topic_updated_at_on_update_for_original_posts
after_destroy :update_topic_updated_at_on_destroy
@@ -157,6 +159,12 @@ class ForumPost < ApplicationRecord
end
end
def handle_reports_on_deletion
return unless moderation_reports.pending.present? && is_deleted_change == [false, true]
moderation_reports.pending.update!(status: :handled)
end
def async_send_discord_notification
DiscordNotificationJob.perform_later(forum_post: self)
end

View File

@@ -12,12 +12,19 @@ class ModerationReport < ApplicationRecord
after_create :create_forum_post!
after_create :autoban_reported_user
after_save :notify_reporter
scope :dmail, -> { where(model_type: "Dmail") }
scope :comment, -> { where(model_type: "Comment") }
scope :forum_post, -> { where(model_type: "ForumPost") }
scope :recent, -> { where("moderation_reports.created_at >= ?", 1.week.ago) }
enum status: {
pending: 0,
rejected: 1,
handled: 2,
}
def self.model_types
MODEL_TYPES
end
@@ -73,6 +80,15 @@ class ModerationReport < ApplicationRecord
end
end
def notify_reporter
return if creator == User.system
return unless handled? && status_before_last_save != :handled
Dmail.create_automated(to: creator, title: "Thank you for reporting #{model.dtext_shortlink}", body: <<~EOS)
Thank you for reporting #{model.dtext_shortlink}. Action has been taken against the user.
EOS
end
def reported_user
case model
when Comment, ForumPost
@@ -85,7 +101,7 @@ class ModerationReport < ApplicationRecord
end
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :creator, :model)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :creator, :model, :status)
q = q.text_attribute_matches(:reason, params[:reason_matches])
q.apply_default_order(params)