diff --git a/src/api/routes/guilds/#guild_id/messages/search.ts b/src/api/routes/guilds/#guild_id/messages/search.ts
index d0888fe0..a762e529 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<string, string>;
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;
|