From 1de5b2a41cba2e5bd3631b9a34bbd6c091918b9b Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sat, 30 Dec 2023 21:29:33 -0800 Subject: feat(tools): add follow fix base --- src/lib/AniList/follow.ts | 45 +++++++++++++++++++++++++++++++++++++ src/lib/Tools/FollowFix.svelte | 50 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/lib/AniList/follow.ts create mode 100644 src/lib/Tools/FollowFix.svelte (limited to 'src/lib') diff --git a/src/lib/AniList/follow.ts b/src/lib/AniList/follow.ts new file mode 100644 index 00000000..da477b6a --- /dev/null +++ b/src/lib/AniList/follow.ts @@ -0,0 +1,45 @@ +import type { AniListAuthorisation } from './identity'; + +export interface User { + id: number; + name: string; + isFollowing: boolean; + isFollower: boolean; +} + +export const toggleFollow = async ( + anilistAuthorisation: AniListAuthorisation, + username: string +): Promise => { + const { + data: { User: user } + } = 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: "${username}") { id } }` + }) + }) + ).json(); + + return ( + 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({ + mutation: `{ ToggleFollow(userId: ${user.id}) { id name isFollowing isFollower } }` + }) + }) + ).json() + )['data']['ToggleFollow']; +}; diff --git a/src/lib/Tools/FollowFix.svelte b/src/lib/Tools/FollowFix.svelte new file mode 100644 index 00000000..de7a7863 --- /dev/null +++ b/src/lib/Tools/FollowFix.svelte @@ -0,0 +1,50 @@ + + +{#if user === undefined} + Please log in to view this page. +{:else} +

+ { + if (e.key === 'Enter') { + submit = input; + + // eslint-disable-next-line no-undef + umami.track('Fix Follow'); + } + }} + /> + (submit = input)}> + Toggle follow for {input.length === 0 ? '...' : input} + +

+ + {#if submit.length > 0} + {#await toggleFollow(user, submit)} + Toggling follow ... + {:then response} +

+ Successfully toggled follow for {submit}. + + {submit} + {response.isFollower ? 'is' : 'is not'} following you, {response.isFollowing + ? 'but' + : 'and'} you {response.isFollowing ? 'are' : 'are not'} following them. +

+ {:catch} +

Failed to toggle follow for {submit}.

+ {/await} + {/if} +{/if} -- cgit v1.2.3