logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

sw.js (4502B)


  1. /* eslint-env serviceworker */
  2. import localForage from 'localforage'
  3. import { parseNotification } from './services/entity_normalizer/entity_normalizer.service.js'
  4. import { prepareNotificationObject } from './services/notification_utils/notification_utils.js'
  5. import { createI18n } from 'vue-i18n'
  6. import messages from './i18n/service_worker_messages.js'
  7. const i18n = createI18n({
  8. // By default, use the browser locale, we will update it if neccessary
  9. locale: 'en',
  10. fallbackLocale: 'en',
  11. messages
  12. })
  13. const state = {
  14. lastFocused: null,
  15. notificationIds: new Set(),
  16. allowedNotificationTypes: null
  17. }
  18. function getWindowClients () {
  19. return clients.matchAll({ includeUncontrolled: true })
  20. .then((clientList) => clientList.filter(({ type }) => type === 'window'))
  21. }
  22. const setSettings = async () => {
  23. const vuexState = await localForage.getItem('vuex-lz')
  24. const locale = vuexState.config.interfaceLanguage || 'en'
  25. i18n.locale = locale
  26. const notificationsNativeArray = Object.entries(vuexState.config.notificationNative)
  27. state.webPushAlwaysShowNotifications = vuexState.config.webPushAlwaysShowNotifications
  28. state.allowedNotificationTypes = new Set(
  29. notificationsNativeArray
  30. .filter(([k, v]) => v)
  31. .map(([k]) => {
  32. switch (k) {
  33. case 'mentions':
  34. return 'mention'
  35. case 'likes':
  36. return 'like'
  37. case 'repeats':
  38. return 'repeat'
  39. case 'emojiReactions':
  40. return 'pleroma:emoji_reaction'
  41. case 'reports':
  42. return 'pleroma:report'
  43. case 'followRequest':
  44. return 'follow_request'
  45. case 'follows':
  46. return 'follow'
  47. case 'polls':
  48. return 'poll'
  49. default:
  50. return k
  51. }
  52. })
  53. )
  54. }
  55. const showPushNotification = async (event) => {
  56. const activeClients = await getWindowClients()
  57. await setSettings()
  58. // Only show push notifications if all tabs/windows are closed
  59. if (state.webPushAlwaysShowNotifications || activeClients.length === 0) {
  60. const data = event.data.json()
  61. const url = `${self.registration.scope}api/v1/notifications/${data.notification_id}`
  62. const notification = await fetch(url, { headers: { Authorization: 'Bearer ' + data.access_token } })
  63. const notificationJson = await notification.json()
  64. const parsedNotification = parseNotification(notificationJson)
  65. const res = prepareNotificationObject(parsedNotification, i18n)
  66. if (state.webPushAlwaysShowNotifications || state.allowedNotificationTypes.has(parsedNotification.type)) {
  67. return self.registration.showNotification(res.title, res)
  68. }
  69. }
  70. return Promise.resolve()
  71. }
  72. self.addEventListener('push', async (event) => {
  73. if (event.data) {
  74. // Supposedly, we HAVE to return a promise inside waitUntil otherwise it will
  75. // show (extra) notification that website is updated in background
  76. event.waitUntil(showPushNotification(event))
  77. }
  78. })
  79. self.addEventListener('message', async (event) => {
  80. await setSettings()
  81. const { type, content } = event.data
  82. if (type === 'desktopNotification') {
  83. const { title, ...rest } = content
  84. const { tag, type } = rest
  85. if (state.notificationIds.has(tag)) return
  86. state.notificationIds.add(tag)
  87. setTimeout(() => state.notificationIds.delete(tag), 10000)
  88. if (state.allowedNotificationTypes.has(type)) {
  89. self.registration.showNotification(title, rest)
  90. }
  91. }
  92. if (type === 'desktopNotificationClose') {
  93. const { id, all } = content
  94. const search = all ? null : { tag: id }
  95. const notifications = await self.registration.getNotifications(search)
  96. notifications.forEach(n => n.close())
  97. }
  98. if (type === 'updateFocus') {
  99. state.lastFocused = event.source.id
  100. const notifications = await self.registration.getNotifications()
  101. notifications.forEach(n => n.close())
  102. }
  103. })
  104. self.addEventListener('notificationclick', (event) => {
  105. event.notification.close()
  106. event.waitUntil(getWindowClients().then((list) => {
  107. for (let i = 0; i < list.length; i++) {
  108. const client = list[i]
  109. client.postMessage({ type: 'notificationClicked', id: event.notification.tag })
  110. }
  111. for (let i = 0; i < list.length; i++) {
  112. const client = list[i]
  113. if (state.lastFocused === null || client.id === state.lastFocused) {
  114. if ('focus' in client) return client.focus()
  115. }
  116. }
  117. if (clients.openWindow) return clients.openWindow('/')
  118. }))
  119. })