aboutsummaryrefslogtreecommitdiff
path: root/src/graphql
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-10-06 01:41:44 -0700
committerFuwn <[email protected]>2024-10-06 01:41:44 -0700
commita84d9c9f47c7cd1b345d0283bef9f211a9727893 (patch)
tree554827840b67e8a0068e707fad973a8780f47957 /src/graphql
parentfeat(graphql): add subtitles (diff)
downloaddue.moe-a84d9c9f47c7cd1b345d0283bef9f211a9727893.tar.xz
due.moe-a84d9c9f47c7cd1b345d0283bef9f211a9727893.zip
feat(badges): move badge operations to graphql
Diffstat (limited to 'src/graphql')
-rw-r--r--src/graphql/user/resolvers.ts125
-rw-r--r--src/graphql/user/schema.graphql19
2 files changed, 135 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)
+ )
+ }
};
diff --git a/src/graphql/user/schema.graphql b/src/graphql/user/schema.graphql
index 942ee72d..1e54f866 100644
--- a/src/graphql/user/schema.graphql
+++ b/src/graphql/user/schema.graphql
@@ -2,6 +2,25 @@ type Query {
User(id: Int!): User!
}
+type Mutation {
+ shadowHideBadges(userId: Int!): [Badge]!
+ shadowHideBadge(id: Int!, state: Boolean): [Badge]!
+ hideBadge(category: String): [Badge]!
+ updateBadge(
+ id: Int
+ post: String
+ image: String
+ description: String
+ time: String
+ category: String
+ hidden: Boolean
+ source: String
+ designer: String
+ ): [Badge]!
+ deleteBadge(id: Int!): [Badge]!
+ pruneUserBadges: [Badge]!
+}
+
type User {
id: Int!
badges: [Badge]!