mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-06-19 03:43:54 +08:00
Currently `e.message` would return the same error, but returning an exception message is brittle as it could change in the future and expose data in the serializer we wouldn't want to expose. Having high control on what we expose in the serializer is a more future proof pattern.
40 lines
1.3 KiB
Ruby
Vendored
40 lines
1.3 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module DiscourseGamification
|
|
class GamificationLeaderboardController < ::ApplicationController
|
|
requires_plugin PLUGIN_NAME
|
|
|
|
def respond
|
|
discourse_expires_in 1.minute
|
|
|
|
default_leaderboard_id = GamificationLeaderboard.first.id
|
|
params[:id] ||= default_leaderboard_id
|
|
leaderboard = GamificationLeaderboard.find(params[:id])
|
|
|
|
period_param = params[:period] == "all" ? "all_time" : params[:period]
|
|
|
|
raise Discourse::NotFound unless @guardian.can_see_leaderboard?(leaderboard)
|
|
|
|
render_serialized(
|
|
{
|
|
leaderboard: leaderboard,
|
|
page: params[:page].to_i,
|
|
for_user_id: current_user&.id,
|
|
period: leaderboard.resolve_period(period_param),
|
|
user_limit: params[:user_limit]&.to_i,
|
|
},
|
|
LeaderboardViewSerializer,
|
|
root: false,
|
|
)
|
|
rescue LeaderboardCachedView::NotReadyError
|
|
Jobs.enqueue(Jobs::GenerateLeaderboardPositions, leaderboard_id: leaderboard.id)
|
|
|
|
render json:
|
|
LeaderboardSerializer
|
|
.new(leaderboard)
|
|
.as_json
|
|
.merge({ users: [], reason: I18n.t("errors.leaderboard_positions_not_ready") }),
|
|
status: :accepted
|
|
end
|
|
end
|
|
end
|