2017-11-11 01:18:08 +08:00
|
|
|
class SpamRule::AutoSilence
|
2013-10-26 01:25:02 +08:00
|
|
|
|
2019-02-08 02:46:05 +08:00
|
|
|
attr_reader :group_message
|
2013-10-26 01:25:02 +08:00
|
|
|
|
2019-02-08 02:46:05 +08:00
|
|
|
def initialize(user, post = nil)
|
|
|
|
@user = user
|
|
|
|
@post = post
|
2013-10-26 01:25:02 +08:00
|
|
|
end
|
|
|
|
|
2019-02-08 02:46:05 +08:00
|
|
|
def perform
|
|
|
|
I18n.with_locale(SiteSetting.default_locale) do
|
|
|
|
silence_user if should_autosilence?
|
|
|
|
end
|
2013-10-26 01:25:02 +08:00
|
|
|
end
|
|
|
|
|
2019-02-08 02:46:05 +08:00
|
|
|
def self.prevent_posting?(user)
|
|
|
|
user.blank? || user.silenced? || new(user).should_autosilence?
|
2013-10-26 01:25:02 +08:00
|
|
|
end
|
|
|
|
|
2019-02-08 02:46:05 +08:00
|
|
|
def should_autosilence?
|
|
|
|
return false if @user.blank?
|
2016-06-15 22:51:26 +08:00
|
|
|
return false if @user.staged?
|
|
|
|
return false if @user.has_trust_level?(TrustLevel[1])
|
|
|
|
|
2019-02-08 02:46:05 +08:00
|
|
|
if SiteSetting.num_spam_flags_to_silence_new_user > 0 &&
|
|
|
|
SiteSetting.num_users_to_silence_new_user > 0 &&
|
|
|
|
num_spam_flags_against_user >= SiteSetting.num_spam_flags_to_silence_new_user &&
|
|
|
|
num_users_who_flagged_spam_against_user >= SiteSetting.num_users_to_silence_new_user
|
2016-06-15 22:51:26 +08:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-02-08 02:46:05 +08:00
|
|
|
if SiteSetting.num_tl3_flags_to_silence_new_user > 0 &&
|
|
|
|
SiteSetting.num_tl3_users_to_silence_new_user > 0 &&
|
|
|
|
num_tl3_flags_against_user >= SiteSetting.num_tl3_flags_to_silence_new_user &&
|
|
|
|
num_tl3_users_who_flagged >= SiteSetting.num_tl3_users_to_silence_new_user
|
2016-06-15 22:51:26 +08:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2013-10-26 01:25:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def num_spam_flags_against_user
|
|
|
|
Post.where(user_id: @user.id).sum(:spam_count)
|
|
|
|
end
|
|
|
|
|
|
|
|
def num_users_who_flagged_spam_against_user
|
|
|
|
post_ids = Post.where('user_id = ? and spam_count > 0', @user.id).pluck(:id)
|
|
|
|
return 0 if post_ids.empty?
|
2017-08-31 12:06:56 +08:00
|
|
|
PostAction.spam_flags.where(post_id: post_ids).pluck(:user_id).uniq.size
|
2013-10-26 01:25:02 +08:00
|
|
|
end
|
|
|
|
|
2016-06-15 22:51:26 +08:00
|
|
|
def num_tl3_flags_against_user
|
|
|
|
if flagged_post_ids.empty?
|
|
|
|
0
|
|
|
|
else
|
|
|
|
PostAction.where(post_id: flagged_post_ids).joins(:user).where('users.trust_level >= ?', 3).count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def num_tl3_users_who_flagged
|
|
|
|
if flagged_post_ids.empty?
|
|
|
|
0
|
|
|
|
else
|
|
|
|
PostAction.where(post_id: flagged_post_ids).joins(:user).where('users.trust_level >= ?', 3).pluck(:user_id).uniq.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def flagged_post_ids
|
|
|
|
Post.where(user_id: @user.id)
|
2017-10-18 01:31:45 +08:00
|
|
|
.where('spam_count > 0 OR off_topic_count > 0 OR inappropriate_count > 0')
|
2017-07-28 09:20:09 +08:00
|
|
|
.pluck(:id)
|
2016-06-15 22:51:26 +08:00
|
|
|
end
|
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
def silence_user
|
2013-10-26 01:25:02 +08:00
|
|
|
Post.transaction do
|
2019-02-08 02:46:05 +08:00
|
|
|
|
|
|
|
silencer = UserSilencer.new(
|
|
|
|
@user,
|
|
|
|
Discourse.system_user,
|
|
|
|
message: :too_many_spam_flags,
|
|
|
|
post_id: @post&.id
|
|
|
|
)
|
|
|
|
|
|
|
|
if silencer.silence && SiteSetting.notify_mods_when_user_silenced
|
|
|
|
@group_message = GroupMessage.create(Group[:moderators].name, :user_automatically_silenced, user: @user, limit_once_per: false)
|
2013-10-26 01:25:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-04-25 01:10:43 +08:00
|
|
|
end
|