This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import webpush from "web-push";
|
||||
|
||||
import { CouchDBChangesStream } from "couchdb-changes-stream";
|
||||
import { qrDb, messagesDbUrl } from "./dbs";
|
||||
import { qrDb, messagesDbUrl, webPushSubscriptionsDb } from "./dbs";
|
||||
import { setupVapidDetails } from "./setupVapidDetails";
|
||||
import type { MessageDocument } from "@hereconnect/types";
|
||||
import { ServiceSeqTracker } from "./serviceSeqTracker";
|
||||
@@ -36,9 +36,9 @@ const start = async () => {
|
||||
console.info("Service started from sequence", since);
|
||||
|
||||
for await (const change of changesStream) {
|
||||
const { seq, doc } = change;
|
||||
const { seq, doc: msgDoc } = change;
|
||||
|
||||
if (!doc) {
|
||||
if (!msgDoc) {
|
||||
console.error("Change missing document:", change);
|
||||
continue;
|
||||
}
|
||||
@@ -46,28 +46,69 @@ const start = async () => {
|
||||
serviceSeqTracker.checkNextSeq(seq);
|
||||
|
||||
try {
|
||||
if (doc.from === "guest") {
|
||||
const qrDoc = await qrDb.get(`qr:${doc.qr_code_uri}`);
|
||||
if (msgDoc.from === "guest") {
|
||||
// send message to owner
|
||||
const qrDoc = await qrDb.get(`qr_code:${msgDoc.qr_code_uri}`);
|
||||
if (qrDoc.messageDeliveryMethod !== "webPush") {
|
||||
console.log(`Skip !webPush`);
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log("FROM GUEST");
|
||||
console.log(qrDoc.user_uuid);
|
||||
console.log(qrDoc.browser_uuid);
|
||||
} else if (doc.from === "owner") {
|
||||
const { rows } = await 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");
|
||||
}
|
||||
|
||||
for (const row of rows) {
|
||||
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 options: NotificationOptions = {
|
||||
icon: "/images/qr-code-logo-200x200.jpg",
|
||||
body: msgDoc.body,
|
||||
tag,
|
||||
requireInteraction: true,
|
||||
data: {
|
||||
url: `/manage/${msgDoc.qr_code_uri}/chat/${msgDoc.chat}`,
|
||||
payload: { tag },
|
||||
},
|
||||
} as const;
|
||||
|
||||
await webpush.sendNotification(
|
||||
subscription,
|
||||
JSON.stringify({
|
||||
type: "notification",
|
||||
notification: {
|
||||
title: qrDoc.name,
|
||||
options,
|
||||
},
|
||||
}),
|
||||
{
|
||||
urgency: "high",
|
||||
topic,
|
||||
},
|
||||
);
|
||||
|
||||
console.log("sent");
|
||||
}
|
||||
} else if (msgDoc.from === "owner") {
|
||||
console.log("FROM OWNER");
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Processed change: ${JSON.stringify({ _id: change.id, ref: doc._rev, seq: change.seq })}`,
|
||||
`Processed change: ${JSON.stringify({ _id: change.id, ref: msgDoc._rev, seq: change.seq })}`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed processing: ${JSON.stringify(error)} change: ${JSON.stringify({ id: doc._id, rev: doc._rev, seq: seq })}`,
|
||||
`Failed processing: ${JSON.stringify(error)} change: ${JSON.stringify({ id: msgDoc._id, rev: msgDoc._rev, seq: seq })}`,
|
||||
);
|
||||
} finally {
|
||||
await serviceSeqTracker.saveSeq(seq);
|
||||
|
||||
Reference in New Issue
Block a user