1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { UserRight } from '../../../shared'
4 import { isAccountIdExist, isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
5 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
7 isVideoChannelDescriptionValid,
9 isVideoChannelNameValid,
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 { isAvatarFile } from '../../helpers/custom-validators/users'
17 import { CONSTRAINTS_FIELDS } from '../../initializers'
19 const listVideoAccountChannelsValidator = [
20 param('accountName').exists().withMessage('Should have a valid account name'),
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
25 if (areValidationErrors(req, res)) return
26 if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
32 const videoChannelsAddValidator = [
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('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
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 isVideoChannelExist(req.params.id, 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('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
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 isVideoChannelExist(req.params.id, res)) return
84 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
85 if (!await checkVideoChannelIsNotTheLastOne(res)) return
91 const videoChannelsGetValidator = [
92 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
94 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
95 logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
97 if (areValidationErrors(req, res)) return
99 if (!await isVideoChannelExist(req.params.id, res)) return
105 // ---------------------------------------------------------------------------
108 listVideoAccountChannelsValidator,
109 videoChannelsAddValidator,
110 videoChannelsUpdateValidator,
111 videoChannelsRemoveValidator,
112 videoChannelsGetValidator
115 // ---------------------------------------------------------------------------
117 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
118 if (videoChannel.Actor.isOwned() === false) {
120 .json({ error: 'Cannot remove video channel of another server.' })
126 // Check if the user can delete the video channel
127 // The user can delete it if s/he is an admin
128 // Or if s/he is the video channel's account
129 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
131 .json({ error: 'Cannot remove video channel of another user' })
140 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
141 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
145 .json({ error: 'Cannot remove the last channel of this user' })