Notify moderators when someone is automatically blocked because of spam flags

This commit is contained in:
Neil Lalonde 2013-06-06 18:46:21 -04:00
parent 11afa0c11b
commit 564d242832
4 changed files with 37 additions and 5 deletions

View file

@ -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

View file

@ -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: |

View file

@ -11,6 +11,7 @@ describe PostAction do
end
Given!(:admin) { Fabricate(:admin) } # needed to send a system message
Given!(:moderator) { Fabricate(:moderator) }
Given(:user1) { Fabricate(:user) }
Given(:user2) { Fabricate(:user) }
@ -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 }

View file

@ -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