diff options
Diffstat (limited to 'src/lib/Data/AniList/notifications.ts')
| -rw-r--r-- | src/lib/Data/AniList/notifications.ts | 160 |
1 files changed, 83 insertions, 77 deletions
diff --git a/src/lib/Data/AniList/notifications.ts b/src/lib/Data/AniList/notifications.ts index 184ffe95..a12fd402 100644 --- a/src/lib/Data/AniList/notifications.ts +++ b/src/lib/Data/AniList/notifications.ts @@ -1,94 +1,100 @@ -import { database } from '$lib/Database/IDB/user'; +import { database } from "$lib/Database/IDB/user"; export interface Notification { - user: { - name: string; - avatar: { - large: string; - }; - }; - thread: { - title: string; - id: number; - }; - activity: { - id: number; - }; - context: string; - 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'; + user: { + name: string; + avatar: { + large: string; + }; + }; + thread: { + title: string; + id: number; + }; + activity: { + id: number; + }; + context: string; + 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"; } -export const notifications = async (accessToken: string): Promise<Notification[] | null> => { - const activityNotification = (type: string, extend = '') => `... on ${type} { +export const notifications = async ( + accessToken: string, +): Promise<Notification[] | null> => { + const activityNotification = (type: string, extend = "") => `... on ${type} { id user { name avatar { large } } context createdAt type ${extend} }`; - const richActivityNotification = (type: string) => - `${activityNotification( - type, - `activity { + const richActivityNotification = (type: string) => + `${activityNotification( + type, + `activity { ... on TextActivity { id } ... on ListActivity { id } ... on MessageActivity { id } - }` - )}`; - const threadNotification = (type: string) => - `${activityNotification(type, `thread { title id }`)}`; - const data = await ( - await fetch('https://graphql.anilist.co', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${accessToken}`, - Accept: 'application/json' - }, - body: JSON.stringify({ - query: `{ Page(page: 1, perPage: 50) { notifications { - ${activityNotification('FollowingNotification')} - ${activityNotification('ActivityMessageNotification')} - ${richActivityNotification('ActivityMentionNotification')} - ${richActivityNotification('ActivityReplyNotification')} - ${richActivityNotification('ActivityReplySubscribedNotification')} - ${richActivityNotification('ActivityLikeNotification')} - ${richActivityNotification('ActivityReplyLikeNotification')} - ${threadNotification('ThreadCommentMentionNotification')} - ${threadNotification('ThreadCommentReplyNotification')} - ${threadNotification('ThreadCommentSubscribedNotification')} - ${threadNotification('ThreadCommentLikeNotification')} - ${threadNotification('ThreadLikeNotification')} - } } }` - }) - }) - ).json(); + }`, + )}`; + const threadNotification = (type: string) => + `${activityNotification(type, `thread { title id }`)}`; + const data = await ( + await fetch("https://graphql.anilist.co", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${accessToken}`, + Accept: "application/json", + }, + body: JSON.stringify({ + query: `{ Page(page: 1, perPage: 50) { notifications { + ${activityNotification("FollowingNotification")} + ${activityNotification("ActivityMessageNotification")} + ${richActivityNotification("ActivityMentionNotification")} + ${richActivityNotification("ActivityReplyNotification")} + ${richActivityNotification("ActivityReplySubscribedNotification")} + ${richActivityNotification("ActivityLikeNotification")} + ${richActivityNotification("ActivityReplyLikeNotification")} + ${threadNotification("ThreadCommentMentionNotification")} + ${threadNotification("ThreadCommentReplyNotification")} + ${threadNotification("ThreadCommentSubscribedNotification")} + ${threadNotification("ThreadCommentLikeNotification")} + ${threadNotification("ThreadLikeNotification")} + } } }`, + }), + }) + ).json(); - if (data['errors']) return null; + if (data["errors"]) return null; - return data['data']['Page']['notifications']; + return data["data"]["Page"]["notifications"]; }; export const isNotificationQueued = ( - recentNotifications: Notification[] | null, - lastNotificationID: number | null + 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()); + 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 }); + userID: number, + recentNotifications: Notification[], +) => + await database.users.update(userID, { + lastNotificationID: recentNotifications[0].id, + }); |