2018-12-07 00:34:00 +11:00
|
|
|
/* eslint-env serviceworker */
|
|
|
|
|
2018-12-13 04:03:50 +11:00
|
|
|
import localForage from 'localforage'
|
2020-06-11 23:22:58 +10:00
|
|
|
import { parseNotification } from './services/entity_normalizer/entity_normalizer.service.js'
|
|
|
|
import { prepareNotificationObject } from './services/notification_utils/notification_utils.js'
|
|
|
|
import Vue from 'vue'
|
|
|
|
import VueI18n from 'vue-i18n'
|
2020-06-16 21:27:36 +10:00
|
|
|
import messages from './i18n/service_worker_messages.js'
|
2020-06-11 23:22:58 +10:00
|
|
|
|
|
|
|
Vue.use(VueI18n)
|
|
|
|
const i18n = new VueI18n({
|
|
|
|
// By default, use the browser locale, we will update it if neccessary
|
|
|
|
locale: 'en',
|
|
|
|
fallbackLocale: 'en',
|
|
|
|
messages
|
|
|
|
})
|
2018-12-13 04:03:50 +11:00
|
|
|
|
|
|
|
function isEnabled () {
|
|
|
|
return localForage.getItem('vuex-lz')
|
|
|
|
.then(data => data.config.webPushNotifications)
|
|
|
|
}
|
|
|
|
|
2018-12-09 23:25:43 +11:00
|
|
|
function getWindowClients () {
|
|
|
|
return clients.matchAll({ includeUncontrolled: true })
|
|
|
|
.then((clientList) => clientList.filter(({ type }) => type === 'window'))
|
|
|
|
}
|
|
|
|
|
2020-06-11 23:22:58 +10:00
|
|
|
const setLocale = async () => {
|
|
|
|
const state = await localForage.getItem('vuex-lz')
|
|
|
|
const locale = state.config.interfaceLanguage || 'en'
|
|
|
|
i18n.locale = locale
|
|
|
|
}
|
|
|
|
|
|
|
|
const maybeShowNotification = async (event) => {
|
|
|
|
const enabled = await isEnabled()
|
|
|
|
const activeClients = await getWindowClients()
|
|
|
|
await setLocale()
|
2020-06-11 23:24:01 +10:00
|
|
|
if (enabled && (activeClients.length === 0)) {
|
2020-06-11 23:22:58 +10:00
|
|
|
const data = event.data.json()
|
2018-12-07 00:34:00 +11:00
|
|
|
|
2020-06-11 23:22:58 +10:00
|
|
|
const url = `${self.registration.scope}api/v1/notifications/${data.notification_id}`
|
|
|
|
const notification = await fetch(url, { headers: { Authorization: 'Bearer ' + data.access_token } })
|
|
|
|
let nj = await notification.json()
|
|
|
|
nj = parseNotification(nj)
|
|
|
|
|
|
|
|
const res = prepareNotificationObject(nj, i18n)
|
|
|
|
|
|
|
|
self.registration.showNotification(res.title, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.addEventListener('push', async (event) => {
|
|
|
|
if (event.data) {
|
|
|
|
event.waitUntil(maybeShowNotification(event))
|
2018-12-07 00:34:00 +11:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-12-09 23:25:43 +11:00
|
|
|
self.addEventListener('notificationclick', (event) => {
|
2018-12-07 00:34:00 +11:00
|
|
|
event.notification.close()
|
|
|
|
|
2018-12-09 23:25:43 +11:00
|
|
|
event.waitUntil(getWindowClients().then((list) => {
|
2018-12-07 00:34:00 +11:00
|
|
|
for (var i = 0; i < list.length; i++) {
|
|
|
|
var client = list[i]
|
|
|
|
if (client.url === '/' && 'focus' in client) { return client.focus() }
|
|
|
|
}
|
2018-12-09 23:25:43 +11:00
|
|
|
|
|
|
|
if (clients.openWindow) return clients.openWindow('/')
|
2018-12-07 00:34:00 +11:00
|
|
|
}))
|
|
|
|
})
|