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