From 6e5792ff7226a0cb59a1e3e5e96380433315f1ef Mon Sep 17 00:00:00 2001 From: Jannis Braun <151788261+TheZwiss@users.noreply.github.com> Date: Thu, 2 Apr 2026 17:51:37 +0200 Subject: [PATCH] feat(server): include attachment metadata in ready payload DM lastMessage --- packages/server/src/ws/handler.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/server/src/ws/handler.ts b/packages/server/src/ws/handler.ts index b34f15f7..e7e042e7 100644 --- a/packages/server/src/ws/handler.ts +++ b/packages/server/src/ws/handler.ts @@ -1165,6 +1165,24 @@ function buildReadyPayload(userId: string): { : []; const dmLastMsgMap = new Map(dmLastMessages.map(m => [m.dmChannelId, m])); + // Batch: attachments for last messages (1 query) + const dmLastMsgAttachments = dmLastMsgIds.length > 0 + ? batchInArray(dmLastMsgIds, ids => + db.select({ + dmMessageId: schema.attachments.dmMessageId, + type: schema.attachments.mimetype, + filename: schema.attachments.originalName, + }).from(schema.attachments).where(inArray(schema.attachments.dmMessageId, ids)).all() + ) + : []; + const dmLastMsgAttachmentMap = new Map>(); + for (const a of dmLastMsgAttachments) { + if (!a.dmMessageId) continue; + const arr = dmLastMsgAttachmentMap.get(a.dmMessageId) ?? []; + arr.push({ type: a.type, filename: a.filename }); + dmLastMsgAttachmentMap.set(a.dmMessageId, arr); + } + // Assemble DM channels with zero additional queries for (const dm of dmMemberships) { const dmChannel = dmChannelMap.get(dm.dmChannelId); @@ -1189,6 +1207,7 @@ function buildReadyPayload(userId: string): { userId: last.userId, content: last.content, createdAt: last.createdAt, + attachments: dmLastMsgAttachmentMap.get(last.id) ?? [], } : null, }); }