Send a message to moderators when a newuser_spam_host_threshold is exceeded. Send it no more than once per day per user.

This commit is contained in:
Neil Lalonde
2013-06-10 13:17:09 -04:00
parent 0d83f373b8
commit 9b1d0baf45
7 changed files with 210 additions and 23 deletions

View File

@@ -0,0 +1,66 @@
# GroupMessage sends a private message to a group.
# It will also avoid sending the same message repeatedly, which can happen with
# notifications to moderators when spam is detected.
#
# Options:
#
# user: (User) If the message is about a user, pass the user object.
# limit_once_per: (seconds) Limit sending the given type of message once every X seconds.
# The default is 24 hours. Set to false to always send the message.
require_dependency 'post_creator'
require_dependency 'topic_subtype'
require_dependency 'discourse'
class GroupMessage
include Rails.application.routes.url_helpers
def self.create(group_name, message_type, opts={})
GroupMessage.new(group_name, message_type, opts).create
end
def initialize(group_name, message_type, opts={})
@group_name = group_name
@message_type = message_type
@opts = opts
end
def create
unless sent_recently?
post = PostCreator.create( Discourse.system_user,
target_group_names: [@group_name],
archetype: Archetype.private_message,
subtype: TopicSubtype.system_message,
title: I18n.t("system_messages.#{@message_type}.subject_template", message_params),
raw: I18n.t("system_messages.#{@message_type}.text_body_template", message_params) )
remember_message_sent
post
else
false
end
end
def message_params
@message_params ||= {
username: @opts[:user].username,
user_url: admin_user_path(@opts[:user].username)
}
end
def sent_recently?
return false if @opts[:limit_once_per] == false
$redis.get(sent_recently_key).present?
end
# default is to send no more than once every 24 hours (24 * 60 * 60 = 86,400 seconds)
def remember_message_sent
$redis.setex(sent_recently_key, @opts[:limit_once_per].try(:to_i) || 86_400, 1) unless @opts[:limit_once_per] == false
end
private
def sent_recently_key
"grpmsg:#{@group_name}:#{@message_type}:#{@opts[:user] ? @opts[:user].username : ''}"
end
end

View File

@@ -53,7 +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
GroupMessage.create(Group[:moderators].name, :user_automatically_blocked, {user: @user, limit_once_per: false})
@user.blocked = true
@user.save
end
@@ -64,18 +64,4 @@ 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