aboutsummaryrefslogtreecommitdiff
path: root/src/graphql/user/resolvers.ts
blob: 1e5b9ec0c7e75fc32f26045c211eaf5dd5506ed7 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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) => {
      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)
      )
  }
};