Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: leaderboard discord properties being null (fehmer) #5032

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/__tests__/dal/leaderboards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ describe("LeaderboardsDal", () => {
expectedLbEntry(4, rank4, "60"),
]);
});
it("should not include discord properties for users without discord connection", async () => {
//GIVEN
const rank1 = await createUser(lbBests(pb(90), pb(100, 90, 2)), {
discordId: undefined,
discordAvatar: undefined,
});

//WHEN
await LeaderboardsDal.update("time", "60", "english");
const lb = (await LeaderboardsDal.get(
"time",
"60",
"english",
0
)) as SharedTypes.LeaderboardEntry[];

//THEN
expect(lb[0]).not.toHaveProperty("discordId");
expect(lb[0]).not.toHaveProperty("discordAvatar");
});

it("should update public speedHistogram for time english 15", async () => {
//GIVEN
Expand Down
8 changes: 6 additions & 2 deletions backend/src/dal/leaderboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ export async function update(
$addFields: {
[`${key}.uid`]: "$uid",
[`${key}.name`]: "$name",
[`${key}.discordId`]: "$discordId",
[`${key}.discordAvatar`]: "$discordAvatar",
[`${key}.discordId`]: {
$ifNull: ["$discordId", "$$REMOVE"],
},
[`${key}.discordAvatar`]: {
$ifNull: ["$discordAvatar", "$$REMOVE"],
},
[`${key}.rank`]: {
$function: {
body: "function() {try {row_number+= 1;} catch (e) {row_number= 1;}return row_number;}",
Expand Down
12 changes: 4 additions & 8 deletions frontend/src/ts/elements/leaderboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function updateFooter(lb: LbKey): void {
side = "right";
}

if (!Auth?.currentUser) {
if (Auth?.currentUser === undefined) {
$(`#leaderboardsWrapper table.${side} tfoot`).html(`
<tr>
<td colspan="6" style="text-align:center;"></>
Expand Down Expand Up @@ -307,7 +307,6 @@ async function fillTable(lb: LbKey): Promise<void> {
if (entry === undefined) {
break;
}
if (entry.hidden) return;
let meClassString = "";
if (entry.name === loggedInUserName) {
meClassString = ' class="me"';
Expand Down Expand Up @@ -443,7 +442,7 @@ async function update(): Promise<void> {
const lbRankRequests: Promise<
Ape.HttpClientResponse<Ape.Leaderboards.GetRank>
>[] = [];
if (Auth?.currentUser) {
if (Auth?.currentUser !== undefined) {
lbRankRequests.push(
...timeModes.map(async (mode2) => {
return Ape.leaderboards.getRank({
Expand Down Expand Up @@ -606,10 +605,7 @@ async function getAvatarUrls(
): Promise<(string | null)[]> {
return Promise.allSettled(
data.map(async (entry) =>
Misc.getDiscordAvatarUrl(
entry.discordId ?? undefined,
entry.discordAvatar ?? undefined
)
Misc.getDiscordAvatarUrl(entry.discordId, entry.discordAvatar)
)
).then((promises) => {
return promises.map((promise) => {
Expand Down Expand Up @@ -646,7 +642,7 @@ export function show(): void {
}
Skeleton.append(wrapperId);
if (!Misc.isPopupVisible("leaderboardsWrapper")) {
if (Auth?.currentUser) {
if (Auth?.currentUser !== undefined) {
$("#leaderboardsWrapper #leaderboards .rightTableJumpToMe").removeClass(
"disabled"
);
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/ts/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1364,10 +1364,8 @@ export async function getDiscordAvatarUrl(
): Promise<string | null> {
if (
discordId === undefined ||
discordId === null ||
discordId === "" ||
discordAvatar === undefined ||
discordAvatar === null ||
discordAvatar === ""
) {
return null;
Expand Down
5 changes: 2 additions & 3 deletions shared-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,10 @@ declare namespace SharedTypes {
consistency: number | "-";
uid: string;
name: string;
discordId: string | null | undefined;
discordAvatar: string | null | undefined;
discordId?: string;
discordAvatar?: string;
rank: number;
badgeId: number | null;
hidden?: boolean;
}

type PostResultResponse = {
Expand Down
Loading