File size: 1,564 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
// @flow
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') {
    const user = loaders.user.load(senderId);
    return {
      user,
    };
  }

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

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

  let [communityPermissions = {}, channelPermissions = {}] = await Promise.all([
    loaders.userPermissionsInCommunity.load([user.id, thread.communityId]),
    loaders.userPermissionsInChannel.load([user.id, thread.channelId]),
  ]);

  if (!communityPermissions) communityPermissions = {};
  if (!channelPermissions) channelPermissions = {};

  const isMember = communityPermissions.isMember || channelPermissions.isMember;
  const isOwner = communityPermissions.isOwner;
  const isModerator =
    communityPermissions.isModerator || channelPermissions.isModerator;
  const isBlocked =
    channelPermissions.isBlocked || communityPermissions.isBlocked;

  const roles = [];
  if (isModerator) roles.push('moderator');
  if (isOwner) roles.push('admin');
  if (isBlocked) roles.push('blocked');

  return {
    id: communityPermissions.id,
    user,
    isOwner,
    isModerator,
    isBlocked,
    isMember,
    roles,
  };
};