aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Media/Anime/cache.ts
blob: e1a711cacb29aa6351a78eb77b9ce6230f50e8f6 (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
import { get } from "svelte/store";
import anime from "$stores/anime";
import { mediaListCollection, Type } from "../../Data/AniList/media";
import lastPruneTimes from "$stores/lastPruneTimes";
import settings from "$stores/settings";
import type {
	AniListAuthorisation,
	UserIdentity,
} from "../../Data/AniList/identity";

export const cleanCache = (
	user: AniListAuthorisation,
	identity: UserIdentity,
) =>
	mediaListCollection(
		user,
		identity,
		Type.Anime,
		get(anime),
		get(lastPruneTimes).anime,
		{
			forcePrune: true,
		},
	);

export const incrementMediaProgress = (
	id: number,
	progress: number | undefined,
	user: AniListAuthorisation,
	callback: (skipped?: boolean) => void,
) => {
	if (get(settings).debugDryRunMutations) {
		console.log(
			`[dry-run] SaveMediaListEntry(mediaId: ${id}, progress: ${
				(progress || 0) + 1
			}) skipped`,
		);
		callback(true);

		return;
	}

	fetch("https://graphql.anilist.co", {
		method: "POST",
		headers: {
			Authorization: `${user.tokenType} ${user.accessToken}`,
			"Content-Type": "application/json",
			Accept: "application/json",
		},
		body: JSON.stringify({
			query: `mutation { SaveMediaListEntry(mediaId: ${id}, progress: ${
				(progress || 0) + 1
			}) { id } }`,
		}),
	}).then(() => callback(false));
};