From 2b37cd2a5ec2bff97583fe75471715cd24151d85 Mon Sep 17 00:00:00 2001 From: MathMan05 Date: Wed, 4 Feb 2026 12:41:42 -0600 Subject: [PATCH] some search fixes while I'm here --- .../guilds/#guild_id/messages/search.ts | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/api/routes/guilds/#guild_id/messages/search.ts b/src/api/routes/guilds/#guild_id/messages/search.ts index d0888fe0e..a762e5297 100644 --- a/src/api/routes/guilds/#guild_id/messages/search.ts +++ b/src/api/routes/guilds/#guild_id/messages/search.ts @@ -51,7 +51,7 @@ router.get( // sort_by, // TODO: Handle 'relevance' limit, author_id, - } = req.query; + } = req.query as Record; const parsedLimit = Number(limit) || 50; if (parsedLimit < 1 || parsedLimit > 100) throw new HTTPError("limit must be between 1 and 100", 422); @@ -78,32 +78,32 @@ router.get( guild: { id: req.params.guild_id as string, }, + ...(content ? { content: Like(`%${content}%`) } : {}), + ...(author_id ? { author_id } : {}), + ...(channel_id + ? { channel_id } + : { + channel_id: In( + ( + await Promise.all( + ( + await Channel.find({ + where: { guild_id: req.params.guild_id as string }, + select: { id: true }, + }) + ).map(async (channel) => { + const perm = await getPermission(req.user_id, req.params.guild_id as string, channel.id); + return perm.has("VIEW_CHANNEL") && perm.has("READ_MESSAGE_HISTORY") ? channel : undefined; + }), + ) + ) + .filter((_) => _ !== undefined) + .map(({ id }) => id), + ), + }), }, relations: { author: true, webhook: true, application: true, mentions: true, mention_roles: true, mention_channels: true, sticker_items: true, attachments: true }, }; - //@ts-ignore - if (channel_id) query.where.channel = { id: channel_id }; - else { - // get all channel IDs that this user can access - const channels = await Channel.find({ - where: { guild_id: req.params.guild_id as string }, - select: { id: true }, - }); - const ids = []; - - for (const channel of channels) { - const perm = await getPermission(req.user_id, req.params.guild_id as string, channel.id); - if (!perm.has("VIEW_CHANNEL") || !perm.has("READ_MESSAGE_HISTORY")) continue; - ids.push(channel.id); - } - - //@ts-ignore - query.where.channel = { id: In(ids) }; - } - //@ts-ignore - if (author_id) query.where.author = { id: author_id }; - //@ts-ignore - if (content) query.where.content = Like(`%${content}%`); const messages: Message[] = await Message.find({ ...query, take: parsedLimit || 0, skip: offset ? Number(offset) : 0 }); delete query.take;