0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-07 13:19:19 +08:00
discourse/app/jobs/scheduled/enqueue_suspect_users.rb
Krzysztof Kotlarek 33d0292170
FIX: Exclude suspended users from suspect users review queue (#37796)
What is the problem?

The `approve_suspect_users` feature flags new accounts as suspect if
they have a bio/website but minimal reading activity. The
`Jobs::EnqueueSuspectUsers` scheduled job runs every 2 hours and targets
accounts that are at least 1 day old. If an admin suspends a spammy user
before that job runs, the suspended user still gets added to the review
queue, creating unnecessary noise for moderators reviewing an
already-handled case.

What is the solution?

Added the `User.not_suspended` scope to the query in
`Jobs::EnqueueSuspectUsers` so that suspended users are excluded. Users
who have already been suspended by an admin have been dealt with and
should not appear in the suspect users review queue.
2026-02-16 10:46:52 +08:00

67 lines
2 KiB
Ruby
Vendored

# frozen_string_literal: true
module Jobs
class EnqueueSuspectUsers < ::Jobs::Scheduled
every 2.hours
def execute(_args)
return unless SiteSetting.approve_suspect_users
return if SiteSetting.must_approve_users
users =
User
.distinct
.activated
.human_users
.not_suspended
.where(approved: false)
.joins(:user_profile, :user_stat)
.where("users.created_at <= ? AND users.created_at >= ?", 1.day.ago, 6.months.ago)
.where("LENGTH(COALESCE(user_profiles.bio_raw, user_profiles.website, '')) > 0")
.where(
"user_stats.posts_read_count <= 1 OR user_stats.topics_entered <= 1 OR user_stats.time_read < ?",
1.minute.to_i,
)
.joins(
"LEFT OUTER JOIN reviewables r ON r.target_id = users.id AND r.target_type = 'User'",
)
.where("r.id IS NULL")
.joins(<<~SQL)
LEFT OUTER JOIN (
SELECT user_id
FROM user_custom_fields
WHERE user_custom_fields.name = 'import_id'
) AS ucf ON ucf.user_id = users.id
SQL
.where("ucf.user_id IS NULL")
.limit(10)
users.each do |user|
user_profile = user.user_profile
reviewable =
ReviewableUser.needs_review!(
target: user,
created_by: Discourse.system_user,
reviewable_by_moderator: true,
payload: {
username: user.username,
name: user.name,
email: user.email,
bio: user_profile.bio_raw,
website: user_profile.website,
},
)
if reviewable.created_new
reviewable.add_score(
Discourse.system_user,
ReviewableScore.types[:needs_approval],
reason: :suspect_user,
force_review: true,
)
end
end
end
end
end