From e86d4690a269cfe4ab301be12a63ad484a516f23 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Thu, 11 Jul 2024 12:13:25 +0200 Subject: [PATCH] Grant badges in `import:ensure_consistency` rake task It disables notifications for all badges. We don't want overwhelm migrated users with lots of notifications about badges. And this also speeds up badge granting... --- lib/tasks/import.rake | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index a7d1e844a50..ab293bf01f0 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -2,6 +2,9 @@ # Use http://tatiyants.com/pev/#/plans/new if you want to optimize a query +require "colored2" +require_relative "../../app/services/badge_granter" + task "import:ensure_consistency" => :environment do log "Starting..." @@ -27,6 +30,7 @@ task "import:ensure_consistency" => :environment do update_topic_users update_topic_featured_users create_category_definitions + grant_badges # run_jobs @@ -717,6 +721,32 @@ task "import:update_avatars_from_sso" => :environment do status_thread.join end +class NoNotificationBadeGranter < BadgeGranter + def self.suppress_notification?(badge, granted_at, skip_new_user_tips) + true + end +end + +def grant_badges + return unless SiteSetting.enable_badges + + Badge.enabled.find_each do |b| + begin + start_time = Time.now + print " - Backfilling #{b.name}" + NoNotificationBadeGranter.backfill(b) + puts ": #{(Time.now - start_time).to_i} seconds" + rescue StandardError + print ": " + puts "failed".red + end + end + + BadgeGranter.revoke_ungranted_titles! + UserBadge.ensure_consistency! # Badge granter sometimes uses raw SQL, so hooks do not run. Clean up data + UserStat.update_distinct_badge_count +end + def run_jobs log "Running jobs"