FEATURE: auto-delete any hidden posts that stay hidden for more than 30 days

This commit is contained in:
Régis Hanol 2014-09-25 19:51:00 +02:00
parent b16e6f8289
commit 69400a802f
6 changed files with 64 additions and 3 deletions

View File

@ -0,0 +1,13 @@
module Jobs
class DestroyOldHiddenPosts < Jobs::Scheduled
every 1.day
def execute(args)
return unless SiteSetting.delete_old_hidden_posts
PostDestroyer.destroy_old_hidden_posts
end
end
end

View File

@ -404,7 +404,7 @@ class PostAction < ActiveRecord::Base
reason = guess_hide_reason(old_flags)
end
Post.where(id: post.id).update_all(["hidden = true, hidden_at = CURRENT_TIMESTAMP, hidden_reason_id = COALESCE(hidden_reason_id, ?)", reason])
Post.where(id: post.id).update_all(["hidden = true, hidden_at = ?, hidden_reason_id = COALESCE(hidden_reason_id, ?)", Time.now, reason])
Topic.where("id = :topic_id AND NOT EXISTS(SELECT 1 FROM POSTS WHERE topic_id = :topic_id AND NOT hidden)", topic_id: post.topic_id).update_all(visible: false)
# inform user

View File

@ -639,6 +639,7 @@ en:
description: "A message that will be displayed at the top of all notification emails."
site_settings:
delete_old_hidden_posts: "Auto-delete any hidden posts that stay hidden for more than 30 days."
default_locale: "The default language of this Discourse instance (ISO 639-1 Code)"
allow_user_locale: "Allow users to choose their own language interface preference"
min_post_length: "Minimum allowed post length in characters"

View File

@ -379,6 +379,7 @@ posting:
autohighlight_all_code:
client: true
default: false
delete_old_hidden_posts: true
email:
email_time_window_mins: 10

View File

@ -4,6 +4,14 @@
#
class PostDestroyer
def self.destroy_old_hidden_posts
Post.where(deleted_at: nil)
.where("hidden_at < ?", 30.days.ago)
.find_each do |post|
PostDestroyer.new(Discourse.system_user, post).destroy
end
end
def self.destroy_stubs
# exclude deleted topics and posts that are actively flagged
Post.where(deleted_at: nil, user_deleted: true)

View File

@ -11,6 +11,44 @@ describe PostDestroyer do
let(:admin) { Fabricate(:admin) }
let(:post) { create_post }
describe "destroy_old_hidden_posts" do
it "destroys posts that have been hidden for 30 days" do
Fabricate(:admin)
now = Time.now
freeze_time(now - 60.days)
topic = post.topic
reply1 = create_post(topic: topic)
freeze_time(now - 40.days)
reply2 = create_post(topic: topic)
PostAction.hide_post!(reply2, PostActionType.types[:off_topic])
freeze_time(now - 20.days)
reply3 = create_post(topic: topic)
PostAction.hide_post!(reply3, PostActionType.types[:off_topic])
freeze_time(now - 10.days)
reply4 = create_post(topic: topic)
freeze_time(now)
PostDestroyer.destroy_old_hidden_posts
reply1.reload
reply2.reload
reply3.reload
reply4.reload
reply1.deleted_at.should == nil
reply2.deleted_at.should_not == nil
reply3.deleted_at.should == nil
reply4.deleted_at.should == nil
end
end
describe 'destroy_old_stubs' do
it 'destroys stubs for deleted by user posts' do
SiteSetting.stubs(:delete_removed_posts_after).returns(24)
@ -314,8 +352,8 @@ describe PostDestroyer do
PostDestroyer.new(moderator, second_post).destroy
expect(UserAction.find_by(id: bookmark.id)).should == nil
expect(UserAction.find_by(id: like.id)).should == nil
expect(UserAction.find_by(id: bookmark.id)).to be_nil
expect(UserAction.find_by(id: like.id)).to be_nil
end
end