|
|
|
|
|
import type { GraphQLContext } from '../../'; |
|
|
import UserError from '../../utils/UserError'; |
|
|
import { getUserPermissionsInCommunity } from '../../models/usersCommunities'; |
|
|
import { getUserPermissionsInChannel } from '../../models/usersChannels'; |
|
|
import { deleteThread, getThreads } from '../../models/thread'; |
|
|
import { isAuthedResolver as requireAuth } from '../../utils/permissions'; |
|
|
|
|
|
type Input = { |
|
|
threadId: string, |
|
|
}; |
|
|
|
|
|
export default requireAuth(async (_: any, args: Input, ctx: GraphQLContext) => { |
|
|
const { user } = ctx; |
|
|
const { threadId } = args; |
|
|
|
|
|
|
|
|
const threads = await getThreads([threadId]); |
|
|
|
|
|
const threadToEvaluate = threads && threads[0]; |
|
|
|
|
|
|
|
|
if (!threadToEvaluate || threadToEvaluate.deletedAt) { |
|
|
return new UserError("This thread doesn't exist"); |
|
|
} |
|
|
|
|
|
|
|
|
const [ |
|
|
currentUserChannelPermissions, |
|
|
currentUserCommunityPermissions, |
|
|
] = await Promise.all([ |
|
|
getUserPermissionsInChannel(threadToEvaluate.channelId, user.id), |
|
|
getUserPermissionsInCommunity(threadToEvaluate.communityId, user.id), |
|
|
]); |
|
|
|
|
|
|
|
|
if ( |
|
|
currentUserChannelPermissions.isOwner || |
|
|
currentUserChannelPermissions.isModerator || |
|
|
currentUserCommunityPermissions.isOwner || |
|
|
currentUserCommunityPermissions.isModerator || |
|
|
threadToEvaluate.creatorId === user.id |
|
|
) { |
|
|
return await deleteThread(threadId, user.id); |
|
|
} |
|
|
|
|
|
|
|
|
return new UserError( |
|
|
"You don't have permission to make changes to this thread." |
|
|
); |
|
|
}); |
|
|
|