From 5f8cbb9d83ffd1d06d0805341770f23ecb2f1c2b Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Fri, 8 Mar 2024 13:53:16 -0600 Subject: [PATCH] FIX: Bug in desktopNotifications service not allowing unsubscription (#26103) --- .../app/services/desktop-notifications.js | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/app/services/desktop-notifications.js b/app/assets/javascripts/discourse/app/services/desktop-notifications.js index 8624c6d1d07..854bc945ea3 100644 --- a/app/assets/javascripts/discourse/app/services/desktop-notifications.js +++ b/app/assets/javascripts/discourse/app/services/desktop-notifications.js @@ -16,6 +16,8 @@ import { } from "discourse/lib/push-notifications"; const keyValueStore = new KeyValueStore(context); +const DISABLED = "disabled"; +const ENABLED = "enabled"; @disableImplicitInjections export default class DesktopNotificationsService extends Service { @@ -28,9 +30,8 @@ export default class DesktopNotificationsService extends Service { constructor() { super(...arguments); - this.notificationsDisabled = keyValueStore.getItem( - "notifications-disabled" - ); + this.notificationsDisabled = + keyValueStore.getItem("notifications-disabled") === DISABLED; this.isEnabledPush = this.currentUser ? pushNotificationKeyValueStore.getItem( pushNotificationUserSubscriptionKey(this.currentUser) @@ -48,9 +49,7 @@ export default class DesktopNotificationsService extends Service { setNotificationsDisabled(value) { keyValueStore.setItem("notifications-disabled", value); - this.notificationsDisabled = keyValueStore.getItem( - "notifications-disabled" - ); + this.notificationsDisabled = value === DISABLED; } get isDeniedPermission() { @@ -71,7 +70,7 @@ export default class DesktopNotificationsService extends Service { get isEnabledDesktop() { if (this.isGrantedPermission) { - return this.notificationsDisabled; + return !this.notificationsDisabled; } return false; @@ -103,7 +102,7 @@ export default class DesktopNotificationsService extends Service { if (this.isPushNotificationsPreferred) { return this.isEnabledPush === "subscribed"; } else { - return this.notificationsDisabled === ""; + return !this.notificationsDisabled; } } @@ -116,16 +115,17 @@ export default class DesktopNotificationsService extends Service { } @action - disable() { + async disable() { if (this.isEnabledDesktop) { - this.setNotificationsDisabled("disabled"); - return true; + this.setNotificationsDisabled(DISABLED); } if (this.isEnabledPush) { - return unsubscribePushNotification(this.currentUser, () => { + await unsubscribePushNotification(this.currentUser, () => { this.setIsEnabledPush(""); }); } + + return true; } @action @@ -135,7 +135,7 @@ export default class DesktopNotificationsService extends Service { this.setIsEnabledPush("subscribed"); }, this.siteSettings.vapid_public_key_bytes); } else { - this.setNotificationsDisabled(""); + this.setNotificationsDisabled(ENABLED); return Notification.requestPermission((permission) => { confirmNotification(this.siteSettings); return permission === "granted";