Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show correct discord avatar on leaderboard (#1955)
Closes #1951 ![image](https://user-images.githubusercontent.com/24505302/222885756-166318fd-7936-4782-ae6d-7b9ab31d4de8.png)
- Loading branch information
Showing
6 changed files
with
102 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { getValidDisplayName } from "src/lib/display_name_validation"; | ||
import { getBatchFrontendUserIdFromBackendUser } from "src/lib/users"; | ||
|
||
export const updateUsersDisplayNames = <T extends { display_name: string; username: string }>(entries: T[]) => { | ||
return entries.map((entry) => ({ | ||
...entry, | ||
display_name: getValidDisplayName(entry.display_name, entry.username), | ||
})); | ||
}; | ||
|
||
export const updateUsersProfilePictures = async <T extends { auth_method: string; username: string }>(entires: T[]) => { | ||
const frontendUserIds = await getBatchFrontendUserIdFromBackendUser(entires); | ||
|
||
const items = await prisma.user.findMany({ | ||
where: { id: { in: frontendUserIds } }, | ||
select: { image: true }, | ||
}); | ||
|
||
return entires.map((entry, idx) => ({ ...entry, image: items[idx].image })); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
import { withAnyRole } from "src/lib/auth"; | ||
import { updateUsersDisplayNames, updateUsersProfilePictures } from "src/lib/leaderboard_utilities"; | ||
import { createApiClient } from "src/lib/oasst_client_factory"; | ||
import { TrollboardTimeFrame } from "src/types/Trollboard"; | ||
import { getValidDisplayName } from "src/lib/display_name_validation"; | ||
|
||
export default withAnyRole(["admin", "moderator"], async (req, res, token) => { | ||
const client = await createApiClient(token); | ||
const limit = parseInt(req.query.limit as string); | ||
const enabled = req.query.enabled === "true"; | ||
|
||
const trollboard = await client.fetch_trollboard(req.query.time_frame as TrollboardTimeFrame, { | ||
limit: req.query.limit as unknown as number, | ||
enabled: req.query.enabled as unknown as boolean, | ||
const trollboardReply = await client.fetch_trollboard(req.query.time_frame as TrollboardTimeFrame, { | ||
limit, | ||
enabled, | ||
}); | ||
|
||
trollboard.trollboard.forEach((troll) => { | ||
troll.display_name = getValidDisplayName(troll.display_name, troll.username); | ||
}); | ||
trollboardReply.trollboard = updateUsersDisplayNames(trollboardReply.trollboard); | ||
trollboardReply.trollboard = await updateUsersProfilePictures(trollboardReply.trollboard); | ||
|
||
return res.status(200).json(trollboard); | ||
return res.status(200).json(trollboardReply); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,34 @@ | ||
import { withoutRole } from "src/lib/auth"; | ||
import { updateUsersDisplayNames, updateUsersProfilePictures } from "src/lib/leaderboard_utilities"; | ||
import { createApiClient } from "src/lib/oasst_client_factory"; | ||
import { getBackendUserCore } from "src/lib/users"; | ||
import { LeaderboardTimeFrame } from "src/types/Leaderboard"; | ||
import { getValidDisplayName } from "src/lib/display_name_validation"; | ||
|
||
/** | ||
* Returns the set of valid labels that can be applied to messages. | ||
*/ | ||
const handler = withoutRole("banned", async (req, res, token) => { | ||
export default 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 includeUserStats = req.query.includeUserStats; | ||
const limit = parseInt(req.query.limit as string); | ||
const includeUserStats = req.query.includeUserStats === "true"; | ||
|
||
if (includeUserStats !== "true") { | ||
let leaderboard = await oasstApiClient.fetch_leaderboard(time_frame, { | ||
limit: req.query.limit as unknown as number, | ||
}); | ||
leaderboard = getValidLeaderboard(leaderboard); | ||
return res.status(200).json(leaderboard); | ||
if (!includeUserStats) { | ||
const leaderboardReply = await oasstApiClient.fetch_leaderboard(time_frame, { limit }); | ||
leaderboardReply.leaderboard = updateUsersDisplayNames(leaderboardReply.leaderboard); | ||
leaderboardReply.leaderboard = await updateUsersProfilePictures(leaderboardReply.leaderboard); | ||
return res.status(200).json(leaderboardReply); | ||
} | ||
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, | ||
}), | ||
const [leaderboardReply, user_stats] = await Promise.all([ | ||
oasstApiClient.fetch_leaderboard(time_frame, { limit }), | ||
oasstApiClient.fetch_user_stats_window(user.user_id, time_frame, 3), | ||
]); | ||
|
||
const validLeaderboard = getValidLeaderboard(leaderboard); | ||
leaderboardReply.leaderboard = updateUsersDisplayNames(leaderboardReply.leaderboard); | ||
leaderboardReply.leaderboard = await updateUsersProfilePictures(leaderboardReply.leaderboard); | ||
|
||
res.status(200).json({ | ||
...validLeaderboard, | ||
...leaderboardReply, | ||
user_stats_window: user_stats?.leaderboard.map((stats) => ({ ...stats, is_window: true })), | ||
}); | ||
}); | ||
|
||
const getValidLeaderboard = (leaderboard) => { | ||
leaderboard.leaderboard.forEach((user) => { | ||
user.display_name = getValidDisplayName(user.display_name, user.username); | ||
}); | ||
return leaderboard; | ||
}; | ||
|
||
export default handler; |