aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Data/AniList/notifications.ts
blob: bebf1b42cad56f1da3c25fb86a19f8a0988511aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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'];
};