summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorMathMan05 <mathmanrm@gmail.com>2026-02-12 17:22:19 -0600
committerRory& <root@rory.gay>2026-02-13 00:33:42 +0100
commitca164c5bff59d618ee5cfe97a5b62dd3cf53b8b5 (patch)
treef470b40630382c7e0e692202c7db5c4ebb34f5e6 /src/api
parentUpdate dependencies (nix/node) (diff)
downloadserver-ts-ca164c5bff59d618ee5cfe97a5b62dd3cf53b8b5.tar.xz
speed up fetching messages
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/channels/#channel_id/messages/index.ts32
1 files changed, 4 insertions, 28 deletions
diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts

index e391b2ab..2446da63 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts
@@ -121,6 +121,7 @@ router.get( const query: FindManyOptions<Message> & { where: { id?: FindOperator<string> | FindOperator<string>[] }; } = { + relationLoadStrategy: "query", order: { timestamp: "DESC" }, take: limit, where: { channel_id }, @@ -133,16 +134,6 @@ router.get( mention_channels: true, sticker_items: true, attachments: true, - referenced_message: { - author: true, - webhook: true, - application: true, - mentions: true, - mention_roles: true, - mention_channels: true, - sticker_items: true, - attachments: true, - }, thread: { recipients: { user: true, @@ -156,6 +147,7 @@ router.get( if (around) { query.take = Math.floor(limit / 2); if (query.take != 0) { + console.time("Query"); const [right, left] = await Promise.all([ Message.find({ ...query, @@ -167,6 +159,7 @@ router.get( order: { timestamp: "ASC" }, }), ]); + console.timeEnd("Query"); left.push(...right); messages = left.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()); } else { @@ -192,6 +185,7 @@ router.get( messages = await Message.find(query); } + await Message.fillReplies(messages); const endpoint = Config.get().cdn.endpointPublic; const ret = messages.map((x: Message) => { @@ -267,24 +261,6 @@ router.get( }), ); - // polyfill message references for old messages - await Promise.all( - ret - .filter((msg) => msg.message_reference && !msg.referenced_message?.id && msg.message_reference.message_id) - .map(async (msg) => { - const whereOptions: { id: string; guild_id?: string; channel_id?: string } = { - id: msg.message_reference!.message_id as string, - }; - if (msg.message_reference!.guild_id) whereOptions.guild_id = msg.message_reference!.guild_id; - if (msg.message_reference!.channel_id) whereOptions.channel_id = msg.message_reference!.channel_id; - - msg.referenced_message = await Message.findOne({ - where: whereOptions, - relations: { author: true, mentions: true, mention_roles: true, mention_channels: true }, - }); - }), - ); - return res.json(ret); }, );