Skip to content

Commit 177eb59

Browse files
fix(profile): prevent profile XP updates on other user profile (@Majestic-Fire) (monkeytypegame#6897)
### Description - Fixed a visual bug in the updateXp() function where claiming rewards would update any visible profile - Added same user check when updating XP from alerts modal - Check uid or displayname, when sameUserCheck is true and the activePage is "profile". https://github.com/user-attachments/assets/bd6ec3ca-14ea-4021-a889-7c1931ed6888 ### Checks - [ ] Adding quotes? - [ ] Make sure to include translations for the quotes in the description (or another comment) so we can verify their content. - [ ] Adding a language? - Make sure to follow the [languages documentation](https://github.com/monkeytypegame/monkeytype/blob/master/docs/LANGUAGES.md) - [ ] Add language to `packages/schemas/src/languages.ts` - [ ] Add language to exactly one group in `frontend/src/ts/constants/languages.ts` - [ ] Add language json file to `frontend/static/languages` - [ ] Adding a theme? - Make sure to follow the [themes documentation](https://github.com/monkeytypegame/monkeytype/blob/master/docs/THEMES.md) - [ ] Add theme to `packages/schemas/src/themes.ts` - [ ] Add theme to `frontend/src/ts/constants/themes.ts` - [ ] Add theme css file to `frontend/static/themes` - [ ] Add some screenshot of the theme, especially with different test settings (colorful, flip colors) to your pull request - [ ] Adding a layout? - [ ] Make sure to follow the [layouts documentation](https://github.com/monkeytypegame/monkeytype/blob/master/docs/LAYOUTS.md) - [ ] Add layout to `packages/schemas/src/layouts.ts` - [ ] Add layout json file to `frontend/static/layouts` - [ ] Adding a font? - Make sure to follow the [themes documentation](https://github.com/monkeytypegame/monkeytype/blob/master/docs/FONTS.md) - [ ] Add font file to `frontend/static/webfonts` - [ ] Add font to `packages/schemas/src/fonts.ts` - [ ] Add font to `frontend/src/ts/constants/fonts.ts` - [x] Check if any open issues are related to this PR; if so, be sure to tag them below. - [x] Make sure the PR title follows the Conventional Commits standard. (https://www.conventionalcommits.org for more info) - [x] Make sure to include your GitHub username prefixed with @ inside parentheses at the end of the PR title. --------- Co-authored-by: Miodec <[email protected]>
1 parent 88bb7a0 commit 177eb59

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

frontend/src/ts/elements/alerts.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { updateXp as accountPageUpdateProfile } from "./profile";
1212
import { MonkeyMail } from "@monkeytype/schemas/users";
1313
import * as XPBar from "../elements/xp-bar";
1414
import * as AuthEvent from "../observables/auth-event";
15+
import * as ActivePage from "../states/active-page";
1516

1617
let accountAlerts: MonkeyMail[] = [];
1718
let maxMail = 0;
@@ -94,7 +95,12 @@ function hide(): void {
9495
if (totalXpClaimed > 0) {
9596
const snapxp = DB.getSnapshot()?.xp ?? 0;
9697
void XPBar.update(snapxp, totalXpClaimed);
97-
accountPageUpdateProfile(snapxp + totalXpClaimed);
98+
99+
const activePage = ActivePage.get();
100+
if (activePage === "account" || activePage === "profile") {
101+
accountPageUpdateProfile(activePage, snapxp + totalXpClaimed, true);
102+
}
103+
98104
DB.addXp(totalXpClaimed);
99105
}
100106
},

frontend/src/ts/elements/profile.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export async function update(
285285
}
286286
}
287287

288-
updateXp(profile.xp ?? 0);
288+
updateXp(where, profile.xp ?? 0);
289289
//lbs
290290

291291
if (banned) {
@@ -373,9 +373,22 @@ export async function update(
373373
}
374374
}
375375

376-
export function updateXp(xp: number): void {
377-
const details = $(" .profile .details .levelAndBar");
376+
export function updateXp(
377+
where: ProfileViewPaths,
378+
xp: number,
379+
sameUserCheck = false
380+
): void {
381+
const elementClass = where.charAt(0).toUpperCase() + where.slice(1);
382+
const profileElement = $(`.page${elementClass} .profile`);
383+
const details = $(`.page${elementClass} .profile .details .levelAndBar`);
384+
378385
if (details === undefined || details === null) return;
386+
387+
if (sameUserCheck && where === "profile") {
388+
const authedUserUid = getAuthenticatedUser()?.uid;
389+
if (authedUserUid !== profileElement.attr("uid")) return;
390+
}
391+
379392
const xpDetails = Levels.getXpDetails(xp);
380393
const xpForLevel = xpDetails.levelMaxXp;
381394
const xpToDisplay = xpDetails.levelCurrentXp;

0 commit comments

Comments
 (0)