mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-11-22 03:13:33 +01:00
Remove webPushEndpoint from indexeddb
Rely directly on getting it from the browser
This commit is contained in:
parent
7aa3d8f59b
commit
4944e3ae4b
3 changed files with 28 additions and 32 deletions
|
@ -134,7 +134,7 @@ class Api {
|
|||
throw new Error(`Unexpected server response ${response.status}`);
|
||||
}
|
||||
|
||||
async unsubscribeWebPush(subscription) {
|
||||
async unsubscribeWebPush(subscription, browserSubscription) {
|
||||
const user = await userManager.get(subscription.baseUrl);
|
||||
|
||||
const url = topicUrlWebPushUnsubscribe(subscription.baseUrl, subscription.topic);
|
||||
|
@ -144,7 +144,7 @@ class Api {
|
|||
method: "POST",
|
||||
headers: maybeWithAuth({}, user),
|
||||
body: JSON.stringify({
|
||||
endpoint: subscription.webPushEndpoint,
|
||||
endpoint: browserSubscription.endpoint,
|
||||
}),
|
||||
});
|
||||
|
||||
|
|
|
@ -47,9 +47,14 @@ class Notifier {
|
|||
|
||||
async unsubscribeWebPush(subscription) {
|
||||
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) {
|
||||
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 {};
|
||||
}
|
||||
|
||||
const registration = await navigator.serviceWorker.getRegistration();
|
||||
|
||||
if (!registration) {
|
||||
console.log("[Notifier.subscribeWebPush] Web push supported but no service worker registration found, skipping");
|
||||
return {};
|
||||
}
|
||||
|
||||
try {
|
||||
const browserSubscription = await registration.pushManager.subscribe({
|
||||
const pushManager = await this.pushManager();
|
||||
const browserSubscription = await pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: urlB64ToUint8Array(config.web_push_public_key),
|
||||
});
|
||||
|
||||
await api.subscribeWebPush(baseUrl, topic, browserSubscription);
|
||||
console.log("[Notifier.subscribeWebPush] Successfully subscribed to web push");
|
||||
return browserSubscription;
|
||||
} catch (e) {
|
||||
console.error("[Notifier.subscribeWebPush] Error subscribing to web push", e);
|
||||
}
|
||||
|
@ -86,6 +85,16 @@ class Notifier {
|
|||
return {};
|
||||
}
|
||||
|
||||
async pushManager() {
|
||||
const registration = await navigator.serviceWorker.getRegistration();
|
||||
|
||||
if (!registration) {
|
||||
throw new Error("No service worker registration found");
|
||||
}
|
||||
|
||||
return registration.pushManager;
|
||||
}
|
||||
|
||||
granted() {
|
||||
return this.supported() && Notification.permission === "granted";
|
||||
}
|
||||
|
|
|
@ -68,16 +68,12 @@ class SubscriptionManager {
|
|||
async add(baseUrl, topic, opts = {}) {
|
||||
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);
|
||||
if (existingSubscription) {
|
||||
if (webPushFields.endpoint) {
|
||||
await this.db.subscriptions.update(existingSubscription.id, {
|
||||
webPushEndpoint: webPushFields.endpoint,
|
||||
});
|
||||
}
|
||||
|
||||
return existingSubscription;
|
||||
}
|
||||
|
||||
|
@ -88,7 +84,6 @@ class SubscriptionManager {
|
|||
mutedUntil: 0,
|
||||
last: null,
|
||||
...opts,
|
||||
webPushEndpoint: webPushFields.endpoint,
|
||||
};
|
||||
|
||||
await this.db.subscriptions.put(subscription);
|
||||
|
@ -139,7 +134,7 @@ class SubscriptionManager {
|
|||
await this.db.subscriptions.delete(subscription.id);
|
||||
await this.db.notifications.where({ subscriptionId: subscription.id }).delete();
|
||||
|
||||
if (subscription.webPushEndpoint) {
|
||||
if (subscription.notificationType === NotificationType.BACKGROUND) {
|
||||
await notifier.unsubscribeWebPush(subscription);
|
||||
}
|
||||
}
|
||||
|
@ -240,10 +235,7 @@ class SubscriptionManager {
|
|||
if (mutedUntil === 1) {
|
||||
await notifier.unsubscribeWebPush(subscription);
|
||||
} else {
|
||||
const webPushFields = await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic);
|
||||
await this.db.subscriptions.update(subscriptionId, {
|
||||
webPushEndpoint: webPushFields.endpoint,
|
||||
});
|
||||
await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -261,19 +253,14 @@ class SubscriptionManager {
|
|||
return;
|
||||
}
|
||||
|
||||
let { webPushEndpoint } = subscription;
|
||||
|
||||
if (oldNotificationType === "background") {
|
||||
await notifier.unsubscribeWebPush(subscription);
|
||||
webPushEndpoint = undefined;
|
||||
} else if (newNotificationType === "background") {
|
||||
const webPushFields = await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic);
|
||||
webPushEndpoint = webPushFields.webPushEndpoint;
|
||||
await notifier.subscribeWebPush(subscription.baseUrl, subscription.topic);
|
||||
}
|
||||
|
||||
await this.db.subscriptions.update(subscription.id, {
|
||||
notificationType: newNotificationType,
|
||||
webPushEndpoint,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue