File size: 1,754 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// @flow
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;
// get the thread being locked
const threads = await getThreads([threadId]);
const threadToEvaluate = threads && threads[0];
// if the thread doesn't exist
if (!threadToEvaluate || threadToEvaluate.deletedAt) {
return new UserError("This thread doesn't exist");
}
// return the thread, channels and communities
const [
currentUserChannelPermissions,
currentUserCommunityPermissions,
] = await Promise.all([
getUserPermissionsInChannel(threadToEvaluate.channelId, user.id),
getUserPermissionsInCommunity(threadToEvaluate.communityId, user.id),
]);
// if the user owns the community or the channel, or they are the original creator, they can delete the thread
if (
currentUserChannelPermissions.isOwner ||
currentUserChannelPermissions.isModerator ||
currentUserCommunityPermissions.isOwner ||
currentUserCommunityPermissions.isModerator ||
threadToEvaluate.creatorId === user.id
) {
return await deleteThread(threadId, user.id);
}
// if the user is not a channel or community owner, the thread can't be locked
return new UserError(
"You don't have permission to make changes to this thread."
);
});
|