1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { UserRight } from '../../../shared'
4 import { logger } from '../../helpers'
5 import { isAccountIdExist } from '../../helpers/custom-validators/accounts'
6 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
8 isVideoChannelDescriptionValid,
10 isVideoChannelNameValid
11 } from '../../helpers/custom-validators/video-channels'
12 import { UserModel } from '../../models/account/user'
13 import { VideoChannelModel } from '../../models/video/video-channel'
14 import { areValidationErrors } from './utils'
16 const listVideoAccountChannelsValidator = [
17 param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'),
19 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
20 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
22 if (areValidationErrors(req, res)) return
23 if (!await isAccountIdExist(req.params.accountId, res)) return
29 const videoChannelsAddValidator = [
30 body('name').custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
31 body('description').custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
33 (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
36 if (areValidationErrors(req, res)) return
42 const videoChannelsUpdateValidator = [
43 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
44 body('name').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
45 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
47 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
50 if (areValidationErrors(req, res)) return
51 if (!await isVideoChannelExist(req.params.id, res)) return
53 // We need to make additional checks
54 if (res.locals.videoChannel.Actor.isOwned() === false) {
55 return res.status(403)
56 .json({ error: 'Cannot update video channel of another server' })
60 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
61 return res.status(403)
62 .json({ error: 'Cannot update video channel of another user' })
70 const videoChannelsRemoveValidator = [
71 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
76 if (areValidationErrors(req, res)) return
77 if (!await isVideoChannelExist(req.params.id, res)) return
79 // Check if the user who did the request is able to delete the video
80 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
81 if (!await checkVideoChannelIsNotTheLastOne(res)) return
87 const videoChannelsGetValidator = [
88 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
90 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
91 logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
93 if (areValidationErrors(req, res)) return
94 if (!await isVideoChannelExist(req.params.id, res)) return
100 // ---------------------------------------------------------------------------
103 listVideoAccountChannelsValidator,
104 videoChannelsAddValidator,
105 videoChannelsUpdateValidator,
106 videoChannelsRemoveValidator,
107 videoChannelsGetValidator
110 // ---------------------------------------------------------------------------
112 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
113 if (videoChannel.Actor.isOwned() === false) {
115 .json({ error: 'Cannot remove video channel of another server.' })
121 // Check if the user can delete the video channel
122 // The user can delete it if s/he is an admin
123 // Or if s/he is the video channel's account
124 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
126 .json({ error: 'Cannot remove video channel of another user' })
135 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
136 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
140 .json({ error: 'Cannot remove the last channel of this user' })