From 2cd8bf62a9b9d62cf386be031b01179d2abb892d Mon Sep 17 00:00:00 2001 From: Vasilii Mikhailovskii Date: Sun, 24 Nov 2024 13:02:27 +0200 Subject: [PATCH] add generate-keys --- .../generate-keys.js | 13 +++++++-- .../test-send-notification.js | 27 +++++++++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/services/message-delivery-method-web-push/generate-keys.js b/services/message-delivery-method-web-push/generate-keys.js index dc91d26..85251a3 100644 --- a/services/message-delivery-method-web-push/generate-keys.js +++ b/services/message-delivery-method-web-push/generate-keys.js @@ -1,5 +1,14 @@ -import webpush from 'web-push'; +import webpush from "web-push"; +import PouchDB from "./PouchDB.js"; const vapidKeys = webpush.generateVAPIDKeys(); -console.log(vapidKeys) +await new PouchDB(`${process.env.API_URL}/server_settings`, { + skip_setup: true, +}).put({ + _id: "server_settings:message_delivery_method_web_push:vapid", + vapid: { + subject: process.env.VAPID_SUBJECT, + keys: vapidKeys, + }, +}); diff --git a/services/message-delivery-method-web-push/test-send-notification.js b/services/message-delivery-method-web-push/test-send-notification.js index 3705215..8609e43 100644 --- a/services/message-delivery-method-web-push/test-send-notification.js +++ b/services/message-delivery-method-web-push/test-send-notification.js @@ -37,16 +37,27 @@ const notification = { const { rows } = await new PouchDB( `${process.env.API_URL}/web_push_subscriptions`, { skip_setup: true }, -).allDocs({ - startkey: "web_push_subscription:", - endkey: "web_push_subscription:\uFFFF", - include_docs: true, -}); +).allDocs( + process.env.WEB_PUSH_SUBSCRIPTION_ID + ? { + key: process.env.WEB_PUSH_SUBSCRIPTION_ID, + include_docs: true, + } + : { + startkey: "web_push_subscription:", + endkey: "web_push_subscription:\uFFFF", + include_docs: true, + }, +); await Promise.allSettled( - rows.map(({ doc }) => - webpush.sendNotification(doc.subscription, JSON.stringify(notification)), - ), + rows.map(async ({ doc }) => { + await webpush.sendNotification( + doc.subscription, + JSON.stringify(notification), + ); + console.log("sent ", doc._id); + }), ); // issues: