Show current user rank in leaderboard (#1263)

close #1000 maybe #1178 too

*  Show current user rank in the leaderboard with +-1 user (only on leaderboard 
*  Extend auto_main script to use random user.
*  Support colSpan in the DataTable component (I haven't verified colSpan in header yet, leave that until we need it)
*  Refactor OasstError to include the path and request method.
This commit is contained in:
notmd
2023-02-07 14:49:50 +09:00
committed by GitHub
parent 931e12f31e
commit 952e021c88
8 changed files with 396 additions and 254 deletions
+23 -2
View File
@@ -1,5 +1,6 @@
import { withoutRole } from "src/lib/auth";
import { createApiClient } from "src/lib/oasst_client_factory";
import { getBackendUserCore } from "src/lib/users";
import { LeaderboardTimeFrame } from "src/types/Leaderboard";
/**
@@ -7,9 +8,29 @@ import { LeaderboardTimeFrame } from "src/types/Leaderboard";
*/
const handler = withoutRole("banned", async (req, res, token) => {
const oasstApiClient = await createApiClient(token);
const backendUser = await getBackendUserCore(token.sub);
const time_frame = (req.query.time_frame as LeaderboardTimeFrame) ?? LeaderboardTimeFrame.day;
const info = await oasstApiClient.fetch_leaderboard(time_frame, { limit: req.query.limit as unknown as number });
res.status(200).json(info);
const includeUserStats = req.query.includeUserStats;
if (includeUserStats !== "true") {
const leaderboard = await oasstApiClient.fetch_leaderboard(time_frame, {
limit: req.query.limit as unknown as number,
});
return res.status(200).json(leaderboard);
}
const user = await oasstApiClient.fetch_frontend_user(backendUser);
const [leaderboard, user_stats] = await Promise.all([
oasstApiClient.fetch_leaderboard(time_frame, {
limit: req.query.limit as unknown as number,
}),
oasstApiClient.fetch_user_stats_window(user.user_id, time_frame, 3),
]);
res.status(200).json({
...leaderboard,
user_stats_window: user_stats.leaderboard.map((stats) => ({ ...stats, is_window: true })),
});
});
export default handler;