diff options
| author | Fuwn <[email protected]> | 2025-01-28 20:59:45 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-01-28 20:59:45 -0800 |
| commit | 4911e3b2d4cc373b3996d2cffe1e469f705ae258 (patch) | |
| tree | b1ff789c201a3f03b808fc73442c84a0f530a1c1 /src/lib/Utility | |
| parent | fix(layout): remove invalid local user entries (diff) | |
| download | due.moe-4911e3b2d4cc373b3996d2cffe1e469f705ae258.tar.xz due.moe-4911e3b2d4cc373b3996d2cffe1e469f705ae258.zip | |
feat(notifications): fallback on service-worker-less notifications
Diffstat (limited to 'src/lib/Utility')
| -rw-r--r-- | src/lib/Utility/notifications.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lib/Utility/notifications.ts b/src/lib/Utility/notifications.ts index e80563ec..929fe6a4 100644 --- a/src/lib/Utility/notifications.ts +++ b/src/lib/Utility/notifications.ts @@ -1,4 +1,6 @@ import { env } from '$env/dynamic/public'; +import { isNotificationQueued, notifications, updateLastNotificationID } from '$lib/Data/AniList/notifications'; +import { database } from '$lib/Database/IDB/user'; import { getFingerprint } from './fingerprint'; import root from './root'; @@ -24,7 +26,31 @@ export const requestNotifications = async () => { await fetch(`/api/notifications/unsubscribe?p=${getFingerprint()}`, { method: 'POST' }); + + setInterval(async () => await updateLocalNotifications(), 1000 * 60); } } } }; + +const updateLocalNotifications = async () => { + const user = (await database.users.toArray()).at(0); + + if (!user) return; + + const recentNotifications = await notifications(user.user.accessToken); + + if (await window.Notification.requestPermission() == "granted") + if ( + recentNotifications && + isNotificationQueued(recentNotifications, user.lastNotificationID) + ) { + await updateLastNotificationID(user.id, recentNotifications) + + new Notification('due.moe', { + body: `${recentNotifications[0].user.name}${recentNotifications[0].context}`, + icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png', + tag: 'notification-1' + }) + } +} |