FIX: don't send repeat notifications to moderators about the same pending approval users

This commit is contained in:
Neil Lalonde 2015-11-20 15:32:08 -05:00
parent e25ee74209
commit 5597957cc6
2 changed files with 27 additions and 5 deletions

View file

@ -7,7 +7,13 @@ module Jobs
def execute(args)
if SiteSetting.must_approve_users
count = AdminUserIndexQuery.new({query: 'pending'}).find_users_query.count
query = AdminUserIndexQuery.new({query: 'pending'}).find_users_query # default order is: users.created_at DESC
newest_username = query.limit(1).pluck(:username).first
return true if newest_username == previous_newest_username # already notified
count = query.count
if count > 0
target_usernames = Group[:moderators].users.map do |u|
u.id > 0 && u.notifications.joins(:topic)
@ -26,9 +32,25 @@ module Jobs
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})
)
self.previous_newest_username = newest_username
end
end
end
true
end
def previous_newest_username
$redis.get previous_newest_username_cache_key
end
def previous_newest_username=(username)
$redis.setex previous_newest_username_cache_key, 7.days, username
end
def previous_newest_username_cache_key
"pending-users-reminder:newest-username".freeze
end
end

View file

@ -4,19 +4,19 @@ describe Jobs::PendingUsersReminder do
context 'must_approve_users is true' do
before do
SiteSetting.stubs(:must_approve_users).returns(true)
SiteSetting.must_approve_users = true
Jobs::PendingUsersReminder.any_instance.stubs(:previous_newest_username).returns(nil)
end
it "doesn't send a message to anyone when there are no pending users" do
AdminUserIndexQuery.any_instance.stubs(:find_users_query).returns(stub_everything(count: 0))
PostCreator.expects(:create).never
Jobs::PendingUsersReminder.new.execute({})
end
it "sends a message when there are pending users" do
Fabricate(:moderator)
Fabricate(:moderator, approved: true, approved_by_id: -1, approved_at: 1.week.ago)
Fabricate(:user)
Group.refresh_automatic_group!(:moderators)
AdminUserIndexQuery.any_instance.stubs(:find_users_query).returns(stub_everything(count: 1))
PostCreator.expects(:create).once
Jobs::PendingUsersReminder.new.execute({})
end