discourse/plugins/discourse-gamification/lib/discourse_gamification/user_extension.rb
Juan David Martínez Cubillos 99805a5d84
FIX: Suspended and deleted users from showing on Leaderboards (#33627)
Description

Prevent users from showing in leaderboards if they have been suspended
or deleted, likewise remove their score from the leaderboard score table
if the user is deleted.
2025-07-23 09:39:28 -05:00

33 lines
946 B
Ruby

# frozen_string_literal: true
module ::DiscourseGamification
module UserExtension
extend ActiveSupport::Concern
prepended do
has_many :gamification_scores,
class_name: "::DiscourseGamification::GamificationScore",
dependent: :destroy
end
DEFAULT_SCORE = 0
def gamification_score
return DEFAULT_SCORE if !default_leaderboard
DiscourseGamification::GamificationLeaderboard.find_position_by(
leaderboard_id: default_leaderboard.id,
period: "all_time",
for_user_id: self.id,
)&.total_score || DEFAULT_SCORE
rescue DiscourseGamification::LeaderboardCachedView::NotReadyError
Jobs.enqueue(Jobs::GenerateLeaderboardPositions, leaderboard_id: default_leaderboard.id)
DEFAULT_SCORE
end
def default_leaderboard
@default_leaderboard ||= DiscourseGamification::GamificationLeaderboard.select(:id).first
end
end
end