diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/Data/AniList/notifications.ts | 36 | ||||
| -rw-r--r-- | src/lib/Utility/notifications.ts | 26 | ||||
| -rw-r--r-- | src/service-worker.ts | 9 |
3 files changed, 53 insertions, 18 deletions
diff --git a/src/lib/Data/AniList/notifications.ts b/src/lib/Data/AniList/notifications.ts index b21a71e4..3b0a9648 100644 --- a/src/lib/Data/AniList/notifications.ts +++ b/src/lib/Data/AniList/notifications.ts @@ -1,3 +1,5 @@ +import { database } from "$lib/Database/IDB/user"; + export interface Notification { user: { name: string; @@ -16,18 +18,18 @@ export interface Notification { id: number; createdAt: number; type: - | 'FOLLOWING' - | 'ACTIVITY_MESSAGE' - | 'ACTIVITY_MENTION' - | 'ACTIVITY_REPLY' - | 'ACTIVITY_REPLY_SUBSCRIBED' - | 'ACTIVITY_LIKE' - | 'ACTIVITY_REPLY_LIKE' - | 'THREAD_COMMENT_MENTION' - | 'THREAD_COMMENT_REPLY' - | 'THREAD_SUBSCRIBED' - | 'THREAD_COMMENT_LIKE' - | 'THREAD_LIKE'; + | 'FOLLOWING' + | 'ACTIVITY_MESSAGE' + | 'ACTIVITY_MENTION' + | 'ACTIVITY_REPLY' + | 'ACTIVITY_REPLY_SUBSCRIBED' + | 'ACTIVITY_LIKE' + | 'ACTIVITY_REPLY_LIKE' + | 'THREAD_COMMENT_MENTION' + | 'THREAD_COMMENT_REPLY' + | 'THREAD_SUBSCRIBED' + | 'THREAD_COMMENT_LIKE' + | 'THREAD_LIKE'; } export const notifications = async (accessToken: string): Promise<Notification[] | null> => { @@ -76,3 +78,13 @@ export const notifications = async (accessToken: string): Promise<Notification[] return data['data']['Page']['notifications']; }; + +export const isNotificationQueued = (recentNotifications: Notification[] | null, lastNotificationID: number | null) => + recentNotifications && + recentNotifications.length > 0 && + (recentNotifications[0].id > (lastNotificationID as number) || + new Date(recentNotifications[0].createdAt * 1000).getTime() + 30000 > + new Date().getTime()) + +export const updateLastNotificationID = async (userID: number, recentNotifications: Notification[]) => + await database.users.update(userID, { lastNotificationID: recentNotifications[0].id }); 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' + }) + } +} diff --git a/src/service-worker.ts b/src/service-worker.ts index 2f70ced2..360ade75 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -5,7 +5,7 @@ import { build, files, version } from '$service-worker'; import { database } from './lib/Database/IDB/user'; -import { notifications } from './lib/Data/AniList/notifications'; +import { isNotificationQueued, notifications, updateLastNotificationID } from './lib/Data/AniList/notifications'; const sw = self as unknown as ServiceWorkerGlobalScope; const CACHE = `cache-${version}`; @@ -82,12 +82,9 @@ sw.addEventListener('push', async (event: PushEvent) => { if ( recentNotifications && - recentNotifications.length > 0 && - (recentNotifications[0].id > (user.lastNotificationID as number) || - new Date(recentNotifications[0].createdAt * 1000).getTime() + 30000 > - new Date().getTime()) + isNotificationQueued(recentNotifications, user.lastNotificationID) ) { - await database.users.update(user.id, { lastNotificationID: recentNotifications[0].id }); + await updateLastNotificationID(user.id, recentNotifications) await sw.registration.showNotification('due.moe', { body: `${recentNotifications[0].user.name}${recentNotifications[0].context}`, icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png', |