From 269d4eecdd3c1ff053b2749ddc186285f1a17a16 Mon Sep 17 00:00:00 2001 From: TomatoCake <60300461+DEVTomatoCake@users.noreply.github.com> Date: Sun, 1 Sep 2024 18:44:35 +0200 Subject: [PATCH] Stop returning defaults when using getPublicUser() --- src/api/routes/channels/#channel_id/invites.ts | 8 ++++---- src/api/routes/users/#id/profile.ts | 5 ++++- src/api/util/handlers/Message.ts | 4 +++- src/gateway/opcodes/LazyRequest.ts | 2 +- src/util/entities/User.ts | 10 ++++------ 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/api/routes/channels/#channel_id/invites.ts b/src/api/routes/channels/#channel_id/invites.ts index ae32e80de..3bb371955 100644 --- a/src/api/routes/channels/#channel_id/invites.ts +++ b/src/api/routes/channels/#channel_id/invites.ts @@ -1,17 +1,17 @@ /* Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Copyright (C) 2023 Spacebar and Spacebar Contributors - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ @@ -84,7 +84,7 @@ router.post( }).save(); const data = invite.toJSON(); - data.inviter = (await User.getPublicUser(req.user_id)).toPublicUser(); + data.inviter = await User.getPublicUser(req.user_id); data.guild = await Guild.findOne({ where: { id: guild_id } }); data.channel = channel; diff --git a/src/api/routes/users/#id/profile.ts b/src/api/routes/users/#id/profile.ts index 44271cad3..6d727dcfb 100644 --- a/src/api/routes/users/#id/profile.ts +++ b/src/api/routes/users/#id/profile.ts @@ -41,7 +41,10 @@ router.get( const { guild_id, with_mutual_guilds } = req.query; - const user = await User.getPublicUser(req.params.id, { + const user = await User.findOneOrFail({ + where: { + id: req.params.id, + }, relations: ["connected_accounts"], }); diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts index f037417a5..256932004 100644 --- a/src/api/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts @@ -87,7 +87,9 @@ export async function handleMessage(opts: MessageOptions): Promise { } if (opts.author_id) { - message.author = await User.getPublicUser(opts.author_id); + message.author = await User.findOneOrFail({ + where: { id: opts.author_id }, + }); const rights = await getRights(opts.author_id); rights.hasThrow("SEND_MESSAGES"); } diff --git a/src/gateway/opcodes/LazyRequest.ts b/src/gateway/opcodes/LazyRequest.ts index 45eaccfaf..c58642b79 100644 --- a/src/gateway/opcodes/LazyRequest.ts +++ b/src/gateway/opcodes/LazyRequest.ts @@ -238,7 +238,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore if (session?.status == "unknown") session.status = "online"; - const user = (await User.getPublicUser(x)).toPublicUser(); // why is this needed? + const user = await User.getPublicUser(x); return Send(this, { op: OPCODES.Dispatch, diff --git a/src/util/entities/User.ts b/src/util/entities/User.ts index 0323de528..89061e7ce 100644 --- a/src/util/entities/User.ts +++ b/src/util/entities/User.ts @@ -279,14 +279,12 @@ export class User extends BaseClass { return user as UserPrivate; } - static async getPublicUser(user_id: string, opts?: FindOneOptions) { - return await User.findOneOrFail({ + static async getPublicUser(user_id: string): Promise { + const user = await User.findOneOrFail({ where: { id: user_id }, - ...opts, - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - //@ts-ignore - select: [...PublicUserProjection, ...(opts?.select || [])], // TODO: fix + select: PublicUserProjection, }); + return user.toPublicUser(); } public static async generateDiscriminator(