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

This commit is contained in:
Mark VanLandingham 2024-03-06 11:31:42 -06:00 committed by GitHub
parent 5d90332cfc
commit 4e80ad0724
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 6 deletions

View File

@ -84,8 +84,8 @@ export function subscribe(callback, applicationServerKey) {
return; return;
} }
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => { return navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
serviceWorkerRegistration.pushManager return serviceWorkerRegistration.pushManager
.subscribe({ .subscribe({
userVisibleOnly: true, userVisibleOnly: true,
applicationServerKey: new Uint8Array(applicationServerKey.split("|")), // eslint-disable-line no-undef applicationServerKey: new Uint8Array(applicationServerKey.split("|")), // eslint-disable-line no-undef
@ -95,10 +95,12 @@ export function subscribe(callback, applicationServerKey) {
if (callback) { if (callback) {
callback(); callback();
} }
return true;
}) })
.catch((e) => { .catch((e) => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(e); console.error(e);
return false;
}); });
}); });
} }
@ -109,7 +111,7 @@ export function unsubscribe(user, callback) {
} }
keyValueStore.setItem(userSubscriptionKey(user), ""); keyValueStore.setItem(userSubscriptionKey(user), "");
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => { return navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
serviceWorkerRegistration.pushManager serviceWorkerRegistration.pushManager
.getSubscription() .getSubscription()
.then((subscription) => { .then((subscription) => {
@ -132,5 +134,6 @@ export function unsubscribe(user, callback) {
if (callback) { if (callback) {
callback(); callback();
} }
return true;
}); });
} }

View File

@ -119,9 +119,10 @@ export default class DesktopNotificationsService extends Service {
disable() { disable() {
if (this.isEnabledDesktop) { if (this.isEnabledDesktop) {
this.setNotificationsDisabled("disabled"); this.setNotificationsDisabled("disabled");
return true;
} }
if (this.isEnabledPush) { if (this.isEnabledPush) {
unsubscribePushNotification(this.currentUser, () => { return unsubscribePushNotification(this.currentUser, () => {
this.setIsEnabledPush(""); this.setIsEnabledPush("");
}); });
} }
@ -130,13 +131,14 @@ export default class DesktopNotificationsService extends Service {
@action @action
enable() { enable() {
if (this.isPushNotificationsPreferred) { if (this.isPushNotificationsPreferred) {
subscribePushNotification(() => { return subscribePushNotification(() => {
this.setIsEnabledPush("subscribed"); this.setIsEnabledPush("subscribed");
}, this.siteSettings.vapid_public_key_bytes); }, this.siteSettings.vapid_public_key_bytes);
} else { } else {
this.setNotificationsDisabled(""); this.setNotificationsDisabled("");
Notification.requestPermission(() => { return Notification.requestPermission((permission) => {
confirmNotification(this.siteSettings); confirmNotification(this.siteSettings);
return permission === "granted";
}); });
} }
} }