aboutsummaryrefslogtreecommitdiff
path: root/src/lib/AniList/forum.ts
blob: 46d569b4bcaf65790d60e0d0c86b8a1274f59f04 (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
import type { User } from './follow';
import { user } from './user';

export interface Thread {
	id: number;
	title: string;
	createdAt: number;
	mediaCategories: {
		coverImage: {
			extraLarge: string;
		};
	}[];
}

export interface ThreadPage {
	data: {
		Page: {
			threads: Thread[];
			pageInfo: {
				hasNextPage: boolean;
				currentPage: number;
			};
		};
	};
}

const threadPage = async (page: number, userId: number): Promise<ThreadPage> =>
	await (
		await fetch('https://graphql.anilist.co', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/json',
				Accept: 'application/json'
			},
			body: JSON.stringify({
				query: `{ Page(perPage: 50, page: ${page}) {
  threads(userId: ${userId}) { id title createdAt mediaCategories { coverImage { extraLarge } } }
  pageInfo { hasNextPage }
} }`
			})
		})
	).json();

export const threads = async (username: string): Promise<Thread[]> => {
	const allThreads = [];
	const userId = (await user(username)).id;
	let page = 1;
	let currentPage = await threadPage(page, userId);

	for (const thread of currentPage.data.Page.threads) allThreads.push(thread);

	while (currentPage.data.Page.pageInfo.hasNextPage) {
		page += 1;
		currentPage = await threadPage(page, userId);

		for (const thread of currentPage.data.Page.threads) allThreads.push(thread);
	}

	return allThreads;
};

export const threadLikes = async (id: number): Promise<Partial<User>[]> => {
	const activityResponse = await (
		await fetch('https://graphql.anilist.co', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/json',
				Accept: 'application/json'
			},
			body: JSON.stringify({ query: `{ Thread(id: ${id}) { likes { name } } }` })
		})
	).json();

	return activityResponse['data']['Thread']['likes'];
};