web: group notifications per chat thread
Some checks failed
Android CI / android (push) Has started running
Android Release / release (push) Has been cancelled
CI / test (push) Has been cancelled

This commit is contained in:
Codex
2026-03-10 00:39:58 +03:00
parent 148870de14
commit e21a54e2bf

View File

@@ -26,15 +26,34 @@ export async function showNotificationViaServiceWorker(params: {
if (!registration) { if (!registration) {
return false; return false;
} }
const tag = `chat-${params.chatId}`;
const active = await registration.getNotifications({ tag });
const prevData = active[0]?.data as
| {
unreadCount?: number;
previews?: string[];
}
| undefined;
const unreadCount = Math.max(0, Number(prevData?.unreadCount ?? 0)) + 1;
const previews = [params.body, ...(prevData?.previews ?? [])]
.filter((item) => item && item.trim().length > 0)
.slice(0, 3);
const extraCount = Math.max(0, unreadCount - previews.length);
const groupedBody =
previews.length <= 1
? previews[0] ?? params.body
: `${previews.join("\n")}${extraCount > 0 ? `\n+${extraCount} more` : ""}`;
const url = `/?chat=${params.chatId}&message=${params.messageId}`; const url = `/?chat=${params.chatId}&message=${params.messageId}`;
await registration.showNotification(params.title, { await registration.showNotification(params.title, {
body: params.body, body: groupedBody,
icon: params.image, icon: params.image,
tag: `chat-${params.chatId}`, tag,
data: { data: {
chatId: params.chatId, chatId: params.chatId,
messageId: params.messageId, messageId: params.messageId,
url, url,
unreadCount,
previews,
}, },
}); });
return true; return true;