File size: 1,047 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// @flow
import type { DBCommunity } from 'shared/types';
import type { GraphQLContext } from '../../';

(community: DBCommunity, _: any, { loaders }: GraphQLContext, info: any) => {
  // in some cases we fetch this upstream - e.g. in the case of querying for communitysThreads, we need to fetch contextPermissions before we hit this step as threadIds are not included in the query variables
  if (community.contextPermissions) return community.contextPermissions;

  const queryName = info.operation.name.value;

  const handleCheck = async () => {
    switch (queryName) {
      case 'getUser': {
        const username = info.variableValues.username;
        const user = await loaders.userByUsername.load(username);
        const {
          isModerator,
          isOwner,
        } = await loaders.userPermissionsInCommunity.load([
          user.id,
          community.id,
        ]);
        return {
          communityId: community.id,
          isModerator,
          isOwner,
        };
      }
    }
  };

  return handleCheck();
};