DEV: Refactor user_badge_granted DiscourseEvent logic (#17579)
Follow-up to 02ce9b8a62
This commit is contained in:
parent
9630095370
commit
78427e0797
|
@ -318,10 +318,6 @@ 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
|
||||
|
|
|
@ -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
|
||||
self.badge.trigger_badge_granted_event(self.user_id)
|
||||
self.trigger_user_badge_granted_event
|
||||
end
|
||||
|
||||
after_destroy do
|
||||
|
@ -93,8 +93,16 @@ class UserBadge < ActiveRecord::Base
|
|||
DB.exec query
|
||||
end
|
||||
|
||||
def self.trigger_user_badge_granted_event(badge_id, user_id)
|
||||
DiscourseEvent.trigger(:user_badge_granted, badge_id, user_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def trigger_user_badge_granted_event
|
||||
self.class.trigger_user_badge_granted_event(self.badge_id, self.user_id)
|
||||
end
|
||||
|
||||
def single_grant_badge?
|
||||
self.badge ? self.badge.single_grant? : true
|
||||
end
|
||||
|
|
|
@ -445,7 +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)
|
||||
UserBadge.trigger_user_badge_granted_event(badge.id, row.user_id)
|
||||
|
||||
DB.exec(
|
||||
"UPDATE user_badges SET notification_id = :notification_id WHERE id = :id",
|
||||
|
|
|
@ -234,17 +234,4 @@ 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
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
describe UserBadge do
|
||||
fab!(:badge) { Fabricate(:badge) }
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
|
||||
context 'validations' do
|
||||
fab!(:badge) { Fabricate(:badge) }
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
let(:subject) { BadgeGranter.grant(badge, user) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:badge_id) }
|
||||
|
@ -14,6 +14,18 @@ describe UserBadge do
|
|||
it { is_expected.to validate_uniqueness_of(:badge_id).scoped_to(:user_id) }
|
||||
end
|
||||
|
||||
describe '#save' do
|
||||
it "triggers the 'user_badge_granted' DiscourseEvent" do
|
||||
user_badge = UserBadge.new(badge: badge, user: user, granted_at: Time.zone.now, granted_by: Discourse.system_user)
|
||||
|
||||
event = DiscourseEvent.track(:user_badge_granted, args: [badge.id, user.id]) do
|
||||
user_badge.save!
|
||||
end
|
||||
|
||||
expect(event).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
describe "featured rank" do
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
fab!(:user_badge_tl1) { UserBadge.create!(badge_id: Badge::BasicUser, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
|
||||
|
|
|
@ -482,14 +482,17 @@ describe BadgeGranter do
|
|||
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
|
||||
it "triggers the 'user_badge_granted' DiscourseEvent per badge when badges are backfilled" 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
|
||||
events = DiscourseEvent.track_events(:user_badge_granted) do
|
||||
BadgeGranter.process_queue!
|
||||
end
|
||||
|
||||
BadgeGranter.process_queue!
|
||||
expect(events.length).to eq(2)
|
||||
expect(events[0][:params]).to eq([Badge::FirstLike, liker.id])
|
||||
expect(events[1][:params]).to eq([Badge::Welcome, user.id])
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -9,16 +9,25 @@ module DiscourseEvent::TestHelper
|
|||
end
|
||||
end
|
||||
|
||||
def track_events
|
||||
def track_events(event_name = nil, args: nil)
|
||||
@events_trigger = events_trigger = []
|
||||
yield
|
||||
@events_trigger = nil
|
||||
|
||||
if event_name
|
||||
events_trigger = events_trigger.filter do |event|
|
||||
next if event[:event_name] != event_name
|
||||
next if args && event[:params] != args
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
events_trigger
|
||||
end
|
||||
|
||||
def track(event_type)
|
||||
events = track_events { yield }
|
||||
events.find { |e| e[:event_name] == event_type }
|
||||
def track(event_name, args: nil)
|
||||
events = track_events(event_name, args: args) { yield }
|
||||
events.first
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue