1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { CONFIG } from '@server/initializers/config'
4 import { MChannelAccountDefault } from '@server/types/models'
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, doesVideoChannelNameWithHostExist } from '../shared'
18 const videoChannelsAddValidator = [
19 body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
20 body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
21 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
22 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
24 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
25 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
27 if (areValidationErrors(req, res)) return
29 const actor = await ActorModel.loadLocalByName(req.body.name)
32 status: HttpStatusCode.CONFLICT_409,
33 message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
38 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
39 if (count >= CONFIG.VIDEO_CHANNELS.MAX_PER_USER) {
40 res.fail({ message: `You cannot create more than ${CONFIG.VIDEO_CHANNELS.MAX_PER_USER} channels` })
48 const videoChannelsUpdateValidator = [
49 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
52 .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
55 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
58 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
59 body('bulkVideosSupportUpdate')
61 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
63 (req: express.Request, res: express.Response, next: express.NextFunction) => {
64 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
66 if (areValidationErrors(req, res)) return
72 const videoChannelsRemoveValidator = [
73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
76 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
82 const videoChannelsNameWithHostValidator = [
83 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
85 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
86 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
88 if (areValidationErrors(req, res)) return
90 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
96 const ensureIsLocalChannel = [
97 (req: express.Request, res: express.Response, next: express.NextFunction) => {
98 if (res.locals.videoChannel.Actor.isOwned() === false) {
100 status: HttpStatusCode.FORBIDDEN_403,
101 message: 'This channel is not owned.'
109 const videoChannelStatsValidator = [
112 .customSanitizer(toBooleanOrNull)
113 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
115 (req: express.Request, res: express.Response, next: express.NextFunction) => {
116 if (areValidationErrors(req, res)) return
121 const videoChannelsListValidator = [
122 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
124 (req: express.Request, res: express.Response, next: express.NextFunction) => {
125 logger.debug('Checking video channels search query', { parameters: req.query })
127 if (areValidationErrors(req, res)) return
133 // ---------------------------------------------------------------------------
136 videoChannelsAddValidator,
137 videoChannelsUpdateValidator,
138 videoChannelsRemoveValidator,
139 videoChannelsNameWithHostValidator,
140 ensureIsLocalChannel,
141 videoChannelsListValidator,
142 videoChannelStatsValidator
145 // ---------------------------------------------------------------------------
147 async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
148 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
152 status: HttpStatusCode.CONFLICT_409,
153 message: 'Cannot remove the last channel of this user'