import type { AniListAuthorisation } from '$lib/AniList/identity'; import type { UserIdentity } from './identity'; import anime from '../../stores/anime'; import manga from '../../stores/manga'; import settings from '../../stores/settings'; import lastPruneTimes from '../../stores/lastPruneTimes'; export enum Type { Anime, Manga } export interface Media { id: number; status: string; type: string; episodes: number; format: string; title: { romaji: string; english: string; native: string; }; nextAiringEpisode?: { episode: number; timeUntilAiring?: number; }; mediaListEntry?: { progress: number; status: string; }; startDate: { year: number; }; } export const flattenLists = (lists: { entries: { media: Media }[] }[]) => { if (lists === undefined) { return []; } let flattenedList: { media: Media }[] = []; const minimisedList: Media[] = []; for (const list of lists) { flattenedList = flattenedList.concat(list.entries); } for (const [position, entry] of flattenedList.entries()) { minimisedList[position] = entry.media; } return minimisedList; }; export const mediaListCollection = async ( anilistAuthorisation: AniListAuthorisation, userIdentity: UserIdentity, type: Type, mediaCache: string | undefined, currentLastPruneAt: string | number, forcePrune = false ): Promise => { let currentCacheMinutes; settings.subscribe((value) => { currentCacheMinutes = value.cacheMinutes; }); if (String(currentLastPruneAt) === '') { if (type === Type.Anime) { lastPruneTimes.setKey('anime', new Date().getTime()); } else { lastPruneTimes.setKey('manga', new Date().getTime()); } } else { if ( (new Date().getTime() - Number(currentLastPruneAt)) / 1000 / 60 > Number(currentCacheMinutes) || forcePrune ) { if (type === Type.Anime) { lastPruneTimes.setKey('anime', new Date().getTime()); anime.set(''); } else { lastPruneTimes.setKey('manga', new Date().getTime()); manga.set(''); } mediaCache = ''; } } if (mediaCache !== undefined && mediaCache !== '') { return JSON.parse(mediaCache); } const userIdResponse = 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: `{ MediaListCollection(userId: ${userIdentity.id}, type: ${ type === Type.Anime ? 'ANIME' : 'MANGA' }, status_not_in: [ COMPLETED ]) { lists { entries { media { id status type episodes format title { romaji english native } nextAiringEpisode { episode timeUntilAiring } mediaListEntry { progress status } startDate { year } } } } } }` }) }) ).json(); if (mediaCache === '') { if (type === Type.Anime) { anime.set( JSON.stringify(flattenLists(userIdResponse['data']['MediaListCollection']['lists'])) ); } else { manga.set( JSON.stringify(flattenLists(userIdResponse['data']['MediaListCollection']['lists'])) ); } } return flattenLists(userIdResponse['data']['MediaListCollection']['lists']); }; export const publicMediaListCollection = async (userId: number, type: Type): Promise => { const userIdResponse = await ( await fetch('https://graphql.anilist.co', { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, body: JSON.stringify({ query: `{ MediaListCollection(userId: ${userId}, type: ${ type === Type.Anime ? 'ANIME' : 'MANGA' }, status_not_in: [ COMPLETED ]) { lists { entries { media { id status type episodes format title { romaji english native } nextAiringEpisode { episode timeUntilAiring } mediaListEntry { progress status } startDate { year } } } } } }` }) }) ).json(); return flattenLists(userIdResponse['data']['MediaListCollection']['lists']); };