Skip to content

Commit

Permalink
FEATURE: Automatically timed delete stub topics after entire topic is…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
AndrewPrigorshnev committed May 28, 2021
1 parent 47e0970 commit 74f7150
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
15 changes: 14 additions & 1 deletion app/models/post_mover.rb
Expand Up @@ -79,7 +79,7 @@ def move_posts_to(topic)
update_bookmarks

if moving_all_posts
@original_topic.update_status('closed', true, @user)
close_topic_and_schedule_deletion
end

destination_topic.reload
Expand Down Expand Up @@ -546,4 +546,17 @@ def enqueue_jobs(topic)
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
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Expand Up @@ -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)"

Expand Down
4 changes: 4 additions & 0 deletions config/site_settings.yml
Expand Up @@ -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
Expand Down
53 changes: 51 additions & 2 deletions spec/models/post_mover_spec.rb
Expand Up @@ -608,11 +608,60 @@ def create_topic_user(user, opts = {})

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
Expand Down

0 comments on commit 74f7150

Please sign in to comment.