couchdb-changes-stream: add limit title and body length
This commit is contained in:
@@ -11,6 +11,10 @@ const abortController = new AbortController();
|
|||||||
|
|
||||||
startHealthServer(abortController);
|
startHealthServer(abortController);
|
||||||
|
|
||||||
|
function limitStringLengthWithEllipsis(str: string, maxLength: number): string {
|
||||||
|
return str.length > maxLength ? str.substring(0, maxLength) + "…" : str;
|
||||||
|
}
|
||||||
|
|
||||||
const start = async () => {
|
const start = async () => {
|
||||||
await setupVapidDetails();
|
await setupVapidDetails();
|
||||||
const serviceSeqTracker = await ServiceSeqTracker.Init(
|
const serviceSeqTracker = await ServiceSeqTracker.Init(
|
||||||
@@ -76,13 +80,21 @@ const start = async () => {
|
|||||||
try {
|
try {
|
||||||
const subscription = row.doc!.subscription;
|
const subscription = row.doc!.subscription;
|
||||||
const topic = `chat-${msgDoc.qr_code_uri}-${msgDoc.chat}`;
|
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",
|
icon: "/images/qr-code-logo-200x200.jpg",
|
||||||
body: msgDoc.body,
|
body,
|
||||||
tag,
|
tag,
|
||||||
requireInteraction: true,
|
requireInteraction: true,
|
||||||
|
timestamp,
|
||||||
data: {
|
data: {
|
||||||
url: `/manage/${msgDoc.qr_code_uri}/chat/${msgDoc.chat}`,
|
url: `/manage/${msgDoc.qr_code_uri}/chat/${msgDoc.chat}`,
|
||||||
payload: { tag },
|
payload: { tag },
|
||||||
@@ -94,7 +106,7 @@ const start = async () => {
|
|||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: "notification",
|
type: "notification",
|
||||||
notification: {
|
notification: {
|
||||||
title: qrDoc.name,
|
title,
|
||||||
options,
|
options,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user