send push notification to guest
Main daploy / deploy (push) Failing after 6s

This commit is contained in:
2024-12-02 10:01:23 +02:00
parent 096c1547d4
commit b5a25a6ece
6 changed files with 124 additions and 78 deletions
@@ -62,6 +62,7 @@ const start = async () => {
try {
if (msgDoc.from === "guest") {
console.log("FROM GUEST");
// send message to owner
const qrDoc = await qrDb.get(`qr_code:${msgDoc.qr_code_uri}`);
if (qrDoc.messageDeliveryMethod !== "webPush") {
@@ -69,19 +70,37 @@ const start = async () => {
continue;
}
const { rows } = await webPushSubscriptionsDb.allDocs({
startkey: `web_push_subscription:${qrDoc.user_uuid}:`,
endkey: `web_push_subscription:${qrDoc.user_uuid}:\uffff`,
include_docs: true,
});
const [{ rows: pushSubscriptionRows }, { rows: userChatSettingsRows }] =
await Promise.all([
webPushSubscriptionsDb.allDocs({
startkey: `web_push_subscription:${qrDoc.user_uuid}:`,
endkey: `web_push_subscription:${qrDoc.user_uuid}:\uffff`,
include_docs: true,
}),
if (rows.length === 0) {
console.log("NO SUBSCRIPTIONS");
} else {
console.log(`sending to ${rows.length} devices`);
userChatSettingsDb.allDocs({
startkey: `user_chat_settings:${msgDoc.qr_code_uri}:${msgDoc.chat}:owner:`,
endkey: `user_chat_settings:${msgDoc.qr_code_uri}:${msgDoc.chat}:owner:\uffff`,
include_docs: true,
}),
]);
if (
userChatSettingsRows.some(
(row) => row.doc!.is_push_notification_enabled === false,
)
) {
console.log("push notifications is disabled");
continue;
}
for (const row of rows) {
if (pushSubscriptionRows.length === 0) {
console.log("NO SUBSCRIPTIONS");
} else {
console.log(`sending to ${pushSubscriptionRows.length} devices`);
}
for (const row of pushSubscriptionRows) {
try {
const subscription = row.doc!.subscription;
const topic = `chat-${msgDoc.qr_code_uri}-${msgDoc.chat}`;
@@ -138,10 +157,11 @@ const start = async () => {
);
for (const { doc: userChatSettingDoc } of userChatSettingsRows) {
if (!userChatSettingDoc) {
console.log("push notification has not been configured");
continue;
}
if (!userChatSettingDoc.is_push_notification_enabled) {
console.log("PUSH NOTIFICATION DISABLED");
console.log("push notification is disabled");
continue;
}
@@ -153,56 +173,56 @@ const start = async () => {
});
if (subscriptionRows.length === 0) {
console.log("NO SUBSCRIPTIONS");
console.log("no subscriptions");
} else {
console.log(`sending to ${subscriptionRows.length} devices`);
}
// for (const row of subscriptionRows) {
// try {
// const subscription = row.doc!.subscription;
// const topic = `chat-${msgDoc.qr_code_uri}-${msgDoc.chat}`;
// const timestamp = new Date(msgDoc.created_at).getTime();
// const tag = `${topic}-${(timestamp - new Date(qrDoc.created_at).getTime()).toString(36)}`;
//
// 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,
// tag,
// requireInteraction: true,
// timestamp,
// data: {
// url: `/manage/${msgDoc.qr_code_uri}/chat/${msgDoc.chat}`,
// payload: { tag },
// },
// } as const;
//
// await webpush.sendNotification(
// subscription,
// JSON.stringify({
// type: "notification",
// notification: {
// title,
// options,
// },
// }),
// {
// urgency: "high",
// topic,
// },
// );
// } catch (error) {
// console.error(
// `Error sending notification to ${row.id} - ${JSON.stringify(error)}`,
// );
// }
// }
for (const row of subscriptionRows) {
try {
const subscription = row.doc!.subscription;
const topic = `chat-${msgDoc.qr_code_uri}-${msgDoc.chat}`;
const timestamp = new Date(msgDoc.created_at).getTime();
const tag = `${topic}-${(timestamp - new Date(qrDoc.created_at).getTime()).toString(36)}`;
const title = limitStringLengthWithEllipsis(
`Новое сообщение`,
53,
);
const body = limitStringLengthWithEllipsis(msgDoc.body, 470);
const options: NotificationOptions & { timestamp: number } = {
icon: "/images/qr-code-logo-200x200.jpg",
body,
tag,
requireInteraction: true,
timestamp,
data: {
url: `/manage/${msgDoc.qr_code_uri}/chat/${msgDoc.chat}`,
payload: { tag },
},
} as const;
await webpush.sendNotification(
subscription,
JSON.stringify({
type: "notification",
notification: {
title,
options,
},
}),
{
urgency: "high",
topic,
},
);
} catch (error) {
console.error(
`Error sending notification to ${row.id} - ${JSON.stringify(error)}`,
);
}
}
}
} else {
continue;