From 0113fce420bcf3bc18a38c6864150334fdefa273 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Apr 2016 13:29:48 +1000 Subject: [PATCH] FEATURE: automatically close topics with 10k+ posts FEATURE: automatically close messages with 2k+ posts Both configurable via `auto_close_messages_post_count` and `auto_close_topics_post_count` --- config/locales/server.en.yml | 8 ++++++++ config/site_settings.yml | 2 ++ lib/post_creator.rb | 22 ++++++++++++++++++++++ spec/components/post_creator_spec.rb | 26 ++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index e9fb197e787..bbb3b520531 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1281,6 +1281,8 @@ en: approve_post_count: "The amount of posts from a new or basic user that must be approved" approve_unless_trust_level: "Posts for users below this trust level must be approved" notify_about_queued_posts_after: "If there are posts that have been waiting to be reviewed for more than this many hours, an email will be sent to the contact_email. Set to 0 to disable these emails." + auto_close_messages_post_count: "Maximum number of posts allowed in a message before it is automatically closed (0 to disable)" + auto_close_topics_post_count: "Maximum number of posts allowed in a topic before it is automatically closed (0 to disable)" default_email_digest_frequency: "How often users receive digest emails by default." default_include_tl0_in_digests: "Include posts from new users in digest emails by default. Users can change this in their preferences." @@ -1391,6 +1393,12 @@ en: archived_disabled: "This topic is now unarchived. It is no longer frozen, and can be changed." closed_enabled: "This topic is now closed. New replies are no longer allowed." closed_disabled: "This topic is now opened. New replies are allowed." + autoclosed_message_max_posts: + one: "This message was automatically closed after reaching the limit of 1 post per message" + other: "This message was automatically closed after reaching the limit of %{count} posts per message" + autoclosed_topic_max_posts: + one: "This topic was automatically closed after reaching the limit of 1 post per topic" + other: "This topic was automatically closed after reaching the limit of %{count} posts per topic" autoclosed_enabled_days: one: "This topic was automatically closed after 1 day. New replies are no longer allowed." other: "This topic was automatically closed after %{count} days. New replies are no longer allowed." diff --git a/config/site_settings.yml b/config/site_settings.yml index d20c56245ec..d88950534ae 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -505,6 +505,8 @@ posting: notify_about_queued_posts_after: default: 24 min: 0 + auto_close_messages_post_count: 200 + auto_close_topics_post_count: 10000 email: email_time_window_mins: diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 62bc2a57934..6b1790b208a 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -148,6 +148,8 @@ class PostCreator BadgeGranter.queue_badge_grant(Badge::Trigger::PostRevision, post: @post) trigger_after_events(@post) + + auto_close end if @post || @spam @@ -221,6 +223,26 @@ class PostCreator DiscourseEvent.trigger(:post_created, post, @opts, @user) end + def auto_close + if @post.topic.private_message? && + !@post.topic.closed && + SiteSetting.auto_close_messages_post_count > 0 && + SiteSetting.auto_close_messages_post_count <= @post.topic.posts_count + + @post.topic.update_status(:closed, true, Discourse.system_user, + message: I18n.t('topic_statuses.autoclosed_message_max_posts', count: SiteSetting.auto_close_messages_post_count)) + + elsif !@post.topic.private_message? && + !@post.topic.closed && + SiteSetting.auto_close_topics_post_count > 0 && + SiteSetting.auto_close_topics_post_count <= @post.topic.posts_count + + @post.topic.update_status(:closed, true, Discourse.system_user, + message: I18n.t('topic_statuses.autoclosed_topic_max_posts', count: SiteSetting.auto_close_messages_post_count)) + + end + end + def transaction(&blk) Post.transaction do if new_topic? diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 8675fdf77e6..762649bc83a 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -547,6 +547,32 @@ describe PostCreator do end end + context 'auto closing' do + it 'closes private messages that have more than N posts' do + SiteSetting.auto_close_messages_post_count = 2 + + admin = Fabricate(:admin) + + post1 = create_post(archetype: Archetype.private_message, + target_usernames: [admin.username]) + + _post2 = create_post(user: post1.user, topic_id: post1.topic_id) + + post1.topic.reload + expect(post1.topic.closed).to eq(true) + end + + it 'closes topics that have more than N posts' do + SiteSetting.auto_close_topics_post_count = 2 + + post1 = create_post + _post2 = create_post(user: post1.user, topic_id: post1.topic_id) + + post1.topic.reload + expect(post1.topic.closed).to eq(true) + end + end + context 'private message to group' do let(:target_user1) { Fabricate(:coding_horror) } let(:target_user2) { Fabricate(:moderator) }