FIX: Don't award new user of the month in the wrong month

see: https://meta.discourse.org/t/new-user-of-the-month-badge-awarded-before-registraton-date/157347/2?u=eviltrout
This commit is contained in:
Robin Ward 2020-07-13 15:05:30 -04:00
parent c02e358146
commit a73da42691
2 changed files with 29 additions and 5 deletions

View File

@ -17,7 +17,7 @@ module Jobs
badge.id, previous_month_beginning, Time.zone.now
).exists?
scores(previous_month_beginning).each do |user_id, score|
scores(previous_month_beginning, previous_month_end).each do |user_id, score|
# Don't bother awarding to users who haven't received any likes
if score > 0.0
user = User.find(user_id)
@ -33,7 +33,7 @@ module Jobs
end
end
def scores(user_created_after_date)
def scores(min_user_created_at, max_user_created_at)
current_owners = UserBadge.where(badge_id: Badge::NewUserOfTheMonth).pluck(:user_id)
current_owners = [-1] if current_owners.blank?
@ -71,7 +71,7 @@ module Jobs
AND NOT u.moderator
AND u.suspended_at IS NULL
AND u.suspended_till IS NULL
AND u.created_at >= :min_user_created_at
AND u.created_at BETWEEN :min_user_created_at AND :max_user_created_at
AND t.archetype <> '#{Archetype.private_message}'
AND t.deleted_at IS NULL
AND p.deleted_at IS NULL
@ -83,7 +83,13 @@ module Jobs
LIMIT #{MAX_AWARDED}
SQL
Hash[*DB.query_single(sql, min_user_created_at: user_created_after_date)]
Hash[
*DB.query_single(
sql,
min_user_created_at: min_user_created_at,
max_user_created_at: max_user_created_at
)
]
end
end

View File

@ -30,6 +30,24 @@ describe Jobs::GrantNewUserOfTheMonthBadges do
expect(badges.first.granted_at.to_s).to eq('2019-12-31 23:59:59 UTC')
end
it "does not include people created after the previous month" do
freeze_time(DateTime.parse('2020-01-15 00:00 UTC'))
user = Fabricate(:user, created_at: 1.week.ago)
p = Fabricate(:post, user: user)
Fabricate(:post, user: user)
old_user = Fabricate(:user, created_at: 6.months.ago)
PostActionCreator.like(old_user, p)
old_user = Fabricate(:user, created_at: 6.months.ago)
PostActionCreator.like(old_user, p)
granter.execute({})
badges = user.user_badges.where(badge_id: Badge::NewUserOfTheMonth)
expect(badges).to be_blank
end
it "does nothing if badges are disabled" do
SiteSetting.enable_badges = false
@ -93,7 +111,7 @@ describe Jobs::GrantNewUserOfTheMonthBadges do
describe '.scores' do
def scores
granter.scores(1.month.ago)
granter.scores(1.month.ago, Time.now)
end
it "doesn't award it to accounts over a month old" do