Skip to content

A platform for community discussion. Free, open, simple.

DEV: Return meaningful values from desktop noti enable/disable fns (#26061)

Mark VanLandingham4e80ad0724e7
diff --git a/app/assets/javascripts/discourse/app/lib/push-notifications.js b/app/assets/javascripts/discourse/app/lib/push-notifications.js
index 090212fea48..39655117da1 100644
--- a/app/assets/javascripts/discourse/app/lib/push-notifications.js
+++ b/app/assets/javascripts/discourse/app/lib/push-notifications.js
@@ -84,8 +84,8 @@ export function subscribe(callback, applicationServerKey) {
return;
}
- navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
- serviceWorkerRegistration.pushManager
+ return navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
+ return serviceWorkerRegistration.pushManager
.subscribe({
userVisibleOnly: true,
applicationServerKey: new Uint8Array(applicationServerKey.split("|")), // eslint-disable-line no-undef
@@ -95,10 +95,12 @@ export function subscribe(callback, applicationServerKey) {
if (callback) {
callback();
}
+ return true;
})
.catch((e) => {
// eslint-disable-next-line no-console
console.error(e);
+ return false;
});
});
}
@@ -109,7 +111,7 @@ export function unsubscribe(user, callback) {
}
keyValueStore.setItem(userSubscriptionKey(user), "");
- navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
+ return navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
serviceWorkerRegistration.pushManager
.getSubscription()
.then((subscription) => {
@@ -132,5 +134,6 @@ export function unsubscribe(user, callback) {
if (callback) {
callback();
}
+ return true;
});
}
diff --git a/app/assets/javascripts/discourse/app/services/desktop-notifications.js b/app/assets/javascripts/discourse/app/services/desktop-notifications.js
index aeb894622f5..8624c6d1d07 100644
--- a/app/assets/javascripts/discourse/app/services/desktop-notifications.js
+++ b/app/assets/javascripts/discourse/app/services/desktop-notifications.js
@@ -119,9 +119,10 @@ export default class DesktopNotificationsService extends Service {
disable() {
if (this.isEnabledDesktop) {
this.setNotificationsDisabled("disabled");
+ return true;
}
if (this.isEnabledPush) {
- unsubscribePushNotification(this.currentUser, () => {
+ return unsubscribePushNotification(this.currentUser, () => {
this.setIsEnabledPush("");
});
}
@@ -130,13 +131,14 @@ export default class DesktopNotificationsService extends Service {
@action
enable() {
if (this.isPushNotificationsPreferred) {
- subscribePushNotification(() => {
+ return subscribePushNotification(() => {
this.setIsEnabledPush("subscribed");
}, this.siteSettings.vapid_public_key_bytes);
} else {
this.setNotificationsDisabled("");
- Notification.requestPermission(() => {
+ return Notification.requestPermission((permission) => {
confirmNotification(this.siteSettings);
+ return permission === "granted";
});
}
}