1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { UserRight } from '../../../../shared'
5 doesLocalVideoChannelNameExist,
6 doesVideoChannelNameWithHostExist,
7 isVideoChannelDescriptionValid,
8 isVideoChannelNameValid,
9 isVideoChannelSupportValid
10 } from '../../../helpers/custom-validators/video-channels'
11 import { logger } from '../../../helpers/logger'
12 import { UserModel } from '../../../models/account/user'
13 import { VideoChannelModel } from '../../../models/video/video-channel'
14 import { areValidationErrors } from '../utils'
15 import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
16 import { ActorModel } from '../../../models/activitypub/actor'
17 import { isBooleanValid } from '../../../helpers/custom-validators/misc'
19 const videoChannelsAddValidator = [
20 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
21 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
22 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
23 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
28 if (areValidationErrors(req, res)) return
30 const actor = await ActorModel.loadLocalByName(req.body.name)
33 .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
42 const videoChannelsUpdateValidator = [
43 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
46 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
49 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
52 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
53 body('bulkVideosSupportUpdate')
55 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
60 if (areValidationErrors(req, res)) return
61 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
63 // We need to make additional checks
64 if (res.locals.videoChannel.Actor.isOwned() === false) {
65 return res.status(403)
66 .json({ error: 'Cannot update video channel of another server' })
70 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
71 return res.status(403)
72 .json({ error: 'Cannot update video channel of another user' })
80 const videoChannelsRemoveValidator = [
81 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
83 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
84 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
86 if (areValidationErrors(req, res)) return
87 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
89 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
90 if (!await checkVideoChannelIsNotTheLastOne(res)) return
96 const videoChannelsNameWithHostValidator = [
97 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
99 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
100 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
102 if (areValidationErrors(req, res)) return
104 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
110 const localVideoChannelValidator = [
111 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
113 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
114 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
116 if (areValidationErrors(req, res)) return
117 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
123 // ---------------------------------------------------------------------------
126 videoChannelsAddValidator,
127 videoChannelsUpdateValidator,
128 videoChannelsRemoveValidator,
129 videoChannelsNameWithHostValidator,
130 localVideoChannelValidator
133 // ---------------------------------------------------------------------------
135 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
136 if (videoChannel.Actor.isOwned() === false) {
138 .json({ error: 'Cannot remove video channel of another server.' })
144 // Check if the user can delete the video channel
145 // The user can delete it if s/he is an admin
146 // Or if s/he is the video channel's account
147 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
149 .json({ error: 'Cannot remove video channel of another user' })
158 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
159 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
163 .json({ error: 'Cannot remove the last channel of this user' })