From 6fecf5db0e21450bd36c0b1cbbdb0df76367ee7c Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 10 Mar 2020 21:23:46 -0500 Subject: [PATCH] BURs: remove old single alias/implication pruning code. * Rename TagChangeRequestPruner to BulkUpdateRequestPruner. * Remove old code for pruning individual alias / implication requests. --- app/logical/bulk_update_request_pruner.rb | 27 ++++++++ app/logical/danbooru_maintenance.rb | 4 +- app/logical/tag_change_request_pruner.rb | 45 ------------- app/models/bulk_update_request.rb | 2 +- test/unit/bulk_update_request_pruner_test.rb | 25 ++++++++ test/unit/tag_change_request_pruner_test.rb | 66 -------------------- 6 files changed, 55 insertions(+), 114 deletions(-) create mode 100644 app/logical/bulk_update_request_pruner.rb delete mode 100644 app/logical/tag_change_request_pruner.rb create mode 100644 test/unit/bulk_update_request_pruner_test.rb delete mode 100644 test/unit/tag_change_request_pruner_test.rb diff --git a/app/logical/bulk_update_request_pruner.rb b/app/logical/bulk_update_request_pruner.rb new file mode 100644 index 000000000..6d0aa7054 --- /dev/null +++ b/app/logical/bulk_update_request_pruner.rb @@ -0,0 +1,27 @@ +module BulkUpdateRequestPruner + module_function + + def warn_old + BulkUpdateRequest.old.pending.find_each do |bulk_update_request| + if bulk_update_request.forum_topic + body = "This bulk update request is pending automatic rejection in 5 days." + unless bulk_update_request.forum_topic.posts.where(creator_id: User.system.id, body: body).exists? + bulk_update_request.forum_updater.update(body) + end + end + end + end + + def reject_expired + BulkUpdateRequest.expired.pending.find_each do |bulk_update_request| + ApplicationRecord.transaction do + if bulk_update_request.forum_topic + body = "This bulk update request has been rejected because it was not approved within 60 days." + bulk_update_request.forum_updater.update(body) + end + + bulk_update_request.reject!(User.system) + end + end + end +end diff --git a/app/logical/danbooru_maintenance.rb b/app/logical/danbooru_maintenance.rb index bcd082f85..330ba8c0f 100644 --- a/app/logical/danbooru_maintenance.rb +++ b/app/logical/danbooru_maintenance.rb @@ -12,8 +12,8 @@ module DanbooruMaintenance safely { PostDisapproval.dmail_messages! } safely { regenerate_post_counts! } safely { TokenBucket.prune! } - safely { TagChangeRequestPruner.warn_all } - safely { TagChangeRequestPruner.reject_all } + safely { BulkUpdateRequestPruner.warn_old } + safely { BulkUpdateRequestPruner.reject_expired } safely { Ban.prune! } safely { ActiveRecord::Base.connection.execute("vacuum analyze") unless Rails.env.test? } end diff --git a/app/logical/tag_change_request_pruner.rb b/app/logical/tag_change_request_pruner.rb deleted file mode 100644 index c80222a22..000000000 --- a/app/logical/tag_change_request_pruner.rb +++ /dev/null @@ -1,45 +0,0 @@ -# Service to prune old unapproved tag change requests -# (including tag aliases, tag implications, and bulk -# update requests). - -class TagChangeRequestPruner - def self.warn_all - [TagAlias, TagImplication, BulkUpdateRequest].each do |model| - new.warn_old(model) - end - end - - def self.reject_all - [TagAlias, TagImplication, BulkUpdateRequest].each do |model| - new.reject_expired(model) - end - end - - def warn_old(model) - model.old.pending.find_each do |tag_change| - if tag_change.forum_topic - name = model.model_name.human.downcase - body = "This #{name} is pending automatic rejection in 5 days." - unless tag_change.forum_topic.posts.where(creator_id: User.system.id, body: body).exists? - tag_change.forum_updater.update(body) - end - end - end - end - - def reject_expired(model) - model.expired.pending.find_each do |tag_change| - ApplicationRecord.transaction do - if tag_change.forum_topic - name = model.model_name.human.downcase - body = "This #{name} has been rejected because it was not approved within 60 days." - tag_change.forum_updater.update(body) - end - - CurrentUser.as_system do - tag_change.reject! - end - end - end - end -end diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index 791d8fff1..d84f5c213 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -93,7 +93,7 @@ class BulkUpdateRequest < ApplicationRecord def reject!(rejector = User.system) transaction do - update(status: "rejected") + update!(status: "rejected") forum_updater.update("The #{bulk_update_request_link} (forum ##{forum_post.id}) has been rejected by @#{rejector.name}.") end end diff --git a/test/unit/bulk_update_request_pruner_test.rb b/test/unit/bulk_update_request_pruner_test.rb new file mode 100644 index 000000000..d6f222682 --- /dev/null +++ b/test/unit/bulk_update_request_pruner_test.rb @@ -0,0 +1,25 @@ +require 'test_helper' + +class BulkUpdateRequestPrunerTest < ActiveSupport::TestCase + context '#warn_old' do + should "update the forum topic for a bulk update request" do + forum_topic = as(create(:user)) { create(:forum_topic) } + bur = create(:bulk_update_request, status: "pending", forum_topic: forum_topic, created_at: (TagRelationship::EXPIRY_WARNING + 1).days.ago) + + BulkUpdateRequestPruner.warn_old + assert_equal("pending", bur.reload.status) + assert_match(/pending automatic rejection/, ForumPost.last.body) + end + end + + context '#reject_expired' do + should "reject the bulk update request" do + forum_topic = as(create(:user)) { create(:forum_topic) } + bur = create(:bulk_update_request, status: "pending", forum_topic: forum_topic, created_at: (TagRelationship::EXPIRY + 1).days.ago) + + BulkUpdateRequestPruner.reject_expired + assert_equal("rejected", bur.reload.status) + assert_match(/rejected because it was not approved within 60 days/, bur.forum_post.body) + end + end +end diff --git a/test/unit/tag_change_request_pruner_test.rb b/test/unit/tag_change_request_pruner_test.rb deleted file mode 100644 index d7798741d..000000000 --- a/test/unit/tag_change_request_pruner_test.rb +++ /dev/null @@ -1,66 +0,0 @@ -require 'test_helper' - -class TagChangeRequestPrunerTest < ActiveSupport::TestCase - setup do - CurrentUser.user = FactoryBot.create(:admin_user) - CurrentUser.ip_addr = "127.0.0.1" - - @forum_topic = create(:forum_topic) - @tag_alias = create(:tag_alias, forum_topic: @forum_topic) - @tag_implication = create(:tag_implication, antecedent_name: "ccc", consequent_name: "ddd", forum_topic: @forum_topic) - @bulk_update_request = create(:bulk_update_request, script: "alias eee -> fff", forum_topic: @forum_topic) - end - - teardown do - CurrentUser.user = nil - CurrentUser.ip_addr = nil - end - - subject { TagChangeRequestPruner.new } - - context '#warn_old' do - setup do - [TagAlias, TagImplication, BulkUpdateRequest].each do |model| - model.update_all(status: "pending", created_at: (TagRelationship::EXPIRY_WARNING + 1).days.ago) - end - end - - should "update the forum topic for an alias" do - ForumUpdater.any_instance.expects(:update) - subject.warn_old(TagAlias) - end - - should "update the forum topic for an implication" do - ForumUpdater.any_instance.expects(:update) - subject.warn_old(TagImplication) - end - - should "update the forum topic for a bulk update request" do - ForumUpdater.any_instance.expects(:update) - subject.warn_old(BulkUpdateRequest) - end - end - - context '#reject_expired' do - setup do - [TagAlias, TagImplication, BulkUpdateRequest].each do |model| - model.update_all(status: "pending", created_at: (TagRelationship::EXPIRY + 1).days.ago) - end - end - - should "reject the alias" do - TagAlias.any_instance.expects(:reject!) - subject.reject_expired(TagAlias) - end - - should "reject the implication" do - TagImplication.any_instance.expects(:reject!) - subject.reject_expired(TagImplication) - end - - should "reject the bulk update request" do - BulkUpdateRequest.any_instance.expects(:reject!) - subject.reject_expired(BulkUpdateRequest) - end - end -end