diff --git a/app/jobs/scheduled/destroy_old_hidden_posts.rb b/app/jobs/scheduled/destroy_old_hidden_posts.rb new file mode 100644 index 00000000000..8cb291d9ed5 --- /dev/null +++ b/app/jobs/scheduled/destroy_old_hidden_posts.rb @@ -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 diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 1a4553d8959..3eb3160a7e7 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -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 diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ccde610c682..4c14b848c5c 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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" diff --git a/config/site_settings.yml b/config/site_settings.yml index c8c40e0de25..8bcf1441904 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -379,6 +379,7 @@ posting: autohighlight_all_code: client: true default: false + delete_old_hidden_posts: true email: email_time_window_mins: 10 diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index 1719f926752..783242c44fa 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -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) diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index 607c915d55a..b25baff2344 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -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