discourse/app/jobs/scheduled/ignored_users_summary.rb
Tarek Khalil bd6d31c9ec
FEATURE: Add IgnoredUsersSummary daily job (#7144)
* FEATURE: Add `IgnoredUsersSummary` daily job

## Why?

This is part of the [Ability to ignore a user feature](https://meta.discourse.org/t/ability-to-ignore-a-user/110254/8).

We want to:

1. Send an automatic group PM that goes out to moderators
2. When {x} users have Ignored the same user, threshold defined by a site setting, default of 5
3. Only send this message every X days which is defined by another site setting
2019-03-14 22:51:43 +00:00

43 lines
1.5 KiB
Ruby

module Jobs
class IgnoredUsersSummary < Jobs::Scheduled
every 1.day
def execute(args)
return unless SiteSetting.ignore_user_enabled
params = {
threshold: SiteSetting.ignored_users_count_message_threshold,
gap_days: SiteSetting.ignored_users_message_gap_days,
coalesced_gap_days: SiteSetting.ignored_users_message_gap_days + 1,
}
user_ids = DB.query_single(<<~SQL, params)
SELECT ignored_user_id
FROM ignored_users
WHERE COALESCE(summarized_at, CURRENT_TIMESTAMP + ':coalesced_gap_days DAYS'::INTERVAL) - ':gap_days DAYS'::INTERVAL > CURRENT_TIMESTAMP
GROUP BY ignored_user_id
HAVING COUNT(ignored_user_id) >= :threshold
SQL
User.where(id: user_ids).find_each { |user| notify_user(user) }
end
private
def notify_user(user)
params = SystemMessage.new(user).defaults.merge(ignores_threshold: SiteSetting.ignored_users_count_message_threshold)
title = I18n.t("system_messages.ignored_users_summary.subject_template")
raw = I18n.t("system_messages.ignored_users_summary.text_body_template", params)
PostCreator.create(
Discourse.system_user,
target_group_names: Group[:moderators].name,
archetype: Archetype.private_message,
subtype: TopicSubtype.system_message,
title: title,
raw: raw,
skip_validations: true)
IgnoredUser.where(ignored_user_id: user.id).update_all(summarized_at: Time.zone.now)
end
end
end