Download spectrum/api/mutations/channel/deleteChannel.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/spectrum/api/mutations/channel/deleteChannel.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/spectrum/api/mutations/channel/deleteChannel.js
-
curl -L -o deleteChannel.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/spectrum/api/mutations/channel/deleteChannel.js
1.26 kB
| // @flow | |
| import type { GraphQLContext } from '../../'; | |
| import UserError from '../../utils/UserError'; | |
| import { removeMembersInChannel } from '../../models/usersChannels'; | |
| import { getChannelById, deleteChannel } from '../../models/channel'; | |
| import { getThreadsByChannelToDelete, deleteThread } from '../../models/thread'; | |
| import { | |
| isAuthedResolver as requireAuth, | |
| canModerateChannel, | |
| } from '../../utils/permissions'; | |
| type Input = { | |
| channelId: string, | |
| }; | |
| export default requireAuth(async (_: any, args: Input, ctx: GraphQLContext) => { | |
| const { channelId } = args; | |
| const { user, loaders } = ctx; | |
| if (!(await canModerateChannel(user.id, channelId, loaders))) { | |
| return new UserError('You don’t have permission to manage this channel'); | |
| } | |
| const channel = await getChannelById(channelId); | |
| if (channel.slug === 'general') { | |
| return new UserError("The general channel can't be deleted"); | |
| } | |
| const [allThreadsInChannel] = await Promise.all([ | |
| getThreadsByChannelToDelete(channelId), | |
| deleteChannel(channelId, user.id), | |
| removeMembersInChannel(channelId), | |
| ]); | |
| if (allThreadsInChannel.length === 0) return true; | |
| return allThreadsInChannel.map( | |
| async thread => await deleteThread(thread.id, user.id) | |
| ); | |
| }); | |