diff options
| author | Fuwn <[email protected]> | 2024-07-24 21:24:09 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-07-24 21:24:09 -0700 |
| commit | 8a94bbfba322f8011017980e4362d46c4d51bb55 (patch) | |
| tree | 4d861cd7f1d9304e6297e1b52fe759674fd5df1a /src/routes/+layout.svelte | |
| parent | feat(layout): browser notifications (diff) | |
| download | due.moe-8a94bbfba322f8011017980e4362d46c4d51bb55.tar.xz due.moe-8a94bbfba322f8011017980e4362d46c4d51bb55.zip | |
feat: background notifications
Diffstat (limited to 'src/routes/+layout.svelte')
| -rw-r--r-- | src/routes/+layout.svelte | 41 |
1 files changed, 11 insertions, 30 deletions
diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index d6575ded..e1381ff0 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -29,6 +29,8 @@ import Announcement from '$lib/Announcement.svelte'; import Message from '$lib/Loading/Message.svelte'; import { notifications } from '$lib/Data/AniList/notifications'; + import { requestNotifications } from '$lib/Utility/notifications'; + import { database as userDatabase } from '$lib/Database/user'; injectSpeedInsights(); @@ -110,36 +112,15 @@ }); }); - if ($settings.displayAniListNotifications && data.user !== undefined) { - if ('Notification' in window && navigator.serviceWorker) - if ((await Notification.requestPermission()) === 'granted') - notificationInterval = setInterval(async () => { - try { - const recentNotifications = await notifications(data.user.accessToken); - - if ( - recentNotifications && - recentNotifications.length > 0 && - recentNotifications[0].id > lastNotificationId && - new Date(recentNotifications[0].createdAt * 1000).getTime() + 30000 > - new Date().getTime() - ) { - lastNotificationId = recentNotifications[0].id; - - if ('serviceWorker' in navigator && navigator.serviceWorker.controller) { - navigator.serviceWorker.ready.then((registration) => { - registration.showNotification('due.moe', { - body: `${recentNotifications[0].user.name}${recentNotifications[0].context}`, - icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png' - }); - }); - } - } - } catch (error) { - console.error(error); - } - }, 15000); - } + if (!(await userDatabase.users.get($userIdentity.id))) + userDatabase.users.put({ + id: $userIdentity.id, + user: data.user, + lastNotificationID: null + }); + + if ($settings.displayAniListNotifications && data.user !== undefined) + if ('Notification' in window && navigator.serviceWorker) requestNotifications(); }); onDestroy(() => { |