1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
4 import { CONFIG } from '@server/initializers/config'
5 import { MChannelAccountDefault } from '@server/types/models'
6 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
7 import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
9 isVideoChannelDescriptionValid,
10 isVideoChannelDisplayNameValid,
11 isVideoChannelSupportValid,
12 isVideoChannelUsernameValid
13 } from '../../../helpers/custom-validators/video-channels'
14 import { logger } from '../../../helpers/logger'
15 import { ActorModel } from '../../../models/actor/actor'
16 import { VideoChannelModel } from '../../../models/video/video-channel'
17 import { areValidationErrors, checkUserQuota, doesVideoChannelNameWithHostExist } from '../shared'
19 export 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 export 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 export const videoChannelsRemoveValidator = [
74 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
75 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
77 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
83 export const videoChannelsNameWithHostValidator = [
84 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
86 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
87 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
89 if (areValidationErrors(req, res)) return
91 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
97 export const ensureIsLocalChannel = [
98 (req: express.Request, res: express.Response, next: express.NextFunction) => {
99 if (res.locals.videoChannel.Actor.isOwned() === false) {
101 status: HttpStatusCode.FORBIDDEN_403,
102 message: 'This channel is not owned.'
110 export const ensureChannelOwnerCanUpload = [
111 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
112 const channel = res.locals.videoChannel
113 const user = { id: channel.Account.userId }
115 if (!await checkUserQuota(user, 1, res)) return
121 export const videoChannelStatsValidator = [
124 .customSanitizer(toBooleanOrNull)
125 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
127 (req: express.Request, res: express.Response, next: express.NextFunction) => {
128 if (areValidationErrors(req, res)) return
133 export const videoChannelsListValidator = [
134 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
136 (req: express.Request, res: express.Response, next: express.NextFunction) => {
137 logger.debug('Checking video channels search query', { parameters: req.query })
139 if (areValidationErrors(req, res)) return
145 export const videoChannelImportVideosValidator = [
146 body('externalChannelUrl').custom(isUrlValid).withMessage('Should have a valid channel url'),
148 (req: express.Request, res: express.Response, next: express.NextFunction) => {
149 logger.debug('Checking videoChannelImport parameters', { parameters: req.body })
151 if (areValidationErrors(req, res)) return
153 if (!CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) {
155 status: HttpStatusCode.FORBIDDEN_403,
156 message: 'Channel import is impossible as video upload via HTTP is not enabled on the server'
164 // ---------------------------------------------------------------------------
166 async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
167 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
171 status: HttpStatusCode.CONFLICT_409,
172 message: 'Cannot remove the last channel of this user'