mirror of
https://github.com/discourse/discourse.git
synced 2026-03-05 15:27:34 +08:00
When a user is auto-silenced because they receive enough spam flag, we notify moderators (given the notify_mods_when_user_silenced is enabled). However, when a user is auto-silenced for other reasons, e.g. typing too fast, we don't send this notification. This commit: - Lifts the notification logic into a new UserSilencer#auto_silence method, which delegates 99% of its work to UserSilencer#silence. - Uses this new method in places where users are auto-silenced.
67 lines
1.8 KiB
Ruby
67 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class SpamRule::AutoSilence
|
|
attr_reader :group_message
|
|
|
|
def initialize(user, post = nil)
|
|
@user = user
|
|
@post = post
|
|
end
|
|
|
|
def perform
|
|
I18n.with_locale(SiteSetting.default_locale) { silence_user if should_autosilence? }
|
|
end
|
|
|
|
def self.prevent_posting?(user)
|
|
user.blank? || user.silenced? || new(user).should_autosilence?
|
|
end
|
|
|
|
def should_autosilence?
|
|
return false if @user.blank?
|
|
return false if @user.staged?
|
|
return false if @user.has_trust_level?(TrustLevel[1])
|
|
|
|
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
|
|
end
|
|
|
|
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]
|
|
end
|
|
|
|
def flagged_post_ids
|
|
Post
|
|
.where(user_id: @user.id)
|
|
.where("spam_count > 0 OR off_topic_count > 0 OR inappropriate_count > 0")
|
|
.pluck(:id)
|
|
end
|
|
|
|
def silence_user
|
|
UserSilencer.auto_silence(
|
|
@user,
|
|
Discourse.system_user,
|
|
message: :too_many_spam_flags,
|
|
post_id: @post&.id,
|
|
)
|
|
end
|
|
end
|