|
|
|
|
|
import type { GraphQLContext } from '../../'; |
|
|
import type { DBUser } from 'shared/types'; |
|
|
import type { PaginationOptions } from '../../utils/paginate-arrays'; |
|
|
import { encode, decode } from '../../utils/base64'; |
|
|
const { |
|
|
getViewableThreadsByUser, |
|
|
getPublicThreadsByUser, |
|
|
getPublicParticipantThreadsByUser, |
|
|
getViewableParticipantThreadsByUser, |
|
|
} = require('../../models/thread'); |
|
|
|
|
|
export default ( |
|
|
{ id }: DBUser, |
|
|
{ |
|
|
first, |
|
|
after, |
|
|
kind, |
|
|
}: { ...PaginationOptions, kind: 'creator' | 'participant' }, |
|
|
{ user }: GraphQLContext |
|
|
) => { |
|
|
const currentUser = user; |
|
|
const cursor = decode(after); |
|
|
|
|
|
const lastDigits = cursor.match(/-(\d+)$/); |
|
|
const lastThreadIndex = |
|
|
lastDigits && lastDigits.length > 0 && parseInt(lastDigits[1], 10); |
|
|
|
|
|
|
|
|
let getThreads; |
|
|
if (currentUser) { |
|
|
getThreads = |
|
|
kind === 'creator' |
|
|
? |
|
|
getViewableThreadsByUser(id, currentUser.id, { |
|
|
first, |
|
|
after: lastThreadIndex, |
|
|
}) |
|
|
: |
|
|
getViewableParticipantThreadsByUser(id, currentUser.id, { |
|
|
first, |
|
|
after: lastThreadIndex, |
|
|
}); |
|
|
} else { |
|
|
getThreads = |
|
|
kind === 'creator' |
|
|
? |
|
|
getPublicThreadsByUser(id, { first, after: lastThreadIndex }) |
|
|
: |
|
|
getPublicParticipantThreadsByUser(id, { |
|
|
first, |
|
|
after: lastThreadIndex, |
|
|
}); |
|
|
} |
|
|
|
|
|
return getThreads.then(result => ({ |
|
|
pageInfo: { |
|
|
|
|
|
hasNextPage: result && result.length >= first, |
|
|
}, |
|
|
edges: result.map((thread, index) => ({ |
|
|
cursor: encode(`${thread.id}-${lastThreadIndex + index + 1}`), |
|
|
node: thread, |
|
|
})), |
|
|
})); |
|
|
}; |
|
|
|