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
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerServiceWorker () {
|
|
|
|
return navigator.serviceWorker.register('/static/sw.js')
|
|
|
|
.then(function (registration) {
|
|
|
|
console.log('Service worker successfully registered.')
|
|
|
|
return registration
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
console.error('Unable to register service worker.', err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function askPermission () {
|
|
|
|
return new Promise(function (resolve, reject) {
|
2018-12-07 18:57:35 +11:00
|
|
|
if (!window.Notification) return reject(new Error('Notifications disabled'))
|
|
|
|
|
|
|
|
if (window.Notification.permission !== 'default') {
|
|
|
|
return resolve(window.Notification.permission)
|
|
|
|
}
|
2018-12-07 00:34:00 +11:00
|
|
|
|
|
|
|
const permissionResult = window.Notification.requestPermission(function (result) {
|
|
|
|
resolve(result)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (permissionResult) permissionResult.then(resolve, reject)
|
|
|
|
}).then(function (permissionResult) {
|
|
|
|
if (permissionResult !== 'granted') {
|
|
|
|
throw new Error('We weren\'t granted permission.')
|
|
|
|
}
|
|
|
|
return permissionResult
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function subscribe (registration, store) {
|
2018-12-07 22:53:40 +11:00
|
|
|
if (!store.rootState.config.webPushNotifications) {
|
2018-12-07 19:15:31 +11:00
|
|
|
return Promise.reject(new Error('Web Push is disabled in config'))
|
|
|
|
}
|
|
|
|
|
2018-12-07 22:53:40 +11:00
|
|
|
if (!store.rootState.instance.vapidPublicKey) {
|
2018-12-07 19:21:19 +11:00
|
|
|
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-07 22:53:40 +11:00
|
|
|
applicationServerKey: urlBase64ToUint8Array(store.rootState.instance.vapidPublicKey)
|
2018-12-07 00:34:00 +11:00
|
|
|
}
|
|
|
|
return registration.pushManager.subscribe(subscribeOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendSubscriptionToBackEnd (subscription, store) {
|
|
|
|
return window.fetch('/api/v1/push/subscription/', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2018-12-07 22:53:40 +11:00
|
|
|
'Authorization': `Bearer ${store.rootState.oauth.token}`
|
2018-12-07 00:34:00 +11:00
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
subscription,
|
|
|
|
data: {
|
|
|
|
alerts: {
|
|
|
|
follow: true,
|
|
|
|
favourite: true,
|
|
|
|
mention: true,
|
|
|
|
reblog: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(function (response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Bad status code from server.')
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.json()
|
|
|
|
})
|
|
|
|
.then(function (responseData) {
|
|
|
|
if (!responseData.id) {
|
|
|
|
throw new Error('Bad response from server.')
|
|
|
|
}
|
|
|
|
return responseData
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function registerPushNotifications (store) {
|
|
|
|
if (isPushSupported()) {
|
|
|
|
registerServiceWorker()
|
|
|
|
.then(function (registration) {
|
|
|
|
return askPermission()
|
|
|
|
.then(() => subscribe(registration, store))
|
|
|
|
.then((subscription) => sendSubscriptionToBackEnd(subscription, store))
|
|
|
|
.catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|