aboutsummaryrefslogtreecommitdiff
path: root/src/lib/AniList/wrapped.ts
blob: bd700101b1ee00ddf8c3ff1074d1b2881c0ad0b3 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import type { AniListAuthorisation, UserIdentity } from './identity';

export interface Wrapped {
	statistics: {
		anime: {
			startYears: {
				startYear: number;
				minutesWatched: number;
				count: number;
			}[];
		};
		manga: {
			startYears: {
				startYear: number;
				chaptersRead: number;
				count: number;
			}[];
		};
	};
	activities: {
		statusCount: number;
		messageCount: number;
	};
	avatar: {
		large: string;
	};
}

const profileActivities = async (user: AniListAuthorisation, identity: UserIdentity) => {
	const get = async (page: number) =>
		await (
			await fetch('https://graphql.anilist.co', {
				method: 'POST',
				headers: {
					Authorization: `${user.tokenType} ${user.accessToken}`,
					'Content-Type': 'application/json',
					Accept: 'application/json'
				},
				body: JSON.stringify({
					query: `{
	Page(page: ${page}) {
    activities(userId: ${identity.id}, type_in: [ TEXT, MESSAGE ]) {
			... on TextActivity {
        type
      }
      ... on MessageActivity {
        type
      }
    }
    pageInfo {
      hasNextPage
    }
  }
}`
				})
			})
		).json();

	const pages = [];
	let page = 1;
	let response = await get(page);

	pages.push(response['data']['Page']['activities']);

	while (response['data']['Page']['pageInfo']['hasNextPage']) {
		page += 1;
		response = await get(page);

		pages.push(response['data']['Page']['activities']);
	}

	return {
		statusCount: pages.flat().filter((activity) => activity.type == 'TEXT').length,
		messageCount: pages.flat().filter((activity) => activity.type == 'MESSAGE').length
	};
};

export const wrapped = async (
	anilistAuthorisation: AniListAuthorisation,
	identity: UserIdentity
): Promise<Wrapped> => {
	const wrappedResponse = await (
		await fetch('https://graphql.anilist.co', {
			method: 'POST',
			headers: {
				Authorization: `${anilistAuthorisation.tokenType} ${anilistAuthorisation.accessToken}`,
				'Content-Type': 'application/json',
				Accept: 'application/json'
			},
			body: JSON.stringify({
				query: `{
  User(name: "${identity.name}") {
		avatar { large }
    statistics {
      anime { startYears { startYear minutesWatched count } }
      manga { startYears { startYear chaptersRead count } }
    }
  }
}`
			})
		})
	).json();
	const { statusCount, messageCount } = await profileActivities(anilistAuthorisation, identity);

	return {
		statistics: wrappedResponse['data']['User']['statistics'],
		activities: {
			statusCount,
			messageCount
		},
		avatar: wrappedResponse['data']['User']['avatar']
	};
};