2018-12-06 13:34:00 +00: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')
|
2018-12-09 12:25:43 +00:00
|
|
|
.catch((err) => console.error('Unable to register service worker.', err))
|
2018-12-06 13:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function askPermission () {
|
2018-12-09 12:25:43 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const Notification = window.Notification
|
2018-12-07 07:57:35 +00:00
|
|
|
|
2018-12-09 12:25:43 +00:00
|
|
|
if (!Notification) return reject(new Error('Notifications disabled'))
|
|
|
|
if (Notification.permission !== 'default') return resolve(Notification.permission)
|
2018-12-06 13:34:00 +00:00
|
|
|
|
2018-12-09 12:25:43 +00:00
|
|
|
const permissionResult = Notification.requestPermission(resolve)
|
2018-12-06 13:34:00 +00:00
|
|
|
|
|
|
|
if (permissionResult) permissionResult.then(resolve, reject)
|
2018-12-09 12:25:43 +00:00
|
|
|
}).then((permissionResult) => {
|
|
|
|
if (permissionResult !== 'granted') throw new Error('We weren\'t granted permission.')
|
2018-12-06 13:34:00 +00:00
|
|
|
return permissionResult
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-09 12:25:43 +00:00
|
|
|
function subscribe (registration, isEnabled, vapidPublicKey) {
|
|
|
|
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 07:57:35 +00:00
|
|
|
|
2018-12-06 13:34:00 +00:00
|
|
|
const subscribeOptions = {
|
|
|
|
userVisibleOnly: true,
|
2018-12-09 12:25:43 +00:00
|
|
|
applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
|
2018-12-06 13:34:00 +00:00
|
|
|
}
|
|
|
|
return registration.pushManager.subscribe(subscribeOptions)
|
|
|
|
}
|
|
|
|
|
2018-12-09 12:25:43 +00:00
|
|
|
function sendSubscriptionToBackEnd (subscription, token) {
|
2018-12-06 13:34:00 +00:00
|
|
|
return window.fetch('/api/v1/push/subscription/', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2018-12-09 12:25:43 +00:00
|
|
|
'Authorization': `Bearer ${token}`
|
2018-12-06 13:34:00 +00:00
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
subscription,
|
|
|
|
data: {
|
|
|
|
alerts: {
|
|
|
|
follow: true,
|
|
|
|
favourite: true,
|
|
|
|
mention: true,
|
|
|
|
reblog: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2018-12-09 12:25:43 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) throw new Error('Bad status code from server.')
|
2018-12-06 13:34:00 +00:00
|
|
|
return response.json()
|
|
|
|
})
|
2018-12-09 12:25:43 +00:00
|
|
|
.then((responseData) => {
|
|
|
|
if (!responseData.id) throw new Error('Bad response from server.')
|
2018-12-06 13:34:00 +00:00
|
|
|
return responseData
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-09 12:25:43 +00:00
|
|
|
export default function registerPushNotifications (isEnabled, vapidPublicKey, token) {
|
2018-12-06 13:34:00 +00:00
|
|
|
if (isPushSupported()) {
|
|
|
|
registerServiceWorker()
|
2018-12-09 12:25:43 +00:00
|
|
|
.then((registration) => {
|
2018-12-06 13:34:00 +00:00
|
|
|
return askPermission()
|
2018-12-09 12:25:43 +00:00
|
|
|
.then(() => subscribe(registration, isEnabled, vapidPublicKey))
|
|
|
|
.then((subscription) => sendSubscriptionToBackEnd(subscription, token))
|
2018-12-06 13:34:00 +00:00
|
|
|
.catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|