aboutsummaryrefslogtreecommitdiff
path: root/src/lib/AniList/activity.ts
blob: bd4bafc0a10682b8aac03d4a9969ba7db7f56a7b (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
import type { UserIdentity } from './identity';

export interface ActivityHistoryEntry {
	date: number;
	amount: number;
}

export const activityHistory = async (
	userIdentity: UserIdentity
): Promise<ActivityHistoryEntry[]> => {
	return (
		await (
			await fetch('https://graphql.anilist.co', {
				method: 'POST',
				headers: {
					'Content-Type': 'application/json',
					Accept: 'application/json'
				},
				body: JSON.stringify({
					query: `{ User(id: ${userIdentity.id}) {
					stats { activityHistory { date amount } }
        } }`
				})
			})
		).json()
	)['data']['User']['stats']['activityHistory'];
};

export const lastActivityDate = async (userIdentity: UserIdentity): Promise<Date> => {
	const history = await activityHistory(userIdentity);
	const date = new Date(
		Number(history[history.length - 1]['date']) * 1000 + new Date().getTimezoneOffset()
	);

	date.setDate(date.getDate() + 1);

	return date;
};