FIX: badge backfilling triggers :user_badge_granted DiscourseEvent (#17514)

This commit is contained in:
Joffrey JAFFEUX 2022-07-20 03:33:07 +02:00 committed by GitHub
parent 4e507cace5
commit 02ce9b8a62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 1 deletions

View File

@ -318,6 +318,10 @@ class Badge < ActiveRecord::Base
id == Welcome || (badge_grouping_id == BadgeGrouping::GettingStarted && id != NewUserOfTheMonth)
end
def trigger_badge_granted_event(user_id)
DiscourseEvent.trigger(:user_badge_granted, self.id, user_id)
end
protected
def ensure_not_system

View File

@ -41,7 +41,7 @@ class UserBadge < ActiveRecord::Base
Badge.increment_counter 'grant_count', self.badge_id
UserStat.update_distinct_badge_count self.user_id
UserBadge.update_featured_ranks! self.user_id
DiscourseEvent.trigger(:user_badge_granted, self.badge_id, self.user_id)
self.badge.trigger_badge_granted_event(self.user_id)
end
after_destroy do

View File

@ -445,6 +445,7 @@ class BadgeGranter
next if row.staff && badge.awarded_for_trust_level?
notification = send_notification(row.user_id, row.username, row.locale, badge)
badge.trigger_badge_granted_event(row.user_id)
DB.exec(
"UPDATE user_badges SET notification_id = :notification_id WHERE id = :id",

View File

@ -234,4 +234,17 @@ describe Badge do
expect(regular_badge.allow_title).to eq(false)
end
end
context "#trigger_badge_granted_event" do
it "triggers a 'user_badge_granted' event" do
badge = Fabricate(:badge)
user = Fabricate(:user)
event = DiscourseEvent.track(:user_badge_granted) do
badge.trigger_badge_granted_event(user.id)
end
expect(event[:event_name]).to eq(:user_badge_granted)
expect(event[:params]).to eq([badge.id, user.id])
end
end
end

View File

@ -481,6 +481,16 @@ describe BadgeGranter do
BadgeGranter.backfill(Badge.find(Badge::GreatPost))
expect(UserBadge.find_by(user_id: user.id, badge_id: Badge::GreatPost)).to eq(nil)
end
it 'calls user_badge_granted on backfilled badge' do
post = create_post(user: user)
action = PostActionCreator.like(liker, post).post_action
Badge.any_instance.expects(:trigger_badge_granted_event).with(user.id).once
Badge.any_instance.expects(:trigger_badge_granted_event).with(liker.id).once
BadgeGranter.process_queue!
end
end
context 'notification locales' do