fix(web): reconcile unread mention counters in realtime
All checks were successful
CI / test (push) Successful in 20s

This commit is contained in:
2026-03-08 12:46:52 +03:00
parent 2af4588688
commit dcc0f2abbc
2 changed files with 39 additions and 7 deletions

View File

@@ -102,7 +102,7 @@ export function useRealtime() {
ws.send(JSON.stringify({ event: "message_read", payload: { chat_id: chatId, message_id: message.id } }));
chatStore.clearUnread(chatId);
} else if (wasInserted) {
chatStore.incrementUnread(chatId);
chatStore.incrementUnread(chatId, hasMentionForUser(message.text, authStore.me?.username ?? null));
}
maybeShowBrowserNotification(chatId, message, chatStore.activeChatId);
}
@@ -331,3 +331,21 @@ function maybeShowBrowserNotification(chatId: number, message: Message, activeCh
};
})();
}
function hasMentionForUser(text: string | null, username: string | null): boolean {
if (!text || !username) {
return false;
}
const normalizedUsername = username.toLowerCase();
const mentionRegex = /(^|[^A-Za-z0-9_])@([A-Za-z0-9_]{3,50})(?![A-Za-z0-9_])/g;
let match: RegExpExecArray | null;
while (true) {
match = mentionRegex.exec(text);
if (!match) {
return false;
}
if ((match[2] ?? "").toLowerCase() === normalizedUsername) {
return true;
}
}
}