mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 10:50:26 -06:00
Grant system badges in a background job.
This commit is contained in:
parent
b3f403952b
commit
b5eea1d79f
25
app/jobs/regular/update_badges.rb
Normal file
25
app/jobs/regular/update_badges.rb
Normal file
@ -0,0 +1,25 @@
|
||||
module Jobs
|
||||
class UpdateBadges < Jobs::Base
|
||||
|
||||
def execute(args)
|
||||
self.send(args[:action], args)
|
||||
end
|
||||
|
||||
def trust_level_change(args)
|
||||
user = User.find(args[:user_id])
|
||||
trust_level = user.trust_level
|
||||
Badge.trust_level_badge_ids.each do |badge_id|
|
||||
user_badge = UserBadge.find_by(user_id: user.id, badge_id: badge_id)
|
||||
if user_badge
|
||||
# Revoke the badge if trust level was lowered.
|
||||
BadgeGranter.revoke(user_badge) if trust_level < badge_id
|
||||
else
|
||||
# Grant the badge if trust level was increased.
|
||||
badge = Badge.find(badge_id)
|
||||
BadgeGranter.grant(badge, user) if trust_level >= badge_id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -48,22 +48,8 @@ class BadgeGranter
|
||||
end
|
||||
end
|
||||
|
||||
def self.update_badges(user, opts={})
|
||||
if opts.has_key?(:trust_level)
|
||||
# Update trust level badges.
|
||||
trust_level = opts[:trust_level]
|
||||
Badge.trust_level_badge_ids.each do |badge_id|
|
||||
user_badge = UserBadge.find_by(user_id: user.id, badge_id: badge_id)
|
||||
if user_badge
|
||||
# Revoke the badge if trust level was lowered.
|
||||
BadgeGranter.revoke(user_badge) if trust_level < badge_id
|
||||
else
|
||||
# Grant the badge if trust level was increased.
|
||||
badge = Badge.find(badge_id)
|
||||
BadgeGranter.grant(badge, user) if trust_level >= badge_id
|
||||
end
|
||||
end
|
||||
end
|
||||
def self.update_badges(args)
|
||||
Jobs.enqueue(:update_badges, args)
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -60,7 +60,7 @@ class Promotion
|
||||
@user.user_profile.recook_bio
|
||||
@user.user_profile.save!
|
||||
Group.user_trust_level_change!(@user.id, @user.trust_level)
|
||||
BadgeGranter.update_badges(@user, trust_level: @user.trust_level)
|
||||
BadgeGranter.update_badges(action: :trust_level_change, user_id: @user.id)
|
||||
end
|
||||
|
||||
true
|
||||
|
Loading…
Reference in New Issue
Block a user