1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { UserRight } from '../../../shared'
4 import { isAccountIdExist } from '../../helpers/custom-validators/accounts'
5 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
7 isVideoChannelDescriptionValid, isVideoChannelExist,
8 isVideoChannelNameValid, isVideoChannelSupportValid
9 } from '../../helpers/custom-validators/video-channels'
10 import { logger } from '../../helpers/logger'
11 import { UserModel } from '../../models/account/user'
12 import { VideoChannelModel } from '../../models/video/video-channel'
13 import { areValidationErrors } from './utils'
15 const listVideoAccountChannelsValidator = [
16 param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'),
18 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
21 if (areValidationErrors(req, res)) return
22 if (!await isAccountIdExist(req.params.accountId, res)) return
28 const videoChannelsAddValidator = [
29 body('name').custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
30 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
31 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
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'),
46 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
48 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
49 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
51 if (areValidationErrors(req, res)) return
52 if (!await isVideoChannelExist(req.params.id, res)) return
54 // We need to make additional checks
55 if (res.locals.videoChannel.Actor.isOwned() === false) {
56 return res.status(403)
57 .json({ error: 'Cannot update video channel of another server' })
61 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
62 return res.status(403)
63 .json({ error: 'Cannot update video channel of another user' })
71 const videoChannelsRemoveValidator = [
72 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
74 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
75 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
77 if (areValidationErrors(req, res)) return
78 if (!await isVideoChannelExist(req.params.id, res)) return
80 // Check if the user who did the request is able to delete the video
81 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
82 if (!await checkVideoChannelIsNotTheLastOne(res)) return
88 const videoChannelsGetValidator = [
89 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
91 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
92 logger.debug('Checking videoChannelsGet parameters', { parameters: req.params })
94 if (areValidationErrors(req, res)) return
95 if (!await isVideoChannelExist(req.params.id, res)) return
101 // ---------------------------------------------------------------------------
104 listVideoAccountChannelsValidator,
105 videoChannelsAddValidator,
106 videoChannelsUpdateValidator,
107 videoChannelsRemoveValidator,
108 videoChannelsGetValidator
111 // ---------------------------------------------------------------------------
113 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
114 if (videoChannel.Actor.isOwned() === false) {
116 .json({ error: 'Cannot remove video channel of another server.' })
122 // Check if the user can delete the video channel
123 // The user can delete it if s/he is an admin
124 // Or if s/he is the video channel's account
125 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
127 .json({ error: 'Cannot remove video channel of another user' })
136 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
137 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
141 .json({ error: 'Cannot remove the last channel of this user' })