File size: 886 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
// TODO: Flow type again
import Raven from 'shared/raven';
import type { DBChannel } from 'shared/types';
import type { GraphQLContext } from '../../';
import { canViewChannel } from '../../utils/permissions';

export default async (root: DBChannel, _: any, ctx: GraphQLContext) => {
  const { user, loaders } = ctx;
  const { id, memberCount: rootMemberCount } = root;

  if (!(await canViewChannel(user, id, loaders))) {
    return {
      members: 0,
    };
  }

  if (typeof rootMemberCount === 'number') {
    return {
      members: rootMemberCount,
    };
  }

  // Fallback if there's no denormalized memberCount, also report to Sentry
  Raven.captureException(
    new Error(`Channel with ID "${id}" does not have denormalized memberCount.`)
  );
  return {
    members: await loaders.channelMemberCount
      .load(id)
      .then(res => (res && res.reduction) || 0),
  };
};