1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { UserRight } from '../../../../shared'
5 isVideoChannelDescriptionValid,
6 isVideoChannelNameValid,
7 isVideoChannelSupportValid
8 } from '../../../helpers/custom-validators/video-channels'
9 import { logger } from '../../../helpers/logger'
10 import { UserModel } from '../../../models/account/user'
11 import { VideoChannelModel } from '../../../models/video/video-channel'
12 import { areValidationErrors } from '../utils'
13 import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
14 import { ActorModel } from '../../../models/activitypub/actor'
15 import { isBooleanValid } from '../../../helpers/custom-validators/misc'
16 import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares'
18 const videoChannelsAddValidator = [
19 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
20 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
21 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
22 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
24 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
25 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
27 if (areValidationErrors(req, res)) return
29 const actor = await ActorModel.loadLocalByName(req.body.name)
32 .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
41 const videoChannelsUpdateValidator = [
42 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
45 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
48 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
51 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
52 body('bulkVideosSupportUpdate')
54 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
56 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
57 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
59 if (areValidationErrors(req, res)) return
60 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
62 // We need to make additional checks
63 if (res.locals.videoChannel.Actor.isOwned() === false) {
64 return res.status(403)
65 .json({ error: 'Cannot update video channel of another server' })
69 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
70 return res.status(403)
71 .json({ error: 'Cannot update video channel of another user' })
79 const videoChannelsRemoveValidator = [
80 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
82 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
83 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
85 if (areValidationErrors(req, res)) return
86 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
88 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
89 if (!await checkVideoChannelIsNotTheLastOne(res)) return
95 const videoChannelsNameWithHostValidator = [
96 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
98 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
99 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
101 if (areValidationErrors(req, res)) return
103 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
109 const localVideoChannelValidator = [
110 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
112 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
113 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
115 if (areValidationErrors(req, res)) return
116 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
122 // ---------------------------------------------------------------------------
125 videoChannelsAddValidator,
126 videoChannelsUpdateValidator,
127 videoChannelsRemoveValidator,
128 videoChannelsNameWithHostValidator,
129 localVideoChannelValidator
132 // ---------------------------------------------------------------------------
134 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
135 if (videoChannel.Actor.isOwned() === false) {
137 .json({ error: 'Cannot remove video channel of another server.' })
143 // Check if the user can delete the video channel
144 // The user can delete it if s/he is an admin
145 // Or if s/he is the video channel's account
146 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
148 .json({ error: 'Cannot remove video channel of another user' })
157 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
158 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
162 .json({ error: 'Cannot remove the last channel of this user' })