diff options
| author | Fuwn <[email protected]> | 2024-02-08 00:01:24 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-02-08 00:01:24 -0800 |
| commit | f78f5f4857f24ee5338fb1643c666a6b18d75769 (patch) | |
| tree | 57b1b09f20b6b261a3b1ae15bfa441965f71ecd9 /src/lib/Data/AniList/notifications.ts | |
| parent | refactor(data): move static data to module (diff) | |
| download | due.moe-f78f5f4857f24ee5338fb1643c666a6b18d75769.tar.xz due.moe-f78f5f4857f24ee5338fb1643c666a6b18d75769.zip | |
refactor(anilist): move to data module
Diffstat (limited to 'src/lib/Data/AniList/notifications.ts')
| -rw-r--r-- | src/lib/Data/AniList/notifications.ts | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/lib/Data/AniList/notifications.ts b/src/lib/Data/AniList/notifications.ts new file mode 100644 index 00000000..bebf1b42 --- /dev/null +++ b/src/lib/Data/AniList/notifications.ts @@ -0,0 +1,78 @@ +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'; +} + +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 { + ... 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(); + + if (data['errors']) return null; + + return data['data']['Page']['notifications']; +}; |