From 564d242832fcbec0741f54af0acab1927b419fc6 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 6 Jun 2013 18:46:21 -0400 Subject: [PATCH] Notify moderators when someone is automatically blocked because of spam flags --- app/services/spam_rules_enforcer.rb | 17 +++++++++++++++++ config/locales/server.en.yml | 7 +++++++ spec/integration/spam_rules_spec.rb | 10 ++++++---- spec/services/spam_rules_enforcer_spec.rb | 8 +++++++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/services/spam_rules_enforcer.rb b/app/services/spam_rules_enforcer.rb index d1fa66bcc18..df6621312e0 100644 --- a/app/services/spam_rules_enforcer.rb +++ b/app/services/spam_rules_enforcer.rb @@ -2,6 +2,8 @@ # receive, their trust level, etc. class SpamRulesEnforcer + include Rails.application.routes.url_helpers + # The exclamation point means that this method may make big changes to posts and the user. def self.enforce!(user) SpamRulesEnforcer.new(user).enforce! @@ -51,6 +53,7 @@ class SpamRulesEnforcer def punish_user Post.update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", Post.hidden_reasons[:new_user_spam_threshold_reached]], user_id: @user.id) SystemMessage.create(@user, :too_many_spam_flags) + notify_moderators @user.blocked = true @user.save end @@ -61,4 +64,18 @@ class SpamRulesEnforcer @user.save end + + private + + def notify_moderators + title = I18n.t("system_messages.user_automatically_blocked.subject_template", {username: @user.username}) + raw_body = I18n.t("system_messages.user_automatically_blocked.text_body_template", {username: @user.username, blocked_user_url: admin_user_path(@user.username)}) + PostCreator.create( Discourse.system_user, + target_group_names: [Group[:moderators].name], + archetype: Archetype.private_message, + subtype: TopicSubtype.system_message, + title: title, + raw: raw_body ) + end + end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index fd853fbd1a4..dbd2ad5110d 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -861,6 +861,13 @@ en: For additional guidance, please refer to our [FAQ](%{base_url}/faq). + user_automatically_blocked: + subject_template: "User %{username} was automatically blocked" + text_body_template: | + This is an automated message to inform you that [%{username}](%{blocked_user_url}) has been automatically blocked because too many people submitted flags against %{username}'s post(s). + + Please [review the flags](/admin/flags). If %{username} should not be blocked from posting, click the unblock button on [the admin user page](%{blocked_user_url}). + unblocked: subject_template: "Account unblocked" text_body_template: | diff --git a/spec/integration/spam_rules_spec.rb b/spec/integration/spam_rules_spec.rb index 28350931015..ceaa5794bfd 100644 --- a/spec/integration/spam_rules_spec.rb +++ b/spec/integration/spam_rules_spec.rb @@ -10,9 +10,10 @@ describe PostAction do SiteSetting.num_users_to_block_new_user = 2 end - Given!(:admin) { Fabricate(:admin) } # needed to send a system message - Given(:user1) { Fabricate(:user) } - Given(:user2) { Fabricate(:user) } + Given!(:admin) { Fabricate(:admin) } # needed to send a system message + Given!(:moderator) { Fabricate(:moderator) } + Given(:user1) { Fabricate(:user) } + Given(:user2) { Fabricate(:user) } context 'spammer is a new user' do Given(:spammer) { Fabricate(:user, trust_level: TrustLevel.levels[:newuser]) } @@ -33,6 +34,7 @@ describe PostAction do context 'one spam post is flagged enough times by enough users' do Given!(:another_topic) { Fabricate(:topic) } Given!(:private_messages_count) { spammer.private_topics_count } + Given!(:mod_pm_count) { moderator.private_topics_count } When { PostAction.act(user2, spam_post, PostActionType.types[:spam]) } @@ -48,7 +50,7 @@ describe PostAction do # The following cases describe when a staff user takes some action, but the user # still won't be able to make posts. - # A staff user needs to clear the spam flag from the user record. + # A staff user needs to clear the blocked flag from the user record. context "a post's flags are cleared" do When { PostAction.clear_flags!(spam_post, admin); spammer.reload } diff --git a/spec/services/spam_rules_enforcer_spec.rb b/spec/services/spam_rules_enforcer_spec.rb index 29f691990e0..b62f6488761 100644 --- a/spec/services/spam_rules_enforcer_spec.rb +++ b/spec/services/spam_rules_enforcer_spec.rb @@ -117,8 +117,14 @@ describe SpamRulesEnforcer do expect(Guardian.new(user).can_create_post?(nil)).to be_false end - it 'sends a system message to the user' do + it 'sends private messages to the user and to moderators' do SystemMessage.expects(:create).with(user, anything, anything) + moderator = Fabricate(:moderator) + PostCreator.expects(:create).with do |from_user, opts| + from_user.id == admin.id && + opts[:target_group_names] && opts[:target_group_names].include?(Group[:moderators].name) && + opts[:archetype] == Archetype.private_message + end.returns(stub_everything) subject.punish_user end