diff options
| author | Fuwn <[email protected]> | 2025-02-14 01:57:30 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-02-14 01:57:30 -0800 |
| commit | cbd31e2734352f73c2d57abf9b631ffbc4053c40 (patch) | |
| tree | ef5c2105d2685ee7eab4f8b0c91f139a03c876ed /src/lib | |
| parent | feat(Tools): Use generic birthdays tool template, adds NIJISANJI birthdays tool (diff) | |
| download | due.moe-cbd31e2734352f73c2d57abf9b631ffbc4053c40.tar.xz due.moe-cbd31e2734352f73c2d57abf9b631ffbc4053c40.zip | |
style: Run formatter
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/Data/AniList/notifications.ts | 40 | ||||
| -rw-r--r-- | src/lib/Utility/notifications.ts | 21 |
2 files changed, 33 insertions, 28 deletions
diff --git a/src/lib/Data/AniList/notifications.ts b/src/lib/Data/AniList/notifications.ts index 3b0a9648..184ffe95 100644 --- a/src/lib/Data/AniList/notifications.ts +++ b/src/lib/Data/AniList/notifications.ts @@ -1,4 +1,4 @@ -import { database } from "$lib/Database/IDB/user"; +import { database } from '$lib/Database/IDB/user'; export interface Notification { user: { @@ -18,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> => { @@ -79,12 +79,16 @@ export const notifications = async (accessToken: string): Promise<Notification[] return data['data']['Page']['notifications']; }; -export const isNotificationQueued = (recentNotifications: Notification[] | null, lastNotificationID: number | null) => +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()) + 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 }); +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 cba430e9..596eec2a 100644 --- a/src/lib/Utility/notifications.ts +++ b/src/lib/Utility/notifications.ts @@ -1,5 +1,9 @@ import { env } from '$env/dynamic/public'; -import { isNotificationQueued, notifications, updateLastNotificationID } from '$lib/Data/AniList/notifications'; +import { + isNotificationQueued, + notifications, + updateLastNotificationID +} from '$lib/Data/AniList/notifications'; import { database } from '$lib/Database/IDB/user'; import { getFingerprint } from './fingerprint'; import root from './root'; @@ -26,7 +30,7 @@ export const requestNotifications = async () => { await fetch(`/api/notifications/unsubscribe?p=${getFingerprint()}`, { method: 'POST' }); - await updateLocalNotifications() + await updateLocalNotifications(); setInterval(async () => await updateLocalNotifications(), 1000 * 60); } } @@ -40,17 +44,14 @@ const updateLocalNotifications = async () => { const recentNotifications = await notifications(user.user.accessToken); - if (await window.Notification.requestPermission() == "granted") - if ( - recentNotifications && - isNotificationQueued(recentNotifications, user.lastNotificationID) - ) { - await updateLastNotificationID(user.id, recentNotifications) + 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' - }) + }); } -} +}; |