Unsubscribe client on too many failures

This commit is contained in:
Karina Kwiatek 2023-01-01 22:06:01 +01:00
parent efad76855e
commit 2417354b31
2 changed files with 24 additions and 3 deletions

View File

@ -1,7 +1,9 @@
import registerEvents from 'retrospring/utilities/registerEvents';
import { enableHandler } from './enable';
import { dismissHandler } from "./dismiss";
import { unsubscribeHandler } from "retrospring/features/webpush/unsubscribe";
import { unsubscribeHandler, checkSubscription } from "retrospring/features/webpush/unsubscribe";
let subscriptionChecked = false;
export default (): void => {
const swCapable = 'serviceWorker' in navigator;
@ -15,6 +17,11 @@ export default (): void => {
if (subscription) {
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
if (!subscriptionChecked) {
checkSubscription(subscription);
subscriptionChecked = true;
}
} else {
enableBtn?.classList.remove('d-none');

View File

@ -1,4 +1,4 @@
import { destroy } from '@rails/request.js';
import { post, destroy } from '@rails/request.js';
import { showErrorNotification, showNotification } from "utilities/notifications";
import I18n from "retrospring/i18n";
@ -7,13 +7,27 @@ export function unsubscribeHandler(): void {
.then(registration => registration.pushManager.getSubscription())
.then(subscription => unsubscribeClient(subscription))
.then(subscription => unsubscribeServer(subscription))
.then()
.catch(error => {
showErrorNotification(I18n.translate("frontend.push_notifications.unsubscribe.error"));
console.error(error);
});
}
export function checkSubscription(subscription: PushSubscription): void {
post('/ajax/webpush/check', {
body: {
endpoint: subscription.endpoint
},
contentType: 'application/json'
}).then(async response => {
const data = await response.json();
if (data.status == 'subscribed') return;
if (data.status == 'failed') await unsubscribeServer(subscription);
await unsubscribeClient(subscription);
})
}
async function unsubscribeClient(subscription?: PushSubscription): Promise<PushSubscription|null> {
subscription?.unsubscribe().then(success => {
if (!success) {