From 2f5acee79899d68975755dba7d5943a743a5d919 Mon Sep 17 00:00:00 2001 From: nimbleghost <132819643+nimbleghost@users.noreply.github.com> Date: Thu, 8 Jun 2023 10:55:11 +0200 Subject: [PATCH] Call pushManager.subscribe only if enabled --- web/src/app/Notifier.js | 23 ++++++++++++++++++----- web/src/app/SubscriptionManager.js | 9 ++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/web/src/app/Notifier.js b/web/src/app/Notifier.js index ddf68f5b..b157ef46 100644 --- a/web/src/app/Notifier.js +++ b/web/src/app/Notifier.js @@ -55,13 +55,26 @@ class Notifier { const pushManager = await this.pushManager(); - return ( - (await pushManager.getSubscription()) ?? - pushManager.subscribe({ + const existingSubscription = await pushManager.getSubscription(); + + if (existingSubscription) { + return existingSubscription; + } + + // create a new subscription only if web push is enabled + // it is possible that web push was previously enabled and then disabled again + // in which case there would be an existingSubscription. + // but if it was _not_ enabled previously, we reach here, and only create a new + // subscription if it is now enabled. + + if (await this.pushEnabled()) { + return pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlB64ToUint8Array(config.web_push_public_key), - }) - ); + }); + } + + return undefined; } async pushManager() { diff --git a/web/src/app/SubscriptionManager.js b/web/src/app/SubscriptionManager.js index 1521aedf..74488bc0 100644 --- a/web/src/app/SubscriptionManager.js +++ b/web/src/app/SubscriptionManager.js @@ -114,7 +114,14 @@ class SubscriptionManager { async refreshWebPushSubscriptions(presetTopics) { const topics = presetTopics ?? (await this.webPushTopics()); - await api.updateWebPushSubscriptions(topics, await notifier.getBrowserSubscription()); + const browserSubscription = await notifier.getBrowserSubscription(); + + if (!browserSubscription) { + console.log("[SubscriptionManager] No browser subscription currently exists, so web push was never enabled. Skipping."); + return; + } + + await api.updateWebPushSubscriptions(topics, browserSubscription); } async updateState(subscriptionId, state) {