1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-09-27 19:12:00 +02:00

Remove webPushEndpoint from indexeddb

Rely directly on getting it from the browser
This commit is contained in:
nimbleghost 2023-05-31 21:08:46 +02:00
parent 7aa3d8f59b
commit 4944e3ae4b
3 changed files with 28 additions and 32 deletions

View file

@ -134,7 +134,7 @@ class Api {
throw new Error(`Unexpected server response ${response.status}`); throw new Error(`Unexpected server response ${response.status}`);
} }
async unsubscribeWebPush(subscription) { async unsubscribeWebPush(subscription, browserSubscription) {
const user = await userManager.get(subscription.baseUrl); const user = await userManager.get(subscription.baseUrl);
const url = topicUrlWebPushUnsubscribe(subscription.baseUrl, subscription.topic); const url = topicUrlWebPushUnsubscribe(subscription.baseUrl, subscription.topic);
@ -144,7 +144,7 @@ class Api {
method: "POST", method: "POST",
headers: maybeWithAuth({}, user), headers: maybeWithAuth({}, user),
body: JSON.stringify({ body: JSON.stringify({
endpoint: subscription.webPushEndpoint, endpoint: browserSubscription.endpoint,
}), }),
}); });

View file

@ -47,9 +47,14 @@ class Notifier {
async unsubscribeWebPush(subscription) { async unsubscribeWebPush(subscription) {
try { try {
await api.unsubscribeWebPush(subscription); const pushManager = await this.pushManager();
const browserSubscription = await pushManager.getSubscription();
if (!browserSubscription) {
throw new Error("No browser subscription found");
}
await api.unsubscribeWebPush(subscription, browserSubscription);
} catch (e) { } catch (e) {
console.error("[Notifier.subscribeWebPush] Error subscribing to web push", e); console.error("[Notifier] Error unsubscribing from web push", e);
} }
} }
@ -64,21 +69,15 @@ class Notifier {
return {}; return {};
} }
const registration = await navigator.serviceWorker.getRegistration();
if (!registration) {
console.log("[Notifier.subscribeWebPush] Web push supported but no service worker registration found, skipping");
return {};
}
try { try {
const browserSubscription = await registration.pushManager.subscribe({ const pushManager = await this.pushManager();
const browserSubscription = await pushManager.subscribe({
userVisibleOnly: true, userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array(config.web_push_public_key), applicationServerKey: urlB64ToUint8Array(config.web_push_public_key),
}); });
await api.subscribeWebPush(baseUrl, topic, browserSubscription); await api.subscribeWebPush(baseUrl, topic, browserSubscription);
console.log("[Notifier.subscribeWebPush] Successfully subscribed to web push"); console.log("[Notifier.subscribeWebPush] Successfully subscribed to web push");
return browserSubscription;
} catch (e) { } catch (e) {
console.error("[Notifier.subscribeWebPush] Error subscribing to web push", e); console.error("[Notifier.subscribeWebPush] Error subscribing to web push", e);
} }
@ -86,6 +85,16 @@ class Notifier {
return {}; return {};
} }
async pushManager() {
const registration = await navigator.serviceWorker.getRegistration();
if (!registration) {
throw new Error("No service worker registration found");
}
return registration.pushManager;
}
granted() { granted() {
return this.supported() && Notification.permission === "granted"; return this.supported() && Notification.permission === "granted";
} }

View file

@ -68,16 +68,12 @@ class SubscriptionManager {
async add(baseUrl, topic, opts = {}) { async add(baseUrl, topic, opts = {}) {
const id = topicUrl(baseUrl, topic); const id = topicUrl(baseUrl, topic);
const webPushFields = opts.notificationType === "background" ? await notifier.subscribeWebPush(baseUrl, topic) : {}; if (opts.notificationType === "background") {
await notifier.subscribeWebPush(baseUrl, topic);
}
const existingSubscription = await this.get(id); const existingSubscription = await this.get(id);
if (existingSubscription) { if (existingSubscription) {
if (webPushFields.endpoint) {
await this.db.subscriptions.update(existingSubscription.id, {
webPushEndpoint: webPushFields.endpoint,
});
}
return existingSubscription; return existingSubscription;
} }
@ -88,7 +84,6 @@ class SubscriptionManager {
mutedUntil: 0, mutedUntil: 0,
last: null, last: null,
...opts, ...opts,
webPushEndpoint: webPushFields.endpoint,
}; };
await this.db.subscriptions.put(subscription); await this.db.subscriptions.put(subscription);
@ -139,7 +134,7 @@ class SubscriptionManager {
await this.db.subscriptions.delete(subscription.id); await this.db.subscriptions.delete(subscription.id);
await this.db.notifications.where({ subscriptionId: subscription.id }).delete(); await this.db.notifications.where({ subscriptionId: subscription.id }).delete();
if (subscription.webPushEndpoint) { if (subscription.notificationType === NotificationType.BACKGROUND) {
await notifier.unsubscribeWebPush(subscription); await notifier.unsubscribeWebPush(subscription);
} }
} }
@ -240,10 +235,7 @@ class SubscriptionManager {
if (mutedUntil === 1) { if (mutedUntil === 1) {
await notifier.unsubscribeWebPush(subscription); await notifier.unsubscribeWebPush(subscription);
} else { } else {
const webPushFields = await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic); await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic);
await this.db.subscriptions.update(subscriptionId, {
webPushEndpoint: webPushFields.endpoint,
});
} }
} }
} }
@ -261,19 +253,14 @@ class SubscriptionManager {
return; return;
} }
let { webPushEndpoint } = subscription;
if (oldNotificationType === "background") { if (oldNotificationType === "background") {
await notifier.unsubscribeWebPush(subscription); await notifier.unsubscribeWebPush(subscription);
webPushEndpoint = undefined;
} else if (newNotificationType === "background") { } else if (newNotificationType === "background") {
const webPushFields = await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic); await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic);
webPushEndpoint = webPushFields.webPushEndpoint;
} }
await this.db.subscriptions.update(subscription.id, { await this.db.subscriptions.update(subscription.id, {
notificationType: newNotificationType, notificationType: newNotificationType,
webPushEndpoint,
}); });
} }