A platform for community discussion. Free, open, simple.
FIX: Bug in desktopNotifications service not allowing unsubscription (#26103)
Mark VanLandingham5f8cbb9d83ff
diff --git a/app/assets/javascripts/discourse/app/services/desktop-notifications.js b/app/assets/javascripts/discourse/app/services/desktop-notifications.jsindex 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";@disableImplicitInjectionsexport 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";