|
|
|
|
|
import type { DBCommunity } from 'shared/types'; |
|
|
import type { GraphQLContext } from '../../'; |
|
|
import { encode, decode } from '../../utils/base64'; |
|
|
import { |
|
|
getChannelsByUserAndCommunity, |
|
|
getPublicChannelsByCommunity, |
|
|
} from '../../models/channel'; |
|
|
import { getThreadsByChannels } from '../../models/thread'; |
|
|
import { canViewCommunity } from '../../utils/permissions'; |
|
|
|
|
|
export type CommunityThreadConnectionPaginationOptions = { |
|
|
after: string, |
|
|
first: number, |
|
|
sort: 'latest' | 'trending', |
|
|
}; |
|
|
|
|
|
|
|
|
export default async (root: DBCommunity, args: CommunityThreadConnectionPaginationOptions, ctx: GraphQLContext) => { |
|
|
const { first = 10, after, sort = 'latest' } = args |
|
|
const { user, loaders } = ctx |
|
|
const { id } = root |
|
|
|
|
|
if (!await canViewCommunity(user, id, loaders)) { |
|
|
return { |
|
|
pageInfo: { |
|
|
hasNextPage: false, |
|
|
}, |
|
|
edges: [] |
|
|
} |
|
|
} |
|
|
|
|
|
const cursor = decode(after); |
|
|
|
|
|
const lastDigits = cursor.match(/-(\d+)$/); |
|
|
const lastThreadIndex = |
|
|
lastDigits && lastDigits.length > 0 && parseInt(lastDigits[1], 10) || 0; |
|
|
const currentUser = user; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let channels; |
|
|
let isMember = false; |
|
|
if (user) { |
|
|
const permissions = await loaders.userPermissionsInCommunity.load([ |
|
|
user.id, |
|
|
id, |
|
|
]); |
|
|
isMember = permissions && permissions.isMember; |
|
|
} |
|
|
if (user && isMember) { |
|
|
channels = await getChannelsByUserAndCommunity(id, currentUser.id); |
|
|
} else { |
|
|
channels = await getPublicChannelsByCommunity(id); |
|
|
} |
|
|
|
|
|
const threads = await getThreadsByChannels(channels, { |
|
|
first, |
|
|
after: lastThreadIndex, |
|
|
sort, |
|
|
}); |
|
|
|
|
|
return { |
|
|
pageInfo: { |
|
|
hasNextPage: threads && threads.length >= first, |
|
|
}, |
|
|
edges: threads.map((thread, index) => ({ |
|
|
cursor: encode(`${thread.id}-${lastThreadIndex + index + 1}`), |
|
|
node: thread, |
|
|
})), |
|
|
}; |
|
|
}; |
|
|
|