import { user, type User } from "./user"; export interface FollowingPage { data: { Page: { pageInfo: { hasNextPage: boolean; }; following: Partial[]; }; }; } const followingPage = async ( page: number, id: number, ): Promise => await ( await fetch("https://graphql.anilist.co", { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ query: `{ Page(page: ${page}) { pageInfo { hasNextPage } following(userId: ${id}) { name id } } }`, }), }) ).json(); export const followers = async (name: string): Promise[]> => { const activities = []; let page = 1; const userData = await user(name); if (!userData) throw new Error(`User not found: ${name}`); const id = userData.id; let currentPage = await followingPage(page, id); for (const activity of currentPage.data.Page.following) activities.push(activity); while (currentPage["data"]["Page"]["pageInfo"]["hasNextPage"]) { for (const activity of currentPage.data.Page.following) activities.push(activity); page += 1; currentPage = await followingPage(page, id); } for (const activity of currentPage.data.Page.following) activities.push(activity); for (let i = activities.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [activities[i], activities[j]] = [activities[j], activities[i]]; } return activities; };