aboutsummaryrefslogtreecommitdiff
path: root/src/sw.js
diff options
context:
space:
mode:
authorShpuld Shpludson <shp@cock.li>2020-06-27 07:19:49 +0000
committerShpuld Shpludson <shp@cock.li>2020-06-27 07:19:49 +0000
commitea0a12f6049241cb9e8fa7cd5bc6dc8b9852b57c (patch)
tree36863abf8ac320756bc16906b11a7892ea7da0dc /src/sw.js
parentbad3dacfac1ef3dd2c0f55b53fb78f4bf410a01e (diff)
parentbbb91d8ae3f1c3d5374de7610e723e63121e8222 (diff)
Merge branch 'develop' into 'iss-149/profile-fields-setting'
# Conflicts: # src/components/settings_modal/tabs/profile_tab.vue
Diffstat (limited to 'src/sw.js')
-rw-r--r--src/sw.js47
1 files changed, 39 insertions, 8 deletions
diff --git a/src/sw.js b/src/sw.js
index 6cecb3f3..f5e34dd6 100644
--- a/src/sw.js
+++ b/src/sw.js
@@ -1,6 +1,19 @@
/* eslint-env serviceworker */
import localForage from 'localforage'
+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'
+import messages from './i18n/service_worker_messages.js'
+
+Vue.use(VueI18n)
+const i18n = new VueI18n({
+ // By default, use the browser locale, we will update it if neccessary
+ locale: 'en',
+ fallbackLocale: 'en',
+ messages
+})
function isEnabled () {
return localForage.getItem('vuex-lz')
@@ -12,15 +25,33 @@ function getWindowClients () {
.then((clientList) => clientList.filter(({ type }) => type === 'window'))
}
-self.addEventListener('push', (event) => {
- if (event.data) {
- event.waitUntil(isEnabled().then((isEnabled) => {
- return isEnabled && getWindowClients().then((list) => {
- const data = event.data.json()
+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()
+ if (enabled && (activeClients.length === 0)) {
+ const data = event.data.json()
+
+ const url = `${self.registration.scope}api/v1/notifications/${data.notification_id}`
+ const notification = await fetch(url, { headers: { Authorization: 'Bearer ' + data.access_token } })
+ const notificationJson = await notification.json()
+ const parsedNotification = parseNotification(notificationJson)
- if (list.length === 0) return self.registration.showNotification(data.title, data)
- })
- }))
+ const res = prepareNotificationObject(parsedNotification, i18n)
+
+ self.registration.showNotification(res.title, res)
+ }
+}
+
+self.addEventListener('push', async (event) => {
+ if (event.data) {
+ event.waitUntil(maybeShowNotification(event))
}
})