add guest subscribe push notification

This commit is contained in:
2024-12-02 08:50:25 +02:00
parent 2ae13f6ec4
commit 1231dc26ce
20 changed files with 742 additions and 49 deletions
@@ -11,35 +11,53 @@ const dbsSecurity = {
},
server_settings: {
admins: { roles: ["_admin"] },
qr: {
admins: { roles: ["_admin"] },
members: { roles: [] },
members: { roles: ["_admin"] },
},
public_settings: {
admins: { roles: ["_admin"] },
members: { roles: [] },
},
members: { roles: ["_admin"] },
},
qr: {
admins: { roles: ["_admin"] },
members: { roles: [] },
},
public_settings: {
admins: { roles: ["_admin"] },
members: { roles: [] },
},
user_chat_settings: {
admins: { roles: ["_admin"] },
members: { roles: [] },
},
};
export const up = async () => {
for (const [dbName, dbSecurity] of Object.entries(dbsSecurity)) {
await fetch(`${process.env.API_URL}/${dbName}`, {
const responseCreateDb = await fetch(`${process.env.API_URL}/${dbName}`, {
method: "PUT",
});
await fetch(`${process.env.API_URL}/${dbName}/_security`, {
method: "PUT",
body: JSON.stringify(dbSecurity),
});
console.assert(
(await responseCreateDb.json()).ok,
`${dbName} create db error`,
);
const responseSetSecurity = await fetch(
`${process.env.API_URL}/${dbName}/_security`,
{
method: "PUT",
body: JSON.stringify(dbSecurity),
},
);
console.assert(
(await responseSetSecurity.json()).ok,
`${dbName} security error`,
);
}
};
export const down = async () => {
for (const dbName of Object.keys(dbsSecurity)) {
await fetch(`${process.env.API_URL}/${dbName}`, {
const responseDeleteDb = await fetch(`${process.env.API_URL}/${dbName}`, {
method: "DELETE",
});
console.assert(
(await responseDeleteDb.json()).ok,
`${dbName} create db error`,
);
}
};
@@ -25,7 +25,7 @@
"contributors": [],
"scripts": {
"build": "tsc",
"generate-keys": "node ./src/generate-keys.js",
"generate-keys": "node src/generate-keys.ts",
"test-send-notification": "node ./src/test-send-notification.js",
"test-send-notification-cancel": "node ./src/test-send-notification-cancel.js"
},
@@ -3,8 +3,10 @@ import type {
MessageDocument,
QRCodeDocument,
ServerSettingsDocument,
UserChatSettingsDocument,
WebPushSubscriptionDocument,
} from "@hereconnect/types";
import { PushSubscription } from "web-push";
export interface ServiceDoc {
_id: string;
@@ -27,12 +29,11 @@ export const qrDb = new PouchDB<QRCodeDocument>(`${process.env.API_URL}/qr`, {
skip_setup: true,
});
export const webPushSubscriptionsDb = new PouchDB<WebPushSubscriptionDocument>(
`${process.env.API_URL}/web_push_subscriptions`,
{
skip_setup: true,
},
);
export const webPushSubscriptionsDb = new PouchDB<
WebPushSubscriptionDocument<PushSubscription>
>(`${process.env.API_URL}/web_push_subscriptions`, {
skip_setup: true,
});
export const { messagesDb, serviceDb } = (() => {
const messagesDB = new PouchDB(messagesDbUrl, {
@@ -44,3 +45,10 @@ export const { messagesDb, serviceDb } = (() => {
serviceDb: messagesDB as PouchDB.Database<ServiceDoc>,
} as const;
})();
export const userChatSettingsDb = new PouchDB<UserChatSettingsDocument>(
`${process.env.API_URL}/user_chat_settings`,
{
skip_setup: true,
},
);
@@ -1,5 +1,5 @@
import webpush from "web-push";
import PouchDB from "./PouchDB.js";
import PouchDB from "./PouchDB";
if (!process.env.VAPID_SUBJECT) {
throw new Error("VAPID_SUBJECT env is empty!");
@@ -1,7 +1,12 @@
import webpush from "web-push";
import { CouchDBChangesStream } from "@hereconnect/couchdb-changes-stream";
import { qrDb, messagesDbUrl, webPushSubscriptionsDb } from "./dbs";
import {
qrDb,
messagesDbUrl,
webPushSubscriptionsDb,
userChatSettingsDb,
} from "./dbs";
import { setupVapidDetails } from "./setupVapidDetails";
import type { MessageDocument } from "@hereconnect/types";
import { ServiceSeqTracker } from "./serviceSeqTracker";
@@ -123,6 +128,82 @@ const start = async () => {
}
} else if (msgDoc.from === "owner") {
console.log("FROM OWNER");
const qrDoc = await qrDb.get(`qr_code:${msgDoc.qr_code_uri}`);
const { rows: userChatSettingsRows } = await userChatSettingsDb.allDocs(
{
startkey: `user_chat_settings:${msgDoc.qr_code_uri}:${msgDoc.chat}:guest:`,
endkey: `user_chat_settings:${msgDoc.qr_code_uri}:${msgDoc.chat}:guest:\uffff`,
include_docs: true,
},
);
for (const { doc: userChatSettingDoc } of userChatSettingsRows) {
if (!userChatSettingDoc) {
continue;
}
if (!userChatSettingDoc.is_push_notification_enabled) {
console.log("PUSH NOTIFICATION DISABLED");
continue;
}
const { rows: subscriptionRows } =
await webPushSubscriptionsDb.allDocs({
startkey: `web_push_subscription:${userChatSettingDoc.user_uuid}:`,
endkey: `web_push_subscription:${userChatSettingDoc.user_uuid}:\uffff`,
include_docs: true,
});
if (subscriptionRows.length === 0) {
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)}`,
// );
// }
// }
}
} else {
continue;
}