From 5fd0d498a4542ded2f943d58f0f0f703e5795dee Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 20 Jan 2022 21:28:29 -0600 Subject: [PATCH] modreports: log modaction when report is handled or rejected. --- app/controllers/moderation_reports_controller.rb | 2 +- app/models/comment.rb | 2 +- app/models/forum_post.rb | 2 +- app/models/mod_action.rb | 2 ++ app/models/moderation_report.rb | 13 +++++++++++++ test/functional/comments_controller_test.rb | 1 + test/functional/forum_posts_controller_test.rb | 1 + .../moderation_reports_controller_test.rb | 2 ++ 8 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/controllers/moderation_reports_controller.rb b/app/controllers/moderation_reports_controller.rb index 99eefa52e..9648ba851 100644 --- a/app/controllers/moderation_reports_controller.rb +++ b/app/controllers/moderation_reports_controller.rb @@ -32,7 +32,7 @@ class ModerationReportsController < ApplicationController def update @moderation_report = authorize ModerationReport.find(params[:id]) - @moderation_report.update(permitted_attributes(@moderation_report)) + @moderation_report.update(updater: CurrentUser.user, **permitted_attributes(@moderation_report)) flash.now[:notice] = @moderation_report.valid? ? "Report updated" : @moderation_report.errors.full_messages.join("; ") respond_with(@moderation_report) diff --git a/app/models/comment.rb b/app/models/comment.rb index 8454569e0..c2ff4c37c 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -84,7 +84,7 @@ class Comment < ApplicationRecord return unless Pundit.policy!(updater, ModerationReport).update? return unless moderation_reports.pending.present? && is_deleted_change == [false, true] - moderation_reports.pending.update!(status: :handled) + moderation_reports.pending.update!(status: :handled, updater: updater) end def quoted_response diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index b87892bc9..5bd66a95b 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -162,7 +162,7 @@ class ForumPost < ApplicationRecord def handle_reports_on_deletion return unless moderation_reports.pending.present? && is_deleted_change == [false, true] - moderation_reports.pending.update!(status: :handled) + moderation_reports.pending.update!(status: :handled, updater: updater) end def async_send_discord_notification diff --git a/app/models/mod_action.rb b/app/models/mod_action.rb index d1090da0b..a305e665f 100644 --- a/app/models/mod_action.rb +++ b/app/models/mod_action.rb @@ -52,6 +52,8 @@ class ModAction < ApplicationRecord forum_topic_lock: 206, forum_post_update: 101, forum_post_delete: 102, + moderation_report_handled: 306, + moderation_report_rejected: 307, tag_alias_create: 120, tag_alias_update: 121, # XXX unused tag_alias_delete: 122, diff --git a/app/models/moderation_report.rb b/app/models/moderation_report.rb index 69cd54bfa..bb6191dde 100644 --- a/app/models/moderation_report.rb +++ b/app/models/moderation_report.rb @@ -3,6 +3,8 @@ class ModerationReport < ApplicationRecord MODEL_TYPES = %w[Dmail Comment ForumPost] + attr_accessor :updater + belongs_to :model, polymorphic: true belongs_to :creator, class_name: "User" @@ -13,6 +15,7 @@ class ModerationReport < ApplicationRecord after_create :create_forum_post! after_create :autoban_reported_user after_save :notify_reporter + after_save :create_modaction scope :dmail, -> { where(model_type: "Dmail") } scope :comment, -> { where(model_type: "Comment") } @@ -89,6 +92,16 @@ class ModerationReport < ApplicationRecord EOS end + def create_modaction + return unless saved_change_to_status? && status != :pending + + if handled? + ModAction.log("handled modreport ##{id}", :moderation_report_handled, updater) + elsif rejected? + ModAction.log("rejected modreport ##{id}", :moderation_report_rejected, updater) + end + end + def reported_user case model when Comment, ForumPost diff --git a/test/functional/comments_controller_test.rb b/test/functional/comments_controller_test.rb index b54060c39..82a5d64c9 100644 --- a/test/functional/comments_controller_test.rb +++ b/test/functional/comments_controller_test.rb @@ -254,6 +254,7 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest assert_equal(true, @comment.reload.is_deleted) assert_equal(true, report1.reload.handled?) assert_equal(true, report2.reload.rejected?) + assert_equal(1, ModAction.moderation_report_handled.where(creator: @mod).count) end end diff --git a/test/functional/forum_posts_controller_test.rb b/test/functional/forum_posts_controller_test.rb index fc3d4b370..aa2e284f9 100644 --- a/test/functional/forum_posts_controller_test.rb +++ b/test/functional/forum_posts_controller_test.rb @@ -239,6 +239,7 @@ class ForumPostsControllerTest < ActionDispatch::IntegrationTest assert_equal(true, @forum_post.reload.is_deleted?) assert_equal(true, report1.reload.handled?) assert_equal(true, report2.reload.rejected?) + assert_equal(1, ModAction.moderation_report_handled.where(creator: @mod).count) end end diff --git a/test/functional/moderation_reports_controller_test.rb b/test/functional/moderation_reports_controller_test.rb index 846c5d428..509cda989 100644 --- a/test/functional/moderation_reports_controller_test.rb +++ b/test/functional/moderation_reports_controller_test.rb @@ -120,6 +120,7 @@ class ModerationReportsControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_equal("handled", report.reload.status) assert_equal(true, @user.dmails.received.exists?(from: User.system, title: "Thank you for reporting comment ##{@comment.id}")) + assert_equal(true, ModAction.moderation_report_handled.where(creator: @mod).exists?) end should "allow a moderator to mark a moderation report as rejected" do @@ -129,6 +130,7 @@ class ModerationReportsControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_equal("rejected", report.reload.status) assert_equal(false, @user.dmails.received.exists?(from: User.system)) + assert_equal(true, ModAction.moderation_report_rejected.where(creator: @mod).exists?) end end end