File size: 2,294 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
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
// @flow
/*

    DEPRECATED 2/3/2018 by @brian

*/
import type { GraphQLContext } from '../../';
import { getThread } from '../../models/thread';
import { getChannelById } from '../../models/channel';
import { getCommunities } from '../../models/community';

export default (user: any, _: any, { loaders }: GraphQLContext, info: any) => {
  // in some cases we fetch this upstream - e.g. in the case of querying for usersThreads, we need to fetch contextPermissions before we hit this step as threadIds are not included in the query variables
  if (user.contextPermissions) return user.contextPermissions;
  if (!info.operation.name) return null;
  const queryName = info.operation.name.value;
  const handleCheck = async () => {
    switch (queryName) {
      case 'getThread':
      case 'getThreadMessages': {
        const threadId = info.variableValues.id;
        const { communityId } = await getThread(threadId);
        const {
          isModerator,
          isOwner,
          isBlocked,
        } = await loaders.userPermissionsInCommunity.load([
          user.id,
          communityId,
        ]);
        return {
          isBlocked,
          communityId,
          isModerator,
          isOwner,
        };
      }
      case 'loadMoreCommunityMembers':
      case 'getCommunityMembers': {
        const communityId = info.variableValues.id;
        const {
          isOwner,
          isBlocked,
          isModerator,
          isMember,
        } = await loaders.userPermissionsInCommunity.load([
          user.id,
          communityId,
        ]);
        return {
          communityId,
          isMember,
          isBlocked,
          isModerator,
          isOwner,
        };
      }
      // eslint-disable-next-line
      case 'loadMoreCommunityMembers':
      case 'getChannelMembers': {
        const channelId = info.variableValues.id;
        const { communityId } = await getChannelById(channelId);
        const {
          isModerator,
          isOwner,
          isBlocked,
        } = await loaders.userPermissionsInCommunity.load([
          user.id,
          communityId,
        ]);
        return {
          isBlocked,
          communityId,
          isModerator,
          isOwner,
        };
      }
    }
  };

  return handleCheck();
};