diff options
Diffstat (limited to 'src/lib/Utility/notifications.ts')
| -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' + }) + } +} |