@@ -110,7 +110,7 @@ def self.mass_grant(badge, user, count:)
110110 WHERE notification_id IS NULL AND user_id = :user_id AND badge_id = :badge_id
111111 SQL
112112
113- UserBadge . update_featured_ranks! ( user . id )
113+ UserBadge . update_featured_ranks! ( [ user . id ] )
114114 end
115115 end
116116
@@ -404,8 +404,9 @@ def self.backfill(badge, opts = nil)
404404 post_clause = badge . target_posts ? "AND (q.post_id = ub.post_id OR NOT :multiple_grant)" : ""
405405 post_id_field = badge . target_posts ? "q.post_id" : "NULL"
406406
407- sql = <<~SQL
408- DELETE FROM user_badges
407+ if badge . auto_revoke && full_backfill
408+ sql = <<~SQL
409+ DELETE FROM user_badges
409410 WHERE id IN (
410411 SELECT ub.id
411412 FROM user_badges ub
@@ -415,17 +416,22 @@ def self.backfill(badge, opts = nil)
415416 #{ post_clause }
416417 WHERE ub.badge_id = :id AND q.user_id IS NULL
417418 )
418- SQL
419+ RETURNING user_badges.user_id
420+ SQL
419421
420- if badge . auto_revoke && full_backfill
421- DB . exec (
422- sql ,
423- id : badge . id ,
424- post_ids : [ -1 ] ,
425- user_ids : [ -2 ] ,
426- backfill : true ,
427- multiple_grant : true , # cheat here, cause we only run on backfill and are deleting
428- )
422+ rows =
423+ DB . query (
424+ sql ,
425+ id : badge . id ,
426+ post_ids : [ -1 ] ,
427+ user_ids : [ -2 ] ,
428+ backfill : true ,
429+ multiple_grant : true , # cheat here, cause we only run on backfill and are deleting
430+ )
431+
432+ if ( revoked_callback = opts &.dig ( :revoked_callback ) ) && rows . size > 0
433+ revoked_callback . call ( rows . map { |r | r . user_id } )
434+ end
429435 end
430436
431437 sql = <<~SQL
@@ -465,34 +471,41 @@ def self.backfill(badge, opts = nil)
465471 return
466472 end
467473
468- builder
469- . query (
474+ rows =
475+ builder . query (
470476 id : badge . id ,
471477 multiple_grant : badge . multiple_grant ,
472478 backfill : full_backfill ,
473479 post_ids : post_ids || [ -2 ] ,
474480 user_ids : user_ids || [ -2 ] ,
475481 )
476- . each do |row |
477- next if suppress_notification? ( badge , row . granted_at , row . skip_new_user_tips )
478- next if row . staff && badge . awarded_for_trust_level?
479482
480- notification = send_notification ( row . user_id , row . username , row . locale , badge )
481- UserBadge . trigger_user_badge_granted_event ( badge . id , row . user_id )
483+ if ( granted_callback = opts &.dig ( :granted_callback ) ) && rows . size > 0
484+ granted_callback . call ( rows . map { |r | r . user_id } )
485+ end
482486
483- DB . exec (
484- "UPDATE user_badges SET notification_id = :notification_id WHERE id = :id" ,
485- notification_id : notification . id ,
486- id : row . id ,
487- )
488- end
487+ rows . each do |row |
488+ next if suppress_notification? ( badge , row . granted_at , row . skip_new_user_tips )
489+ next if row . staff && badge . awarded_for_trust_level?
490+
491+ notification = send_notification ( row . user_id , row . username , row . locale , badge )
492+ UserBadge . trigger_user_badge_granted_event ( badge . id , row . user_id )
493+
494+ DB . exec (
495+ "UPDATE user_badges SET notification_id = :notification_id WHERE id = :id" ,
496+ notification_id : notification . id ,
497+ id : row . id ,
498+ )
499+ end
489500
490501 badge . reset_grant_count!
491502 rescue => e
492503 raise GrantError , "Failed to backfill '#{ badge . name } ' badge: #{ opts } . Reason: #{ e . message } "
493504 end
494505
495- def self . revoke_ungranted_titles!
506+ def self . revoke_ungranted_titles! ( user_ids = nil )
507+ user_ids = user_ids . join ( ", " ) if user_ids
508+
496509 DB . exec <<~SQL
497510 UPDATE users u
498511 SET title = ''
@@ -509,6 +522,7 @@ def self.revoke_ungranted_titles!
509522 AND b.allow_title
510523 AND b.enabled
511524 )
525+ #{ user_ids . present? ? "AND u.id IN (:user_ids)" : "" }
512526 SQL
513527
514528 DB . exec <<~SQL
@@ -518,6 +532,7 @@ def self.revoke_ungranted_titles!
518532 WHERE up.user_id = u.id
519533 AND (u.title IS NULL OR u.title = '')
520534 AND up.granted_title_badge_id IS NOT NULL
535+ #{ user_ids . present? ? "AND up.user_id IN (:user_ids)" : "" }
521536 SQL
522537 end
523538
0 commit comments