From c20429a8ff604d7c64e093a64181588e54cdf997 Mon Sep 17 00:00:00 2001 From: Vasilii Mikhailovskii Date: Sun, 1 Dec 2024 18:31:02 +0200 Subject: [PATCH] couchdb-changes-stream: add limit title and body length --- .../src/server.ts | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/services/message-delivery-method-web-push/src/server.ts b/services/message-delivery-method-web-push/src/server.ts index 0390301..693acef 100644 --- a/services/message-delivery-method-web-push/src/server.ts +++ b/services/message-delivery-method-web-push/src/server.ts @@ -11,6 +11,10 @@ const abortController = new AbortController(); startHealthServer(abortController); +function limitStringLengthWithEllipsis(str: string, maxLength: number): string { + return str.length > maxLength ? str.substring(0, maxLength) + "…" : str; +} + const start = async () => { await setupVapidDetails(); const serviceSeqTracker = await ServiceSeqTracker.Init( @@ -76,13 +80,21 @@ const start = async () => { try { const subscription = row.doc!.subscription; const topic = `chat-${msgDoc.qr_code_uri}-${msgDoc.chat}`; - const tag = `${topic}-${(new Date(msgDoc.created_at).getTime() - new Date(qrDoc.created_at).getTime()).toString(36)}`; + const timestamp = new Date(msgDoc.created_at).getTime(); + const tag = `${topic}-${(timestamp - new Date(qrDoc.created_at).getTime()).toString(36)}`; - const options: NotificationOptions = { + const title = limitStringLengthWithEllipsis( + `${qrDoc.name}: новое сообщение`, + 53, + ); + const body = limitStringLengthWithEllipsis(msgDoc.body, 470); + + const options: NotificationOptions & { timestamp: number } = { icon: "/images/qr-code-logo-200x200.jpg", - body: msgDoc.body, + body, tag, requireInteraction: true, + timestamp, data: { url: `/manage/${msgDoc.qr_code_uri}/chat/${msgDoc.chat}`, payload: { tag }, @@ -94,7 +106,7 @@ const start = async () => { JSON.stringify({ type: "notification", notification: { - title: qrDoc.name, + title, options, }, }),