forked from AkkomaGang/akkoma-fe
small refactor, added push unsub notice for BE
This commit is contained in:
parent
957b2a6f7e
commit
a4f0902926
2 changed files with 56 additions and 25 deletions
|
@ -117,7 +117,9 @@ const users = {
|
||||||
registerPushNotifications(isEnabled, vapidPublicKey, token)
|
registerPushNotifications(isEnabled, vapidPublicKey, token)
|
||||||
},
|
},
|
||||||
unregisterPushNotifications (store) {
|
unregisterPushNotifications (store) {
|
||||||
unregisterPushNotifications()
|
const token = store.state.currentUser.credentials
|
||||||
|
|
||||||
|
unregisterPushNotifications(token)
|
||||||
},
|
},
|
||||||
addNewStatuses (store, { statuses }) {
|
addNewStatuses (store, { statuses }) {
|
||||||
const users = map(statuses, 'user')
|
const users = map(statuses, 'user')
|
||||||
|
|
|
@ -14,18 +14,12 @@ function isPushSupported () {
|
||||||
return 'serviceWorker' in navigator && 'PushManager' in window
|
return 'serviceWorker' in navigator && 'PushManager' in window
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerServiceWorker () {
|
function getOrCreateServiceWorker () {
|
||||||
return runtime.register()
|
return runtime.register()
|
||||||
.catch((err) => console.error('Unable to register service worker.', err))
|
.catch((err) => console.error('Unable to get or create a service worker.', err))
|
||||||
}
|
}
|
||||||
|
|
||||||
function unregisterServiceWorker () {
|
function subscribePush (registration, isEnabled, vapidPublicKey) {
|
||||||
return runtime.register()
|
|
||||||
.then((registration) => registration.unregister())
|
|
||||||
.catch((err) => console.error('Unable to unregister serviceworker', err))
|
|
||||||
}
|
|
||||||
|
|
||||||
function subscribe (registration, isEnabled, vapidPublicKey) {
|
|
||||||
if (!isEnabled) return Promise.reject(new Error('Web Push is disabled in config'))
|
if (!isEnabled) return Promise.reject(new Error('Web Push is disabled in config'))
|
||||||
if (!vapidPublicKey) return Promise.reject(new Error('VAPID public key is not found'))
|
if (!vapidPublicKey) return Promise.reject(new Error('VAPID public key is not found'))
|
||||||
|
|
||||||
|
@ -36,6 +30,30 @@ function subscribe (registration, isEnabled, vapidPublicKey) {
|
||||||
return registration.pushManager.subscribe(subscribeOptions)
|
return registration.pushManager.subscribe(subscribeOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function unsubscribePush (registration) {
|
||||||
|
return registration.pushManager.getSubscription()
|
||||||
|
.then((subscribtion) => {
|
||||||
|
if (subscribtion === null) { return }
|
||||||
|
return subscribtion.unsubscribe()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteSubscriptionFromBackEnd (token) {
|
||||||
|
return window.fetch('/api/v1/push/subscription/', {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
}).then((response) => {
|
||||||
|
if (!response.ok) throw new Error('Bad status code from server.')
|
||||||
|
return response.json()
|
||||||
|
}).then((responseData) => {
|
||||||
|
if (!responseData.id) throw new Error('Bad response from server.')
|
||||||
|
return responseData
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function sendSubscriptionToBackEnd (subscription, token) {
|
function sendSubscriptionToBackEnd (subscription, token) {
|
||||||
return window.fetch('/api/v1/push/subscription/', {
|
return window.fetch('/api/v1/push/subscription/', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -54,12 +72,10 @@ function sendSubscriptionToBackEnd (subscription, token) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
}).then((response) => {
|
||||||
.then((response) => {
|
|
||||||
if (!response.ok) throw new Error('Bad status code from server.')
|
if (!response.ok) throw new Error('Bad status code from server.')
|
||||||
return response.json()
|
return response.json()
|
||||||
})
|
}).then((responseData) => {
|
||||||
.then((responseData) => {
|
|
||||||
if (!responseData.id) throw new Error('Bad response from server.')
|
if (!responseData.id) throw new Error('Bad response from server.')
|
||||||
return responseData
|
return responseData
|
||||||
})
|
})
|
||||||
|
@ -67,18 +83,31 @@ function sendSubscriptionToBackEnd (subscription, token) {
|
||||||
|
|
||||||
export function registerPushNotifications (isEnabled, vapidPublicKey, token) {
|
export function registerPushNotifications (isEnabled, vapidPublicKey, token) {
|
||||||
if (isPushSupported()) {
|
if (isPushSupported()) {
|
||||||
registerServiceWorker()
|
getOrCreateServiceWorker()
|
||||||
.then((registration) => subscribe(registration, isEnabled, vapidPublicKey))
|
.then((registration) => subscribePush(registration, isEnabled, vapidPublicKey))
|
||||||
.then((subscription) => sendSubscriptionToBackEnd(subscription, token))
|
.then((subscription) => sendSubscriptionToBackEnd(subscription, token))
|
||||||
.catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
|
.catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unregisterPushNotifications (isEnabled, vapidPublicKey, token) {
|
export function unregisterPushNotifications (token) {
|
||||||
if (isPushSupported()) {
|
if (isPushSupported()) {
|
||||||
unregisterServiceWorker()
|
Promise.all([
|
||||||
.then((registration) => subscribe(registration, isEnabled, vapidPublicKey))
|
deleteSubscriptionFromBackEnd(token),
|
||||||
.then((subscription) => sendSubscriptionToBackEnd(subscription, token))
|
getOrCreateServiceWorker()
|
||||||
.catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
|
.then((registration) => {
|
||||||
|
return unsubscribePush(registration).then((result) => [registration, result])
|
||||||
|
})
|
||||||
|
.then(([registration, unsubResult]) => {
|
||||||
|
if (!unsubResult) {
|
||||||
|
console.warn('Push subscription cancellation wasn\'t successful, killing SW anyway...')
|
||||||
|
}
|
||||||
|
return registration.unregister().then((result) => {
|
||||||
|
if (!result) {
|
||||||
|
console.warn('Failed to kill SW')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
]).catch((e) => console.warn(`Failed to disable Web Push Notifications: ${e.message}`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue