2014-04-04 02:34:21 +08:00
|
|
|
require_dependency 'flag_query'
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
|
|
|
|
class PendingFlagsReminder < Jobs::Scheduled
|
|
|
|
|
2017-04-20 04:16:27 +08:00
|
|
|
every 1.hour
|
2014-04-04 02:34:21 +08:00
|
|
|
|
|
|
|
def execute(args)
|
2017-05-23 03:18:13 +08:00
|
|
|
if SiteSetting.notify_about_flags_after > 0
|
|
|
|
|
|
|
|
flagged_posts_count = PostAction.flagged_posts_count
|
|
|
|
|
|
|
|
return unless flagged_posts_count > 0
|
|
|
|
|
|
|
|
flag_ids = pending_flag_ids
|
2017-04-20 04:16:27 +08:00
|
|
|
|
2017-05-23 03:18:13 +08:00
|
|
|
if flag_ids.size > 0 && last_notified_id.to_i < flag_ids.max
|
|
|
|
|
|
|
|
usernames = active_moderator_usernames
|
|
|
|
mentions = usernames.size > 0 ? "@#{usernames.join(', @')} " : ""
|
|
|
|
|
|
|
|
PostCreator.create(
|
|
|
|
Discourse.system_user,
|
|
|
|
target_group_names: Group[:moderators].name,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
subtype: TopicSubtype.system_message,
|
2017-07-28 09:20:09 +08:00
|
|
|
title: I18n.t('flags_reminder.subject_template', count: flagged_posts_count),
|
|
|
|
raw: mentions + I18n.t('flags_reminder.flags_were_submitted', count: SiteSetting.notify_about_flags_after)
|
2017-05-23 03:18:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
self.last_notified_id = flag_ids.max
|
|
|
|
end
|
|
|
|
end
|
2017-04-20 04:16:27 +08:00
|
|
|
end
|
|
|
|
|
2017-05-23 03:18:13 +08:00
|
|
|
def pending_flag_ids
|
2017-09-14 02:43:36 +08:00
|
|
|
FlagQuery.flagged_post_actions(filter: 'active')
|
2017-04-20 04:16:27 +08:00
|
|
|
.where('post_actions.created_at < ?', SiteSetting.notify_about_flags_after.to_i.hours.ago)
|
|
|
|
.pluck(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_notified_id
|
2017-04-21 03:26:20 +08:00
|
|
|
$redis.get(self.class.last_notified_key)&.to_i
|
2017-04-20 04:16:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def last_notified_id=(arg)
|
|
|
|
$redis.set(self.class.last_notified_key, arg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.last_notified_key
|
2017-05-23 03:18:13 +08:00
|
|
|
"last_notified_pending_flag_id".freeze
|
2017-04-20 04:16:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def active_moderator_usernames
|
2017-05-23 03:18:13 +08:00
|
|
|
User.where(moderator: true)
|
2017-07-28 09:20:09 +08:00
|
|
|
.human_users
|
|
|
|
.order('last_seen_at DESC')
|
|
|
|
.limit(3)
|
|
|
|
.pluck(:username)
|
2014-04-04 02:34:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|