diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-09-05 23:29:22 +1000 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-09-05 23:29:22 +1000 |
commit | add16ab26eb37cace5663980cb3c3042871b7693 (patch) | |
tree | ee7185cb59f1bbea9a62f3831165e73e88d34267 /src/api/routes/channels/#channel_id | |
parent | Merge pull request #1097 from spacebarchat/steam-facebook-embed-handlers (diff) | |
download | server-add16ab26eb37cace5663980cb3c3042871b7693.tar.xz |
* allow limit=1
* fix GET messages?around order
Diffstat (limited to 'src/api/routes/channels/#channel_id')
-rw-r--r-- | src/api/routes/channels/#channel_id/messages/index.ts | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index c384a05b..a5bfcfd7 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -40,7 +40,13 @@ import { import { Request, Response, Router } from "express"; import { HTTPError } from "lambert-server"; import multer from "multer"; -import { FindManyOptions, FindOperator, LessThan, MoreThan } from "typeorm"; +import { + FindManyOptions, + FindOperator, + LessThan, + MoreThan, + MoreThanOrEqual, +} from "typeorm"; import { URL } from "url"; const router: Router = Router(); @@ -122,12 +128,24 @@ router.get( if (around) { query.take = Math.floor(limit / 2); - 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; + if (query.take != 0) { + const [right, left] = await Promise.all([ + Message.find({ ...query, where: { id: LessThan(around) } }), + Message.find({ + ...query, + where: { id: MoreThanOrEqual(around) }, + }), + ]); + left.push(...right); + messages = left; + } else { + query.take = 1; + const message = await Message.findOne({ + ...query, + where: { id: around }, + }); + messages = message ? [message] : []; + } } else { if (after) { if (BigInt(after) > BigInt(Snowflake.generate())) |