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'
5 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
7 isLocalVideoChannelNameExist,
8 isVideoChannelDescriptionValid,
10 isVideoChannelNameValid,
11 isVideoChannelSupportValid
12 } from '../../helpers/custom-validators/video-channels'
13 import { logger } from '../../helpers/logger'
14 import { UserModel } from '../../models/account/user'
15 import { VideoChannelModel } from '../../models/video/video-channel'
16 import { areValidationErrors } from './utils'
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('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
33 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
34 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
36 (req: express.Request, res: express.Response, next: express.NextFunction) => {
37 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
39 if (areValidationErrors(req, res)) return
45 const videoChannelsUpdateValidator = [
46 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
47 body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
48 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
49 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
51 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
52 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
54 if (areValidationErrors(req, res)) return
55 if (!await isVideoChannelExist(req.params.id, res)) return
57 // We need to make additional checks
58 if (res.locals.videoChannel.Actor.isOwned() === false) {
59 return res.status(403)
60 .json({ error: 'Cannot update video channel of another server' })
64 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
65 return res.status(403)
66 .json({ error: 'Cannot update video channel of another user' })
74 const videoChannelsRemoveValidator = [
75 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
77 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
78 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
80 if (areValidationErrors(req, res)) return
81 if (!await isVideoChannelExist(req.params.id, res)) return
83 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
84 if (!await checkVideoChannelIsNotTheLastOne(res)) return
90 const videoChannelsGetValidator = [
91 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
93 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
94 logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
96 if (areValidationErrors(req, res)) return
98 if (!await isVideoChannelExist(req.params.id, res)) return
104 const localVideoChannelValidator = [
105 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
107 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
108 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
110 if (areValidationErrors(req, res)) return
111 if (!await isLocalVideoChannelNameExist(req.params.name, res)) return
117 // ---------------------------------------------------------------------------
120 listVideoAccountChannelsValidator,
121 videoChannelsAddValidator,
122 videoChannelsUpdateValidator,
123 videoChannelsRemoveValidator,
124 videoChannelsGetValidator,
125 localVideoChannelValidator
128 // ---------------------------------------------------------------------------
130 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
131 if (videoChannel.Actor.isOwned() === false) {
133 .json({ error: 'Cannot remove video channel of another server.' })
139 // Check if the user can delete the video channel
140 // The user can delete it if s/he is an admin
141 // Or if s/he is the video channel's account
142 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
144 .json({ error: 'Cannot remove video channel of another user' })
153 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
154 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
158 .json({ error: 'Cannot remove the last channel of this user' })