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 (4910B)


  1. import runtime from 'serviceworker-webpack5-plugin/lib/runtime'
  2. function urlBase64ToUint8Array (base64String) {
  3. const padding = '='.repeat((4 - base64String.length % 4) % 4)
  4. const base64 = (base64String + padding)
  5. .replace(/-/g, '+')
  6. .replace(/_/g, '/')
  7. const rawData = window.atob(base64)
  8. return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0)))
  9. }
  10. export function isSWSupported () {
  11. return 'serviceWorker' in navigator
  12. }
  13. function isPushSupported () {
  14. return 'PushManager' in window
  15. }
  16. function getOrCreateServiceWorker () {
  17. return runtime.register()
  18. .catch((err) => console.error('Unable to get or create a service worker.', err))
  19. }
  20. function subscribePush (registration, isEnabled, vapidPublicKey) {
  21. if (!isEnabled) return Promise.reject(new Error('Web Push is disabled in config'))
  22. if (!vapidPublicKey) return Promise.reject(new Error('VAPID public key is not found'))
  23. const subscribeOptions = {
  24. userVisibleOnly: false,
  25. applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
  26. }
  27. return registration.pushManager.subscribe(subscribeOptions)
  28. }
  29. function unsubscribePush (registration) {
  30. return registration.pushManager.getSubscription()
  31. .then((subscription) => {
  32. if (subscription === null) { return }
  33. return subscription.unsubscribe()
  34. })
  35. }
  36. function deleteSubscriptionFromBackEnd (token) {
  37. return fetch('/api/v1/push/subscription/', {
  38. method: 'DELETE',
  39. headers: {
  40. 'Content-Type': 'application/json',
  41. Authorization: `Bearer ${token}`
  42. }
  43. }).then((response) => {
  44. if (!response.ok) throw new Error('Bad status code from server.')
  45. return response
  46. })
  47. }
  48. function sendSubscriptionToBackEnd (subscription, token, notificationVisibility) {
  49. return window.fetch('/api/v1/push/subscription/', {
  50. method: 'POST',
  51. headers: {
  52. 'Content-Type': 'application/json',
  53. Authorization: `Bearer ${token}`
  54. },
  55. body: JSON.stringify({
  56. subscription,
  57. data: {
  58. alerts: {
  59. follow: notificationVisibility.follows,
  60. favourite: notificationVisibility.likes,
  61. mention: notificationVisibility.mentions,
  62. reblog: notificationVisibility.repeats,
  63. move: notificationVisibility.moves
  64. }
  65. }
  66. })
  67. }).then((response) => {
  68. if (!response.ok) throw new Error('Bad status code from server.')
  69. return response.json()
  70. }).then((responseData) => {
  71. if (!responseData.id) throw new Error('Bad response from server.')
  72. return responseData
  73. })
  74. }
  75. export async function initServiceWorker (store) {
  76. if (!isSWSupported()) return
  77. await getOrCreateServiceWorker()
  78. navigator.serviceWorker.addEventListener('message', (event) => {
  79. const { dispatch } = store
  80. const { type, ...rest } = event.data
  81. switch (type) {
  82. case 'notificationClicked':
  83. dispatch('notificationClicked', { id: rest.id })
  84. }
  85. })
  86. }
  87. export async function showDesktopNotification (content) {
  88. if (!isSWSupported) return
  89. const { active: sw } = await window.navigator.serviceWorker.getRegistration()
  90. if (!sw) return console.error('No serviceworker found!')
  91. sw.postMessage({ type: 'desktopNotification', content })
  92. }
  93. export async function closeDesktopNotification ({ id }) {
  94. if (!isSWSupported) return
  95. const { active: sw } = await window.navigator.serviceWorker.getRegistration()
  96. if (!sw) return console.error('No serviceworker found!')
  97. if (id >= 0) {
  98. sw.postMessage({ type: 'desktopNotificationClose', content: { id } })
  99. } else {
  100. sw.postMessage({ type: 'desktopNotificationClose', content: { all: true } })
  101. }
  102. }
  103. export async function updateFocus () {
  104. if (!isSWSupported) return
  105. const { active: sw } = await window.navigator.serviceWorker.getRegistration()
  106. if (!sw) return console.error('No serviceworker found!')
  107. sw.postMessage({ type: 'updateFocus' })
  108. }
  109. export function registerPushNotifications (isEnabled, vapidPublicKey, token, notificationVisibility) {
  110. if (isPushSupported()) {
  111. getOrCreateServiceWorker()
  112. .then((registration) => subscribePush(registration, isEnabled, vapidPublicKey))
  113. .then((subscription) => sendSubscriptionToBackEnd(subscription, token, notificationVisibility))
  114. .catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
  115. }
  116. }
  117. export function unregisterPushNotifications (token) {
  118. if (isPushSupported()) {
  119. Promise.all([
  120. deleteSubscriptionFromBackEnd(token),
  121. getOrCreateServiceWorker()
  122. .then((registration) => {
  123. return unsubscribePush(registration).then((result) => [registration, result])
  124. })
  125. .then(([registration, unsubResult]) => {
  126. if (!unsubResult) {
  127. console.warn('Push subscription cancellation wasn\'t successful')
  128. }
  129. })
  130. ]).catch((e) => console.warn(`Failed to disable Web Push Notifications: ${e.message}`))
  131. }
  132. }