|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import type { GraphQLContext } from '../../'; |
|
|
import { getThread } from '../../models/thread'; |
|
|
import { getChannelById } from '../../models/channel'; |
|
|
import { getCommunities } from '../../models/community'; |
|
|
|
|
|
export default (user: any, _: any, { loaders }: GraphQLContext, info: any) => { |
|
|
|
|
|
if (user.contextPermissions) return user.contextPermissions; |
|
|
if (!info.operation.name) return null; |
|
|
const queryName = info.operation.name.value; |
|
|
const handleCheck = async () => { |
|
|
switch (queryName) { |
|
|
case 'getThread': |
|
|
case 'getThreadMessages': { |
|
|
const threadId = info.variableValues.id; |
|
|
const { communityId } = await getThread(threadId); |
|
|
const { |
|
|
isModerator, |
|
|
isOwner, |
|
|
isBlocked, |
|
|
} = await loaders.userPermissionsInCommunity.load([ |
|
|
user.id, |
|
|
communityId, |
|
|
]); |
|
|
return { |
|
|
isBlocked, |
|
|
communityId, |
|
|
isModerator, |
|
|
isOwner, |
|
|
}; |
|
|
} |
|
|
case 'loadMoreCommunityMembers': |
|
|
case 'getCommunityMembers': { |
|
|
const communityId = info.variableValues.id; |
|
|
const { |
|
|
isOwner, |
|
|
isBlocked, |
|
|
isModerator, |
|
|
isMember, |
|
|
} = await loaders.userPermissionsInCommunity.load([ |
|
|
user.id, |
|
|
communityId, |
|
|
]); |
|
|
return { |
|
|
communityId, |
|
|
isMember, |
|
|
isBlocked, |
|
|
isModerator, |
|
|
isOwner, |
|
|
}; |
|
|
} |
|
|
|
|
|
case 'loadMoreCommunityMembers': |
|
|
case 'getChannelMembers': { |
|
|
const channelId = info.variableValues.id; |
|
|
const { communityId } = await getChannelById(channelId); |
|
|
const { |
|
|
isModerator, |
|
|
isOwner, |
|
|
isBlocked, |
|
|
} = await loaders.userPermissionsInCommunity.load([ |
|
|
user.id, |
|
|
communityId, |
|
|
]); |
|
|
return { |
|
|
isBlocked, |
|
|
communityId, |
|
|
isModerator, |
|
|
isOwner, |
|
|
}; |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
return handleCheck(); |
|
|
}; |
|
|
|