aboutsummaryrefslogtreecommitdiff
path: root/src/sw.js
blob: d0050b24e6d8ac3b9a1b4659a21c7ea01c3a4de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* 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'

const messages = {
  ar: require('./i18n/ar.json'),
  ca: require('./i18n/ca.json'),
  cs: require('./i18n/cs.json'),
  de: require('./i18n/de.json'),
  eo: require('./i18n/eo.json'),
  es: require('./i18n/es.json'),
  et: require('./i18n/et.json'),
  eu: require('./i18n/eu.json'),
  fi: require('./i18n/fi.json'),
  fr: require('./i18n/fr.json'),
  ga: require('./i18n/ga.json'),
  he: require('./i18n/he.json'),
  hu: require('./i18n/hu.json'),
  it: require('./i18n/it.json'),
  ja: require('./i18n/ja_pedantic.json'),
  ja_easy: require('./i18n/ja_easy.json'),
  ko: require('./i18n/ko.json'),
  nb: require('./i18n/nb.json'),
  nl: require('./i18n/nl.json'),
  oc: require('./i18n/oc.json'),
  pl: require('./i18n/pl.json'),
  pt: require('./i18n/pt.json'),
  ro: require('./i18n/ro.json'),
  ru: require('./i18n/ru.json'),
  te: require('./i18n/te.json'),
  zh: require('./i18n/zh.json'),
  en: require('./i18n/en.json')
}

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')
    .then(data => data.config.webPushNotifications)
}

function getWindowClients () {
  return clients.matchAll({ includeUncontrolled: true })
    .then((clientList) => clientList.filter(({ type }) => type === 'window'))
}

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 } })
    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))
  }
})

self.addEventListener('notificationclick', (event) => {
  event.notification.close()

  event.waitUntil(getWindowClients().then((list) => {
    for (var i = 0; i < list.length; i++) {
      var client = list[i]
      if (client.url === '/' && 'focus' in client) { return client.focus() }
    }

    if (clients.openWindow) return clients.openWindow('/')
  }))
})