diff options
Diffstat (limited to 'src/graphql/user/resolvers.ts')
| -rw-r--r-- | src/graphql/user/resolvers.ts | 125 |
1 files changed, 116 insertions, 9 deletions
diff --git a/src/graphql/user/resolvers.ts b/src/graphql/user/resolvers.ts index af71d384..1e5b9ec0 100644 --- a/src/graphql/user/resolvers.ts +++ b/src/graphql/user/resolvers.ts @@ -1,14 +1,121 @@ -import { getUserBadges } from '$lib/Database/SB/User/badges'; +import { userIdentity, type UserIdentity } from '$lib/Data/AniList/identity'; +import { + addUserBadge, + getUserBadges, + removeAllUserBadges, + removeUserBadge, + setShadowHidden, + setShadowHiddenBadge, + updateUserBadge +} from '$lib/Database/SB/User/badges'; import type { WithIndex } from '../$types'; import type { Resolvers, Badge } from './$types'; +import authorisedJson from '$lib/Data/Static/authorised.json'; +import type { RequestEvent } from '@sveltejs/kit'; + +type Context = RequestEvent<Partial<Record<string, string>>, string | null>; + +const auth = async (context: Context) => { + const userCookie = context.cookies.get('user'); + + if (!userCookie) return Error('Unauthorised'); + + const user = JSON.parse(userCookie); + + return await userIdentity({ + tokenType: user['token_type'], + expiresIn: user['expires_in'], + accessToken: user['access_token'], + refreshToken: user['refresh_token'] + }); +}; + +const authenticatedOperation = async ( + context: Context, + operation: (identity: UserIdentity, authorised: boolean) => void +) => { + const identity = await auth(context); + + if (identity instanceof Error) return []; + + const authorised = authorisedJson.includes(identity.id); + + operation(identity, authorised); + + return await getUserBadges(identity.id); +}; export const resolvers: WithIndex<Resolvers> = { - Query: { - User: async (_, args /* , _context */) => { - return { - id: args.id, - badges: (await getUserBadges(args.id)) as Badge[] - }; - } - } + Query: { + User: async (_, args) => { + return { + id: args.id, + badges: (await getUserBadges(args.id)) as Badge[] + }; + } + }, + Mutation: { + shadowHideBadges: async (_, args, context) => + await authenticatedOperation( + context, + async (_, authorised) => await setShadowHidden(args.userId, authorised) + ), + shadowHideBadge: async (_, args, context) => + await authenticatedOperation( + context, + async () => + await setShadowHiddenBadge(args.id, args.state == null ? true : args.state) + ), + hideBadge: async (_, args, context) => + await authenticatedOperation(context, async (identity) => { + const allBadges = await getUserBadges(identity.id); + const category = args.category || ''; + + await Promise.all( + allBadges + .filter((badge) => badge.category === category) + .map(async (badge) => { + await updateUserBadge(identity.id, badge.id as number, { + ...badge, + hidden: + allBadges + .filter((badge) => badge.category === category) + .filter((badge) => badge.hidden).length > + allBadges.filter((badge) => badge.category === category).length / 2 + ? false + : true + }); + }) + ); + }), + updateBadge: async (_, args, context) => + await authenticatedOperation(context, async (identity) => { + const badge = { + post: args.post || undefined, + image: args.image || undefined, + description: args.description || null, + time: args.time || undefined, + category: args.category || null, + hidden: args.hidden || false, + source: args.source || null, + designer: args.designer || null + }; + + if ((await getUserBadges(identity.id)).find((badge) => badge.id === args.id)) { + await updateUserBadge(identity.id, args.id as number, badge); + } else { + await addUserBadge(identity.id, badge); + } + }), + deleteBadge: async (_, args, context) => + await authenticatedOperation( + context, + async (identity) => await removeUserBadge(identity.id, args.id) + ), + pruneUserBadges: async (_, context) => + await authenticatedOperation( + context as Context, + async (identity) => await removeAllUserBadges(identity.id) + ) + } }; |