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'
17 const listVideoAccountChannelsValidator = [
18 param('accountName').exists().withMessage('Should have a valid account name'),
20 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
23 if (areValidationErrors(req, res)) return
24 if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
30 const videoChannelsAddValidator = [
31 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
32 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
33 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
35 (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
38 if (areValidationErrors(req, res)) return
44 const videoChannelsUpdateValidator = [
45 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
46 body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
47 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
48 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
53 if (areValidationErrors(req, res)) return
54 if (!await isVideoChannelExist(req.params.id, res)) return
56 // We need to make additional checks
57 if (res.locals.videoChannel.Actor.isOwned() === false) {
58 return res.status(403)
59 .json({ error: 'Cannot update video channel of another server' })
63 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
64 return res.status(403)
65 .json({ error: 'Cannot update video channel of another user' })
73 const videoChannelsRemoveValidator = [
74 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
79 if (areValidationErrors(req, res)) return
80 if (!await isVideoChannelExist(req.params.id, res)) return
82 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
83 if (!await checkVideoChannelIsNotTheLastOne(res)) return
89 const videoChannelsGetValidator = [
90 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
92 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
93 logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
95 if (areValidationErrors(req, res)) return
97 if (!await isVideoChannelExist(req.params.id, res)) return
103 // ---------------------------------------------------------------------------
106 listVideoAccountChannelsValidator,
107 videoChannelsAddValidator,
108 videoChannelsUpdateValidator,
109 videoChannelsRemoveValidator,
110 videoChannelsGetValidator
113 // ---------------------------------------------------------------------------
115 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
116 if (videoChannel.Actor.isOwned() === false) {
118 .json({ error: 'Cannot remove video channel of another server.' })
124 // Check if the user can delete the video channel
125 // The user can delete it if s/he is an admin
126 // Or if s/he is the video channel's account
127 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
129 .json({ error: 'Cannot remove video channel of another user' })
138 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
139 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
143 .json({ error: 'Cannot remove the last channel of this user' })