1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { MChannelAccountDefault, MUser } from '@server/types/models'
4 import { UserRight } from '../../../../shared'
5 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
6 import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
8 isVideoChannelDescriptionValid,
9 isVideoChannelDisplayNameValid,
10 isVideoChannelSupportValid,
11 isVideoChannelUsernameValid
12 } from '../../../helpers/custom-validators/video-channels'
13 import { logger } from '../../../helpers/logger'
14 import { ActorModel } from '../../../models/actor/actor'
15 import { VideoChannelModel } from '../../../models/video/video-channel'
16 import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared'
17 import { CONFIG } from '@server/initializers/config'
19 const videoChannelsAddValidator = [
20 body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
21 body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
22 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
23 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
28 if (areValidationErrors(req, res)) return
30 const actor = await ActorModel.loadLocalByName(req.body.name)
33 status: HttpStatusCode.CONFLICT_409,
34 message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
39 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
40 if (count >= CONFIG.VIDEO_CHANNELS.MAX_PER_USER) {
41 res.fail({ message: `You cannot create more than ${CONFIG.VIDEO_CHANNELS.MAX_PER_USER} channels` })
49 const videoChannelsUpdateValidator = [
50 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
53 .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
56 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
59 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
60 body('bulkVideosSupportUpdate')
62 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
64 (req: express.Request, res: express.Response, next: express.NextFunction) => {
65 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
67 if (areValidationErrors(req, res)) return
73 const videoChannelsRemoveValidator = [
74 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
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 doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
82 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
83 if (!await checkVideoChannelIsNotTheLastOne(res)) return
89 const videoChannelsNameWithHostValidator = [
90 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
92 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
93 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
95 if (areValidationErrors(req, res)) return
97 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
103 const localVideoChannelValidator = [
104 param('name').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid video channel name'),
106 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
107 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
109 if (areValidationErrors(req, res)) return
110 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
116 const videoChannelStatsValidator = [
119 .customSanitizer(toBooleanOrNull)
120 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
122 (req: express.Request, res: express.Response, next: express.NextFunction) => {
123 if (areValidationErrors(req, res)) return
128 const videoChannelsListValidator = [
129 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
131 (req: express.Request, res: express.Response, next: express.NextFunction) => {
132 logger.debug('Checking video channels search query', { parameters: req.query })
134 if (areValidationErrors(req, res)) return
140 // ---------------------------------------------------------------------------
143 videoChannelsAddValidator,
144 videoChannelsUpdateValidator,
145 videoChannelsRemoveValidator,
146 videoChannelsNameWithHostValidator,
147 videoChannelsListValidator,
148 localVideoChannelValidator,
149 videoChannelStatsValidator
152 // ---------------------------------------------------------------------------
154 function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
155 if (videoChannel.Actor.isOwned() === false) {
157 status: HttpStatusCode.FORBIDDEN_403,
158 message: 'Cannot remove video channel of another server.'
163 // Check if the user can delete the video channel
164 // The user can delete it if s/he is an admin
165 // Or if s/he is the video channel's account
166 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
168 status: HttpStatusCode.FORBIDDEN_403,
169 message: 'Cannot remove video channel of another user'
177 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
178 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
182 status: HttpStatusCode.CONFLICT_409,
183 message: 'Cannot remove the last channel of this user'