mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-16 18:05:35 +08:00
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.
33 lines
946 B
Ruby
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
|