From 72849e65decf3fcfa6c85cff321a295a98b7cecf Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Apr 2016 22:08:38 +1000 Subject: [PATCH] FIX: when granting old badges that are bronze, do not notify --- app/services/badge_granter.rb | 21 ++++++++++++--------- spec/services/badge_granter_spec.rb | 11 +++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index c61f11e8c86..a1b930ea112 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -41,15 +41,18 @@ class BadgeGranter end if SiteSetting.enable_badges? - I18n.with_locale(@user.effective_locale) do - notification = @user.notifications.create( - notification_type: Notification.types[:granted_badge], - data: { badge_id: @badge.id, - badge_name: @badge.display_name, - badge_slug: @badge.slug, - username: @user.username}.to_json - ) - user_badge.update_attributes notification_id: notification.id + + unless @badge.badge_type_id == BadgeType::Bronze && user_badge.granted_at < 2.days.ago + I18n.with_locale(@user.effective_locale) do + notification = @user.notifications.create( + notification_type: Notification.types[:granted_badge], + data: { badge_id: @badge.id, + badge_name: @badge.display_name, + badge_slug: @badge.slug, + username: @user.username}.to_json + ) + user_badge.update_attributes notification_id: notification.id + end end end end diff --git a/spec/services/badge_granter_spec.rb b/spec/services/badge_granter_spec.rb index 2eef495663b..6111be99a1c 100644 --- a/spec/services/badge_granter_spec.rb +++ b/spec/services/badge_granter_spec.rb @@ -98,6 +98,17 @@ describe BadgeGranter do describe 'grant' do + it 'allows overriding of granted_at does not notify old bronze' do + badge = Fabricate(:badge, badge_type_id: BadgeType::Bronze) + time = 1.year.ago + + user_badge = BadgeGranter.grant(badge, user, created_at: time) + + expect(user_badge.granted_at).to eq(time) + expect(Notification.where(user_id: user.id).count).to eq(0) + + end + it 'grants multiple badges' do badge = Fabricate(:badge, multiple_grant: true) user_badge = BadgeGranter.grant(badge, user)