From 74f71503249ecbfc8b4e29c34807963ddd650dfa Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Fri, 28 May 2021 17:33:10 +0400 Subject: [PATCH] FEATURE: Automatically timed delete stub topics after entire topic is merged into another topic (#13187) When a topic is fully merged into another topic we close it. Now we want also to set a timer for deleting this topic. By default, stub topics will be deleted in 7 days. Users can change this period or disable auto-deleting by setting the period to 0. --- app/models/post_mover.rb | 15 +++++++++- config/locales/server.en.yml | 1 + config/site_settings.yml | 4 +++ spec/models/post_mover_spec.rb | 53 ++++++++++++++++++++++++++++++++-- 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb index 27a352738e6..bedd483978b 100644 --- a/app/models/post_mover.rb +++ b/app/models/post_mover.rb @@ -79,7 +79,7 @@ class PostMover update_bookmarks if moving_all_posts - @original_topic.update_status('closed', true, @user) + close_topic_and_schedule_deletion end destination_topic.reload @@ -546,4 +546,17 @@ class PostMover topic_id: topic.id ) end + + def close_topic_and_schedule_deletion + @original_topic.update_status('closed', true, @user) + + days_to_deleting = SiteSetting.delete_merged_stub_topics_after_days + if days_to_deleting > 0 + @original_topic.set_or_create_timer( + TopicTimer.types[:delete], + days_to_deleting * 24, + by_user: @user + ) + end + end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 6db98692d13..e56cd63c50d 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2185,6 +2185,7 @@ en: show_create_topics_notice: "If the site has fewer than 5 public topics, show a notice asking admins to create some topics." delete_drafts_older_than_n_days: "Delete drafts older than (n) days." + delete_merged_stub_topics_after_days: "Number of days to wait before automatically deleting fully merged stub topics. Set to 0 to never delete stub topics." bootstrap_mode_min_users: "Minimum number of users required to disable bootstrap mode (set to 0 to disable)" diff --git a/config/site_settings.yml b/config/site_settings.yml index 1672d579d0e..5c2ef5daac6 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -2146,6 +2146,10 @@ uncategorized: default: 180 max: 36500 + delete_merged_stub_topics_after_days: + default: 7 + min: 0 + backup_drafts_to_pm_length: default: 0 hidden: true diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb index 2729981a399..05220dd93fa 100644 --- a/spec/models/post_mover_spec.rb +++ b/spec/models/post_mover_spec.rb @@ -608,11 +608,60 @@ describe PostMover do it "moving all posts will close the topic" do topic.expects(:add_moderator_post).twice - moved_to = topic.move_posts(user, [p1.id, p2.id, p3.id, p4.id], destination_topic_id: destination_topic.id) + posts_to_move = [p1.id, p2.id, p3.id, p4.id] + moved_to = topic.move_posts(user, posts_to_move, destination_topic_id: destination_topic.id) expect(moved_to).to be_present topic.reload - expect(topic.closed).to eq(true) + expect(topic).to be_closed + end + + it "doesn't close the topic when not all posts were moved" do + topic.expects(:add_moderator_post).once + posts_to_move = [p1.id, p2.id, p3.id] + moved_to = topic.move_posts(user, posts_to_move, destination_topic_id: destination_topic.id) + expect(moved_to).to be_present + + topic.reload + expect(topic).to_not be_closed + end + + it "schedules topic deleting when all posts were moved" do + SiteSetting.delete_merged_stub_topics_after_days = 7 + freeze_time + + topic.expects(:add_moderator_post).twice + posts_to_move = [p1.id, p2.id, p3.id, p4.id] + moved_to = topic.move_posts(user, posts_to_move, destination_topic_id: destination_topic.id) + expect(moved_to).to be_present + + timer = topic.topic_timers.find_by(status_type: TopicTimer.types[:delete]) + expect(timer).to be_present + expect(timer.execute_at).to eq_time(7.days.from_now) + end + + it "doesn't schedule topic deleting when not all posts were moved" do + SiteSetting.delete_merged_stub_topics_after_days = 7 + + topic.expects(:add_moderator_post).once + posts_to_move = [p1.id, p2.id, p3.id] + moved_to = topic.move_posts(user, posts_to_move, destination_topic_id: destination_topic.id) + expect(moved_to).to be_present + + timer = topic.topic_timers.find_by(status_type: TopicTimer.types[:delete]) + expect(timer).to be_nil + end + + it "doesn't schedule topic deleting when all posts were moved if it's disabled in settings" do + SiteSetting.delete_merged_stub_topics_after_days = 0 + + topic.expects(:add_moderator_post).twice + posts_to_move = [p1.id, p2.id, p3.id, p4.id] + moved_to = topic.move_posts(user, posts_to_move, destination_topic_id: destination_topic.id) + expect(moved_to).to be_present + + timer = topic.topic_timers.find_by(status_type: TopicTimer.types[:delete]) + expect(timer).to be_nil end it "does not try to move small action posts" do