feat(pwa): allow access elk users from service worker (#662)

Co-authored-by: patak <matias.capeletto@gmail.com>
This commit is contained in:
Joaquín Sánchez 2023-01-01 20:38:05 +01:00 committed by GitHub
parent ca93f1a813
commit 496da96072
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 413 additions and 39 deletions

View file

@ -1,5 +1,6 @@
/// <reference lib="WebWorker" />
/// <reference types="vite/client" />
import { createNotificationOptions, findNotification } from './notification'
import type { PushPayload } from '~/service-worker/types'
declare const self: ServiceWorkerGlobalScope
@ -10,32 +11,15 @@ export const onPush = (event: PushEvent) => {
return Promise.resolve()
const options: PushPayload = event.data!.json()
const {
access_token,
body,
icon,
notification_id,
notification_type,
preferred_locale,
} = options
const url = notification_type === 'mention' ? 'notifications/mention' : 'notifications'
const notificationOptions: NotificationOptions = {
badge: '/pwa-192x192.png',
body,
data: {
access_token,
preferred_locale,
url: `/${url}`,
},
dir: 'auto',
icon,
lang: preferred_locale,
tag: notification_id,
timestamp: new Date().getTime(),
}
return self.registration.showNotification(options.title, notificationOptions)
return findNotification(options)
.catch((e) => {
console.error('unhandled error finding notification', e)
return Promise.resolve(undefined)
})
.then((notificationInfo) => {
return self.registration.showNotification(options.title, createNotificationOptions(options, notificationInfo))
})
})
event.waitUntil(promise)