From 08f17f88e78bb01ecc822a16d22b8b35c01ee83d Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Fri, 28 Jul 2023 09:45:13 +1000 Subject: Fix regression of 5f22faa57b7104ba29a01889a1bafc35b3be4f02 --- .../routes/channels/#channel_id/messages/index.ts | 35 +++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index a15b462d..4cc38497 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -119,25 +119,26 @@ router.get( }; let messages: Message[]; - if (after) { - if (BigInt(after) > BigInt(Snowflake.generate())) - return res.status(422); - query.where.id = MoreThan(after); - messages = await Message.find(query); - } else if (before) { - if (BigInt(before) < BigInt(req.params.channel_id)) - return res.status(422); - query.where.id = LessThan(before); - messages = await Message.find(query); - } else if (around) { + + if (around) { query.take = Math.floor(limit / 2); - query.where.id = LessThan(around); - const messages_before = await Message.find(query); - query.where.id = MoreThan(around); - const messages_after = await Message.find(query); - messages = messages_before.concat(messages_after); + const [right, left] = await Promise.all([ + Message.find({ ...query, where: { id: LessThan(around) } }), + Message.find({ ...query, where: { id: MoreThan(around) } }), + ]); + messages = right.concat(left); } else { - throw new HTTPError("after, around or before must be present", 422); + if (after) { + if (BigInt(after) > BigInt(Snowflake.generate())) + return res.status(422); + query.where.id = MoreThan(after); + } else if (before) { + if (BigInt(before) < BigInt(Snowflake.generate())) + return res.status(422); + query.where.id = LessThan(before); + } + + messages = await Message.find(query); } const endpoint = Config.get().cdn.endpointPublic; -- cgit 1.4.1