More refactor

This commit is contained in:
binwiederhier 2023-06-08 23:09:38 -04:00
parent 9d38aeb863
commit 966ffe1669
8 changed files with 110 additions and 130 deletions

View file

@ -115,22 +115,22 @@ class Api {
throw new Error(`Unexpected server response ${response.status}`);
}
async updateWebPushSubscriptions(topics, browserSubscription) {
async updateWebPushSubscriptions(topics, pushSubscription) {
const user = await userManager.get(config.base_url);
const url = accountWebPushUrl(config.base_url);
console.log(`[Api] Sending Web Push Subscriptions`, { url, topics, endpoint: browserSubscription.endpoint });
const response = await fetch(url, {
console.log(`[Api] Sending Web Push Subscriptions`, { url, topics, endpoint: pushSubscription.endpoint });
console.log(`[Api] Sending Web Push Subscriptions`, { pushSubscription });
const serializedSubscription = JSON.parse(JSON.stringify(pushSubscription)); // Ugh ... https://stackoverflow.com/a/40525434/1440785
await fetchOrThrow(url, {
method: "PUT",
headers: maybeWithAuth({}, user),
body: JSON.stringify({ topics, browser_subscription: browserSubscription }),
body: JSON.stringify({
endpoint: serializedSubscription.endpoint,
auth: serializedSubscription.keys.auth,
p256dh: serializedSubscription.keys.p256dh,
topics
}),
});
if (response.ok) {
return true;
}
throw new Error(`Unexpected server response ${response.status}`);
}
}

View file

@ -58,7 +58,7 @@ class Notifier {
return existingSubscription;
}
// Create a new subscription only if web push is enabled.
// Create a new subscription only if web push is enabled.
// It is possible that web push was previously enabled and then disabled again
// in which case there would be an existingSubscription.
// but if it was _not_ enabled previously, we reach here, and only create a new
@ -76,11 +76,9 @@ class Notifier {
async pushManager() {
const registration = await navigator.serviceWorker.getRegistration();
if (!registration) {
throw new Error("No service worker registration found");
}
return registration.pushManager;
}