Tweak badge granter not to try to double grant

This commit is contained in:
Sam 2014-04-14 15:58:27 +10:00
parent cfccf8b4ed
commit 0d114d7b17
1 changed files with 9 additions and 7 deletions

View File

@ -12,15 +12,17 @@ class BadgeGranter
def grant
return if @granted_by and !Guardian.new(@granted_by).can_grant_badges?(@user)
user_badge = nil
user_badge = UserBadge.find_by(badge_id: @badge.id, user_id: @user.id)
UserBadge.transaction do
user_badge = UserBadge.create!(badge: @badge, user: @user,
granted_by: @granted_by, granted_at: Time.now)
unless user_badge
UserBadge.transaction do
user_badge = UserBadge.create!(badge: @badge, user: @user,
granted_by: @granted_by, granted_at: Time.now)
Badge.increment_counter 'grant_count', @badge.id
if @granted_by != Discourse.system_user
StaffActionLogger.new(@granted_by).log_badge_grant(user_badge)
Badge.increment_counter 'grant_count', @badge.id
if @granted_by != Discourse.system_user
StaffActionLogger.new(@granted_by).log_badge_grant(user_badge)
end
end
end