From 8f1276b0137b8612ca7d1209ff6260ed5def8224 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 16 Aug 2020 16:56:30 -0500 Subject: [PATCH] appeals: crosspost appeals to deletion appeal thread. Crosspost pending appeals to the deletion appeal thread. We do this once per hour so we can batch together multiple appeals into one post. --- app/logical/danbooru_maintenance.rb | 1 + app/logical/post_appeal_forum_updater.rb | 26 ++++++++++++++++++++++ test/unit/danbooru_maintenance_test.rb | 28 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 app/logical/post_appeal_forum_updater.rb diff --git a/app/logical/danbooru_maintenance.rb b/app/logical/danbooru_maintenance.rb index 0d66ed948..c990dcfae 100644 --- a/app/logical/danbooru_maintenance.rb +++ b/app/logical/danbooru_maintenance.rb @@ -4,6 +4,7 @@ module DanbooruMaintenance def hourly safely { Upload.prune! } safely { PostPruner.prune! } + safely { PostAppealForumUpdater.update_forum! } safely { regenerate_post_counts! } end diff --git a/app/logical/post_appeal_forum_updater.rb b/app/logical/post_appeal_forum_updater.rb new file mode 100644 index 000000000..cbb3881c5 --- /dev/null +++ b/app/logical/post_appeal_forum_updater.rb @@ -0,0 +1,26 @@ +module PostAppealForumUpdater + APPEAL_TOPIC_TITLE = "Deletion appeal thread" + + def self.update_forum! + return if pending_appeals.empty? + + CurrentUser.scoped(User.system) do + topic = ForumTopic.order(:id).create_with(creator: User.system).find_or_create_by!(title: APPEAL_TOPIC_TITLE) + ForumPost.create!(creator: User.system, topic: topic, body: forum_post_body) + end + end + + def self.pending_appeals + PostAppeal.pending.where(created_at: (1.hour.ago..Time.zone.now)).order(post_id: :asc) + end + + def self.forum_post_body + pending_appeals.map do |appeal| + if appeal.reason.present? + "post ##{appeal.post_id}: #{appeal.reason}" + else + "post ##{appeal.post_id}" + end + end.join("\n") + end +end diff --git a/test/unit/danbooru_maintenance_test.rb b/test/unit/danbooru_maintenance_test.rb index c6f73b924..b7f24eb27 100644 --- a/test/unit/danbooru_maintenance_test.rb +++ b/test/unit/danbooru_maintenance_test.rb @@ -38,4 +38,32 @@ class DanbooruMaintenanceTest < ActiveSupport::TestCase end end end + + context "PostAppealForumUpdater" do + should "work when there are no pending appeals" do + assert_nothing_raised do + PostAppealForumUpdater.update_forum! + end + end + + should "post pending appeals to the deletion appeal thread" do + @topic = as(create(:user)) { create(:forum_topic, title: PostAppealForumUpdater::APPEAL_TOPIC_TITLE) } + @appeal1 = create(:post_appeal, reason: "test") + @appeal2 = create(:post_appeal, reason: "") + @appeal3 = create(:post_appeal, created_at: 2.hours.ago) + + PostAppealForumUpdater.update_forum! + + assert_equal(@topic.id, ForumPost.last.topic_id) + assert_equal("post ##{@appeal1.post_id}: #{@appeal1.reason}\npost ##{@appeal2.post_id}", ForumPost.last.body) + end + + should "create the deletion appeal thread if it doesn't already exist" do + @appeal = create(:post_appeal, reason: "") + PostAppealForumUpdater.update_forum! + + assert_equal(PostAppealForumUpdater::APPEAL_TOPIC_TITLE, ForumPost.last.topic.title) + assert_equal("post ##{@appeal.post_id}", ForumPost.last.body) + end + end end