Download spectrum/api/mutations/thread/deleteThread.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.75 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/spectrum/api/mutations/thread/deleteThread.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/spectrum/api/mutations/thread/deleteThread.js
-
curl -L -o deleteThread.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/spectrum/api/mutations/thread/deleteThread.js
1.75 kB
| // @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." | |
| ); | |
| }); | |