1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { UserRight } from '../../../shared'
4 import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
6 isLocalVideoChannelNameExist,
7 isVideoChannelDescriptionValid,
8 isVideoChannelNameValid,
9 isVideoChannelNameWithHostExist,
10 isVideoChannelSupportValid
11 } from '../../helpers/custom-validators/video-channels'
12 import { logger } from '../../helpers/logger'
13 import { UserModel } from '../../models/account/user'
14 import { VideoChannelModel } from '../../models/video/video-channel'
15 import { areValidationErrors } from './utils'
16 import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
18 const listVideoAccountChannelsValidator = [
19 param('accountName').exists().withMessage('Should have a valid account name'),
21 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
22 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
24 if (areValidationErrors(req, res)) return
25 if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
31 const videoChannelsAddValidator = [
32 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
33 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
34 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
35 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
37 (req: express.Request, res: express.Response, next: express.NextFunction) => {
38 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
40 if (areValidationErrors(req, res)) return
46 const videoChannelsUpdateValidator = [
47 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
48 body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
49 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
50 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
52 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
53 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
55 if (areValidationErrors(req, res)) return
56 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
58 // We need to make additional checks
59 if (res.locals.videoChannel.Actor.isOwned() === false) {
60 return res.status(403)
61 .json({ error: 'Cannot update video channel of another server' })
65 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
66 return res.status(403)
67 .json({ error: 'Cannot update video channel of another user' })
75 const videoChannelsRemoveValidator = [
76 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
78 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
79 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
81 if (areValidationErrors(req, res)) return
82 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
84 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
85 if (!await checkVideoChannelIsNotTheLastOne(res)) return
91 const videoChannelsNameWithHostValidator = [
92 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
94 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
95 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
97 if (areValidationErrors(req, res)) return
99 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
105 const localVideoChannelValidator = [
106 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
108 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
111 if (areValidationErrors(req, res)) return
112 if (!await isLocalVideoChannelNameExist(req.params.name, res)) return
118 // ---------------------------------------------------------------------------
121 listVideoAccountChannelsValidator,
122 videoChannelsAddValidator,
123 videoChannelsUpdateValidator,
124 videoChannelsRemoveValidator,
125 videoChannelsNameWithHostValidator,
126 localVideoChannelValidator
129 // ---------------------------------------------------------------------------
131 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
132 if (videoChannel.Actor.isOwned() === false) {
134 .json({ error: 'Cannot remove video channel of another server.' })
140 // Check if the user can delete the video channel
141 // The user can delete it if s/he is an admin
142 // Or if s/he is the video channel's account
143 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
145 .json({ error: 'Cannot remove video channel of another user' })
154 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
155 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
159 .json({ error: 'Cannot remove the last channel of this user' })