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'
14 import { AccountModel } from '../../models/account/account'
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').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
32 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
34 (req: express.Request, res: express.Response, next: express.NextFunction) => {
35 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
37 if (areValidationErrors(req, res)) return
43 const videoChannelsUpdateValidator = [
44 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
45 body('name').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid name'),
46 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
47 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
49 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
50 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
52 if (areValidationErrors(req, res)) return
53 if (!await isVideoChannelExist(req.params.id, res)) return
55 // We need to make additional checks
56 if (res.locals.videoChannel.Actor.isOwned() === false) {
57 return res.status(403)
58 .json({ error: 'Cannot update video channel of another server' })
62 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
63 return res.status(403)
64 .json({ error: 'Cannot update video channel of another user' })
72 const videoChannelsRemoveValidator = [
73 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
75 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
76 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
78 if (areValidationErrors(req, res)) return
79 if (!await isVideoChannelExist(req.params.id, res)) return
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
96 if (!await isVideoChannelExist(req.params.id, res)) return
102 // ---------------------------------------------------------------------------
105 listVideoAccountChannelsValidator,
106 videoChannelsAddValidator,
107 videoChannelsUpdateValidator,
108 videoChannelsRemoveValidator,
109 videoChannelsGetValidator
112 // ---------------------------------------------------------------------------
114 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
115 if (videoChannel.Actor.isOwned() === false) {
117 .json({ error: 'Cannot remove video channel of another server.' })
123 // Check if the user can delete the video channel
124 // The user can delete it if s/he is an admin
125 // Or if s/he is the video channel's account
126 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
128 .json({ error: 'Cannot remove video channel of another user' })
137 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
138 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
142 .json({ error: 'Cannot remove the last channel of this user' })