2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-26 15:15:59 -05:00
|
|
|
module Jobs
|
2019-10-01 23:01:53 -05:00
|
|
|
class PendingUsersReminder < ::Jobs::Scheduled
|
2021-06-24 08:02:56 -05:00
|
|
|
every 5.minutes
|
2013-08-26 15:15:59 -05:00
|
|
|
|
|
|
|
def execute(args)
|
2021-06-24 08:02:56 -05:00
|
|
|
if SiteSetting.must_approve_users && SiteSetting.pending_users_reminder_delay_minutes >= 0
|
2023-01-09 06:20:10 -06:00
|
|
|
query = AdminUserIndexQuery.new(query: "pending", stats: false).find_users_query # default order is: users.created_at DESC
|
2021-06-24 08:02:56 -05:00
|
|
|
if SiteSetting.pending_users_reminder_delay_minutes > 0
|
2023-01-09 06:20:10 -06:00
|
|
|
query =
|
|
|
|
query.where(
|
|
|
|
"users.created_at < ?",
|
|
|
|
SiteSetting.pending_users_reminder_delay_minutes.minutes.ago,
|
|
|
|
)
|
2015-11-20 15:05:57 -06:00
|
|
|
end
|
|
|
|
|
2017-07-31 17:27:29 -05:00
|
|
|
newest_username = query.limit(1).select(:username).first&.username
|
2015-11-20 14:32:08 -06:00
|
|
|
|
|
|
|
return true if newest_username == previous_newest_username # already notified
|
|
|
|
|
|
|
|
count = query.count
|
|
|
|
|
2013-08-26 15:15:59 -05:00
|
|
|
if count > 0
|
2023-01-09 06:20:10 -06:00
|
|
|
target_usernames =
|
|
|
|
Group[:moderators]
|
|
|
|
.users
|
|
|
|
.map do |user|
|
|
|
|
next if user.bot?
|
|
|
|
|
|
|
|
unseen_count =
|
|
|
|
user
|
|
|
|
.notifications
|
|
|
|
.joins(:topic)
|
|
|
|
.where("notifications.id > ?", user.seen_notification_id)
|
|
|
|
.where("notifications.read = false")
|
|
|
|
.where("topics.subtype = ?", TopicSubtype.pending_users_reminder)
|
|
|
|
.count
|
|
|
|
|
|
|
|
unseen_count == 0 ? user.username : nil
|
|
|
|
end
|
|
|
|
.compact
|
2014-07-16 16:53:44 -05:00
|
|
|
|
|
|
|
unless target_usernames.empty?
|
|
|
|
PostCreator.create(
|
|
|
|
Discourse.system_user,
|
|
|
|
target_usernames: target_usernames,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
subtype: TopicSubtype.pending_users_reminder,
|
2023-01-09 06:20:10 -06:00
|
|
|
title:
|
|
|
|
I18n.t("system_messages.pending_users_reminder.subject_template", count: count),
|
|
|
|
raw:
|
|
|
|
I18n.t(
|
|
|
|
"system_messages.pending_users_reminder.text_body_template",
|
|
|
|
count: count,
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
),
|
2014-07-16 16:53:44 -05:00
|
|
|
)
|
2015-11-20 14:32:08 -06:00
|
|
|
|
|
|
|
self.previous_newest_username = newest_username
|
2014-07-16 16:53:44 -05:00
|
|
|
end
|
2013-08-26 15:15:59 -05:00
|
|
|
end
|
|
|
|
end
|
2015-11-20 14:32:08 -06:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_newest_username
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.get previous_newest_username_cache_key
|
2015-11-20 14:32:08 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous_newest_username=(username)
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.setex previous_newest_username_cache_key, 7.days, username
|
2015-11-20 14:32:08 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous_newest_username_cache_key
|
2020-04-30 01:48:34 -05:00
|
|
|
"pending-users-reminder:newest-username"
|
2013-08-26 15:15:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|