File size: 972 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
// @flow
import type { GraphQLContext } from '../../';
import type { PaginationOptions } from '../../utils/paginate-arrays';
import { getThreadsByChannel } from '../../models/thread';
import { encode, decode } from '../../utils/base64';
import { canViewChannel } from '../../utils/permissions';
import type { DBChannel } from 'shared/types';

export default async (
  channel: DBChannel,
  { first, after }: PaginationOptions,
  ctx: GraphQLContext
) => {
  const { id, isPrivate } = channel;
  const { loaders, user: currentUser } = ctx;

  if (isPrivate) {
    if (!(await canViewChannel(currentUser, id, loaders))) return null;
  }

  // $FlowFixMe
  return getThreadsByChannel(id, {
    first,
    after: after && parseInt(decode(after), 10),
  }).then(threads => ({
    pageInfo: {
      hasNextPage: threads.length >= first,
    },
    edges: threads.map(thread => ({
      cursor: encode(String(thread.lastActive.getTime())),
      node: thread,
    })),
  }));
};