From 9bfc356b9d3050310a5d99a5177d10c8f72dca29 Mon Sep 17 00:00:00 2001 From: xEsiah Date: Sat, 17 Oct 2026 14:59:09 +0200 Subject: [PATCH] fix(chat): default the read cursor to 0 when counting unread The unread count compared message ids against COALESCE over a scalar subquery with no fallback. When a user had never opened a discussion there was no chat_reads row, so the subquery returned NULL and `m.id > NULL` was never true: the folder never got a badge even though messages from others were unread. Give COALESCE an explicit 0 default so discussions that were never opened count all of the others' messages as unread. --- backend/src/routes/chat.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/routes/chat.ts b/backend/src/routes/chat.ts index 04adf61..aaa3b3c 100644 --- a/backend/src/routes/chat.ts +++ b/backend/src/routes/chat.ts @@ -111,9 +111,9 @@ export const registerChatRoutes = (app: App, db: Db, config: Config): void => { AND m.user_id <> ? WHERE f.deleted_at IS NULL AND m.id > COALESCE ( - SELECT cr.last_read_message_id - FROM chat_reads cr - WHERE cr.folder_id = f.id AND cr.user_id = ? ) + ( SELECT cr.last_read_message_id + FROM chat_reads cr + WHERE cr.folder_id = f.id AND cr.user_id = ? ), 0 ) AND ${READ_SQL} GROUP BY f.id HAVING unread > 0`,