Unsubscribe client on too many failures
This commit is contained in:
parent
efad76855e
commit
2417354b31
|
@ -1,7 +1,9 @@
|
||||||
import registerEvents from 'retrospring/utilities/registerEvents';
|
import registerEvents from 'retrospring/utilities/registerEvents';
|
||||||
import { enableHandler } from './enable';
|
import { enableHandler } from './enable';
|
||||||
import { dismissHandler } from "./dismiss";
|
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 => {
|
export default (): void => {
|
||||||
const swCapable = 'serviceWorker' in navigator;
|
const swCapable = 'serviceWorker' in navigator;
|
||||||
|
@ -15,6 +17,11 @@ export default (): void => {
|
||||||
if (subscription) {
|
if (subscription) {
|
||||||
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
|
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
|
||||||
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
|
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
|
||||||
|
|
||||||
|
if (!subscriptionChecked) {
|
||||||
|
checkSubscription(subscription);
|
||||||
|
subscriptionChecked = true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
enableBtn?.classList.remove('d-none');
|
enableBtn?.classList.remove('d-none');
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { destroy } from '@rails/request.js';
|
import { post, destroy } from '@rails/request.js';
|
||||||
import { showErrorNotification, showNotification } from "utilities/notifications";
|
import { showErrorNotification, showNotification } from "utilities/notifications";
|
||||||
import I18n from "retrospring/i18n";
|
import I18n from "retrospring/i18n";
|
||||||
|
|
||||||
|
@ -7,13 +7,27 @@ export function unsubscribeHandler(): void {
|
||||||
.then(registration => registration.pushManager.getSubscription())
|
.then(registration => registration.pushManager.getSubscription())
|
||||||
.then(subscription => unsubscribeClient(subscription))
|
.then(subscription => unsubscribeClient(subscription))
|
||||||
.then(subscription => unsubscribeServer(subscription))
|
.then(subscription => unsubscribeServer(subscription))
|
||||||
.then()
|
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
showErrorNotification(I18n.translate("frontend.push_notifications.unsubscribe.error"));
|
showErrorNotification(I18n.translate("frontend.push_notifications.unsubscribe.error"));
|
||||||
console.error(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> {
|
async function unsubscribeClient(subscription?: PushSubscription): Promise<PushSubscription|null> {
|
||||||
subscription?.unsubscribe().then(success => {
|
subscription?.unsubscribe().then(success => {
|
||||||
if (!success) {
|
if (!success) {
|
||||||
|
|
Loading…
Reference in New Issue