diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-07-28 10:34:36 +1000 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-07-28 10:34:36 +1000 |
commit | a5a632115605b5b52d9ceded939695c89c868313 (patch) | |
tree | a9856d95e35070927e6ce2e0b11e0be43a60305b | |
parent | refactor checkToken (diff) | |
parent | goof (diff) | |
download | server-a5a632115605b5b52d9ceded939695c89c868313.tar.xz |
Merge branch 'master' into feat/refactorIdentify
-rw-r--r-- | src/api/routes/channels/#channel_id/messages/index.ts | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index a15b462d..75fb7b83 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -119,25 +119,27 @@ 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) } }), + ]); + right.push(...left); + messages = right; } 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; |