2018-12-13 04:03:50 +11:00
|
|
|
import runtime from 'serviceworker-webpack-plugin/lib/runtime'
|
|
|
|
|
2018-12-07 00:34:00 +11:00
|
|
|
function urlBase64ToUint8Array (base64String) {
|
|
|
|
const padding = '='.repeat((4 - base64String.length % 4) % 4)
|
|
|
|
const base64 = (base64String + padding)
|
|
|
|
.replace(/-/g, '+')
|
|
|
|
.replace(/_/g, '/')
|
|
|
|
|
|
|
|
const rawData = window.atob(base64)
|
|
|
|
return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0)))
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPushSupported () {
|
|
|
|
return 'serviceWorker' in navigator && 'PushManager' in window
|
|
|
|
}
|
|
|
|
|
2018-12-25 11:46:19 +11:00
|
|
|
function getOrCreateServiceWorker () {
|
2018-12-13 04:03:50 +11:00
|
|
|
return runtime.register()
|
2018-12-25 11:46:19 +11:00
|
|
|
.catch((err) => console.error('Unable to get or create a service worker.', err))
|
2018-12-07 00:34:00 +11:00
|
|
|
}
|
|
|
|
|
2018-12-25 11:46:19 +11:00
|
|
|
function subscribePush (registration, isEnabled, vapidPublicKey) {
|
2018-12-09 23:25:43 +11:00
|
|
|
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'))
|
2018-12-07 18:57:35 +11:00
|
|
|
|
2018-12-07 00:34:00 +11:00
|
|
|
const subscribeOptions = {
|
|
|
|
userVisibleOnly: true,
|
2018-12-09 23:25:43 +11:00
|
|
|
applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
|
2018-12-07 00:34:00 +11:00
|
|
|
}
|
|
|
|
return registration.pushManager.subscribe(subscribeOptions)
|
|
|
|
}
|
|
|
|
|
2018-12-25 11:46:19 +11:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-09 23:25:43 +11:00
|
|
|
function sendSubscriptionToBackEnd (subscription, token) {
|
2018-12-07 00:34:00 +11:00
|
|
|
return window.fetch('/api/v1/push/subscription/', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2018-12-09 23:25:43 +11:00
|
|
|
'Authorization': `Bearer ${token}`
|
2018-12-07 00:34:00 +11:00
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
subscription,
|
|
|
|
data: {
|
|
|
|
alerts: {
|
|
|
|
follow: true,
|
|
|
|
favourite: true,
|
|
|
|
mention: true,
|
|
|
|
reblog: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2018-12-25 11:46:19 +11:00
|
|
|
}).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
|
2018-12-07 00:34:00 +11:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-20 17:17:59 +11:00
|
|
|
export function registerPushNotifications (isEnabled, vapidPublicKey, token) {
|
2018-12-07 00:34:00 +11:00
|
|
|
if (isPushSupported()) {
|
2018-12-25 11:46:19 +11:00
|
|
|
getOrCreateServiceWorker()
|
|
|
|
.then((registration) => subscribePush(registration, isEnabled, vapidPublicKey))
|
2018-12-13 22:04:09 +11:00
|
|
|
.then((subscription) => sendSubscriptionToBackEnd(subscription, token))
|
|
|
|
.catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
|
2018-12-07 00:34:00 +11:00
|
|
|
}
|
|
|
|
}
|
2018-12-20 17:17:59 +11:00
|
|
|
|
2018-12-25 11:46:19 +11:00
|
|
|
export function unregisterPushNotifications (token) {
|
2018-12-20 17:17:59 +11:00
|
|
|
if (isPushSupported()) {
|
2018-12-25 11:46:19 +11:00
|
|
|
Promise.all([
|
|
|
|
deleteSubscriptionFromBackEnd(token),
|
|
|
|
getOrCreateServiceWorker()
|
|
|
|
.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}`))
|
2018-12-20 17:17:59 +11:00
|
|
|
}
|
|
|
|
}
|