(req: MonkeyRequest)
| 534 | } |
| 535 | |
| 536 | export async function getUser(req: MonkeyRequest): Promise<GetUserResponse> { |
| 537 | const { uid } = req.ctx.decodedToken; |
| 538 | |
| 539 | const { data: userInfo, error } = await tryCatch( |
| 540 | UserDAL.getUser(uid, "get user"), |
| 541 | ); |
| 542 | |
| 543 | if (error) { |
| 544 | if (error instanceof MonkeyError && error.status === 404) { |
| 545 | //if the user is in the auth system but not in the db, its possible that the user was created by bypassing captcha |
| 546 | //since there is no data in the database anyway, we can just delete the user from the auth system |
| 547 | //and ask them to sign up again |
| 548 | try { |
| 549 | await AuthUtil.deleteUser(uid); |
| 550 | throw new MonkeyError( |
| 551 | 404, |
| 552 | "User not found in the database, but found in the auth system. We have deleted the ghost user from the auth system. Please sign up again.", |
| 553 | "get user", |
| 554 | uid, |
| 555 | ); |
| 556 | } catch (e) { |
| 557 | // oxlint-disable-next-line no-unsafe-member-access |
| 558 | if (e.code === "auth/user-not-found") { |
| 559 | throw new MonkeyError( |
| 560 | 404, |
| 561 | "User not found in the database or the auth system. Please sign up again.", |
| 562 | "get user", |
| 563 | uid, |
| 564 | ); |
| 565 | } else { |
| 566 | throw e; |
| 567 | } |
| 568 | } |
| 569 | } else { |
| 570 | throw error; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | userInfo.personalBests ??= { |
| 575 | time: {}, |
| 576 | words: {}, |
| 577 | quote: {}, |
| 578 | zen: {}, |
| 579 | custom: {}, |
| 580 | }; |
| 581 | |
| 582 | const agentLog = buildAgentLog(req); |
| 583 | void addLog("user_data_requested", agentLog, uid); |
| 584 | void UserDAL.logIpAddress(uid, agentLog.ip, userInfo); |
| 585 | |
| 586 | let inboxUnreadSize = 0; |
| 587 | if (req.ctx.configuration.users.inbox.enabled) { |
| 588 | inboxUnreadSize = userInfo.inbox?.filter((mail) => !mail.read).length ?? 0; |
| 589 | } |
| 590 | |
| 591 | if (!userInfo.name) { |
| 592 | userInfo.needsToChangeName = true; |
| 593 | await UserDAL.flagForNameChange(uid); |
nothing calls this directly
no test coverage detected