WIP message-delivery-method-web-push
Main daploy / deploy (push) Successful in 1m22s

This commit is contained in:
2024-11-27 15:57:11 +02:00
parent 056247a414
commit 1c8019d41a
48 changed files with 464 additions and 128 deletions
@@ -0,0 +1,81 @@
import webpush from "web-push";
import { CouchDBChangesStream } from "couchdb-changes-stream";
import { qrDb, messagesDbUrl } from "./dbs";
import { setupVapidDetails } from "./setupVapidDetails";
import type { MessageDocument } from "@hereconnect/types";
import { ServiceSeqTracker } from "./serviceSeqTracker";
const serviceSeqTracker = new ServiceSeqTracker();
const start = async () => {
await setupVapidDetails();
const since = await serviceSeqTracker.getSince();
const changesStream = new CouchDBChangesStream<MessageDocument>(
messagesDbUrl,
{
since,
feed: "continuous",
include_docs: true,
heartbeat: 1000,
filter: "_selector",
selector: {
type: "message",
},
},
);
// Gracefully handle SIGINT
process.on("SIGINT", async () => {
changesStream.stop();
process.stdout.write("\n");
process.exit(2);
});
console.info("Service started from sequence", since);
for await (const change of changesStream) {
const { seq, doc } = change;
if (!doc) {
console.error("Change missing document:", change);
continue;
}
serviceSeqTracker.checkNextSeq(seq);
try {
if (doc.from === "guest") {
const qrDoc = await qrDb.get(`qr:${doc.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") {
console.log("FROM OWNER");
} else {
continue;
}
console.log(
`Processed change: ${JSON.stringify({ _id: change.id, ref: doc._rev, seq: change.seq })}`,
);
} catch (error) {
console.error(
`Failed processing: ${JSON.stringify(error)} change: ${JSON.stringify({ id: doc._id, rev: doc._rev, seq: seq })}`,
);
} finally {
await serviceSeqTracker.saveSeq(seq);
}
}
};
start().catch((error) => {
console.error("error", error);
process.exit(1);
});