2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-10 11:18:08 -06:00
|
|
|
class SpamRule::AutoSilence
|
2019-02-07 12:46:05 -06:00
|
|
|
attr_reader :group_message
|
2013-10-25 12:25:02 -05:00
|
|
|
|
2019-02-07 12:46:05 -06:00
|
|
|
def initialize(user, post = nil)
|
|
|
|
@user = user
|
|
|
|
@post = post
|
2013-10-25 12:25:02 -05:00
|
|
|
end
|
|
|
|
|
2019-02-07 12:46:05 -06:00
|
|
|
def perform
|
|
|
|
I18n.with_locale(SiteSetting.default_locale) { silence_user if should_autosilence? }
|
2013-10-25 12:25:02 -05:00
|
|
|
end
|
|
|
|
|
2019-02-07 12:46:05 -06:00
|
|
|
def self.prevent_posting?(user)
|
|
|
|
user.blank? || user.silenced? || new(user).should_autosilence?
|
2013-10-25 12:25:02 -05:00
|
|
|
end
|
|
|
|
|
2019-02-07 12:46:05 -06:00
|
|
|
def should_autosilence?
|
|
|
|
return false if @user.blank?
|
2016-06-15 09:51:26 -05:00
|
|
|
return false if @user.staged?
|
|
|
|
return false if @user.has_trust_level?(TrustLevel[1])
|
|
|
|
|
2019-05-24 13:13:03 -05:00
|
|
|
SiteSetting.num_users_to_silence_new_user > 0 &&
|
|
|
|
user_spam_stats.total_spam_score >= Reviewable.spam_score_to_silence_new_user &&
|
|
|
|
user_spam_stats.spam_user_count >= SiteSetting.num_users_to_silence_new_user
|
2013-10-25 12:25:02 -05:00
|
|
|
end
|
|
|
|
|
2019-01-03 11:03:01 -06:00
|
|
|
def user_spam_stats
|
|
|
|
return @user_spam_stats if @user_spam_stats
|
|
|
|
|
|
|
|
params = {
|
|
|
|
user_id: @user.id,
|
|
|
|
spam_type: PostActionType.types[:spam],
|
|
|
|
pending: ReviewableScore.statuses[:pending],
|
|
|
|
agreed: ReviewableScore.statuses[:agreed],
|
|
|
|
}
|
|
|
|
|
|
|
|
result = DB.query(<<~SQL, params)
|
|
|
|
SELECT COALESCE(SUM(rs.score), 0) AS total_spam_score,
|
|
|
|
COUNT(DISTINCT rs.user_id) AS spam_user_count
|
|
|
|
FROM reviewables AS r
|
|
|
|
INNER JOIN reviewable_scores AS rs ON rs.reviewable_id = r.id
|
|
|
|
WHERE r.target_created_by_id = :user_id
|
|
|
|
AND rs.reviewable_score_type = :spam_type
|
|
|
|
AND rs.status IN (:pending, :agreed)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
@user_spam_stats = result[0]
|
2016-06-15 09:51:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def flagged_post_ids
|
|
|
|
Post
|
|
|
|
.where(user_id: @user.id)
|
2017-10-17 12:31:45 -05:00
|
|
|
.where("spam_count > 0 OR off_topic_count > 0 OR inappropriate_count > 0")
|
2016-06-15 09:51:26 -05:00
|
|
|
.pluck(:id)
|
|
|
|
end
|
|
|
|
|
2017-11-10 11:18:08 -06:00
|
|
|
def silence_user
|
2013-10-25 12:25:02 -05:00
|
|
|
Post.transaction do
|
2019-02-07 12:46:05 -06: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-25 12:25:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-04-24 12:10:43 -05:00
|
|
|
end
|