FIX: Bug in desktopNotifications service not allowing unsubscription (#26103)

This commit is contained in:
Mark VanLandingham 2024-03-08 13:53:16 -06:00 committed by GitHub
parent 4c7d58a883
commit 5f8cbb9d83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -16,6 +16,8 @@ import {
} from "discourse/lib/push-notifications"; } from "discourse/lib/push-notifications";
const keyValueStore = new KeyValueStore(context); const keyValueStore = new KeyValueStore(context);
const DISABLED = "disabled";
const ENABLED = "enabled";
@disableImplicitInjections @disableImplicitInjections
export default class DesktopNotificationsService extends Service { export default class DesktopNotificationsService extends Service {
@ -28,9 +30,8 @@ export default class DesktopNotificationsService extends Service {
constructor() { constructor() {
super(...arguments); super(...arguments);
this.notificationsDisabled = keyValueStore.getItem( this.notificationsDisabled =
"notifications-disabled" keyValueStore.getItem("notifications-disabled") === DISABLED;
);
this.isEnabledPush = this.currentUser this.isEnabledPush = this.currentUser
? pushNotificationKeyValueStore.getItem( ? pushNotificationKeyValueStore.getItem(
pushNotificationUserSubscriptionKey(this.currentUser) pushNotificationUserSubscriptionKey(this.currentUser)
@ -48,9 +49,7 @@ export default class DesktopNotificationsService extends Service {
setNotificationsDisabled(value) { setNotificationsDisabled(value) {
keyValueStore.setItem("notifications-disabled", value); keyValueStore.setItem("notifications-disabled", value);
this.notificationsDisabled = keyValueStore.getItem( this.notificationsDisabled = value === DISABLED;
"notifications-disabled"
);
} }
get isDeniedPermission() { get isDeniedPermission() {
@ -71,7 +70,7 @@ export default class DesktopNotificationsService extends Service {
get isEnabledDesktop() { get isEnabledDesktop() {
if (this.isGrantedPermission) { if (this.isGrantedPermission) {
return this.notificationsDisabled; return !this.notificationsDisabled;
} }
return false; return false;
@ -103,7 +102,7 @@ export default class DesktopNotificationsService extends Service {
if (this.isPushNotificationsPreferred) { if (this.isPushNotificationsPreferred) {
return this.isEnabledPush === "subscribed"; return this.isEnabledPush === "subscribed";
} else { } else {
return this.notificationsDisabled === ""; return !this.notificationsDisabled;
} }
} }
@ -116,16 +115,17 @@ export default class DesktopNotificationsService extends Service {
} }
@action @action
disable() { async disable() {
if (this.isEnabledDesktop) { if (this.isEnabledDesktop) {
this.setNotificationsDisabled("disabled"); this.setNotificationsDisabled(DISABLED);
return true;
} }
if (this.isEnabledPush) { if (this.isEnabledPush) {
return unsubscribePushNotification(this.currentUser, () => { await unsubscribePushNotification(this.currentUser, () => {
this.setIsEnabledPush(""); this.setIsEnabledPush("");
}); });
} }
return true;
} }
@action @action
@ -135,7 +135,7 @@ export default class DesktopNotificationsService extends Service {
this.setIsEnabledPush("subscribed"); this.setIsEnabledPush("subscribed");
}, this.siteSettings.vapid_public_key_bytes); }, this.siteSettings.vapid_public_key_bytes);
} else { } else {
this.setNotificationsDisabled(""); this.setNotificationsDisabled(ENABLED);
return Notification.requestPermission((permission) => { return Notification.requestPermission((permission) => {
confirmNotification(this.siteSettings); confirmNotification(this.siteSettings);
return permission === "granted"; return permission === "granted";