mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-05 20:38:57 +08:00
33 lines
942 B
Ruby
33 lines
942 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
|