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

    DEPRECATED 2/3/2018 by @brian

*/
import type { DBMessage } from 'shared/types';
import type { GraphQLContext } from '../../';

export default async (
  { senderId, threadId, threadType }: DBMessage,
  _: any,
  { loaders }: GraphQLContext
) => {
  // there will be no community to resolve in direct message threads, so we can escape early
  // and only return the sender
  if (threadType === 'directMessageThread') {
    return loaders.user.load(senderId);
  }

  const [thread, sender] = await Promise.all([
    loaders.thread.load(threadId),
    loaders.user.load(senderId),
  ]);

  if (!thread || !sender) return null;

  const permissions = await loaders.userPermissionsInCommunity.load([
    senderId,
    thread.communityId,
  ]);

  return {
    ...sender,
    contextPermission: {
      communityId: thread.communityId,
      isModerator: permissions ? permissions.isModerator : false,
      isOwner: permissions ? permissions.isOwner : false,
      isBlocked: permissions ? permissions.isBlocked : false,
    },
  };
};