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 { VideosImportInChannelCreate } from '@shared/models'
7 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
8 import { isBooleanValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
10 isVideoChannelDescriptionValid,
11 isVideoChannelDisplayNameValid,
12 isVideoChannelSupportValid,
13 isVideoChannelUsernameValid
14 } from '../../../helpers/custom-validators/video-channels'
15 import { logger } from '../../../helpers/logger'
16 import { ActorModel } from '../../../models/actor/actor'
17 import { VideoChannelModel } from '../../../models/video/video-channel'
18 import { areValidationErrors, checkUserQuota, doesVideoChannelNameWithHostExist } from '../shared'
19 import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs'
21 export const videoChannelsAddValidator = [
22 body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
23 body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
24 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
25 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
27 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
30 if (areValidationErrors(req, res)) return
32 const actor = await ActorModel.loadLocalByName(req.body.name)
35 status: HttpStatusCode.CONFLICT_409,
36 message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
41 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
42 if (count >= CONFIG.VIDEO_CHANNELS.MAX_PER_USER) {
43 res.fail({ message: `You cannot create more than ${CONFIG.VIDEO_CHANNELS.MAX_PER_USER} channels` })
51 export const videoChannelsUpdateValidator = [
52 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
55 .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
58 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
61 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
62 body('bulkVideosSupportUpdate')
64 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
66 (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
69 if (areValidationErrors(req, res)) return
75 export const videoChannelsRemoveValidator = [
76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
79 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
85 export const videoChannelsNameWithHostValidator = [
86 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
88 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
89 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
91 if (areValidationErrors(req, res)) return
93 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
99 export const ensureIsLocalChannel = [
100 (req: express.Request, res: express.Response, next: express.NextFunction) => {
101 if (res.locals.videoChannel.Actor.isOwned() === false) {
103 status: HttpStatusCode.FORBIDDEN_403,
104 message: 'This channel is not owned.'
112 export const ensureChannelOwnerCanUpload = [
113 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
114 const channel = res.locals.videoChannel
115 const user = { id: channel.Account.userId }
117 if (!await checkUserQuota(user, 1, res)) return
123 export const videoChannelStatsValidator = [
126 .customSanitizer(toBooleanOrNull)
127 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
129 (req: express.Request, res: express.Response, next: express.NextFunction) => {
130 if (areValidationErrors(req, res)) return
135 export const videoChannelsListValidator = [
136 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
138 (req: express.Request, res: express.Response, next: express.NextFunction) => {
139 logger.debug('Checking video channels search query', { parameters: req.query })
141 if (areValidationErrors(req, res)) return
147 export const videoChannelImportVideosValidator = [
148 body('externalChannelUrl').custom(isUrlValid).withMessage('Should have a valid channel url'),
150 body('videoChannelSyncId')
152 .custom(isIdValid).withMessage('Should have a valid channel sync id'),
154 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
155 logger.debug('Checking videoChannelImport parameters', { parameters: req.body })
157 if (areValidationErrors(req, res)) return
159 const body: VideosImportInChannelCreate = req.body
161 if (!CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) {
163 status: HttpStatusCode.FORBIDDEN_403,
164 message: 'Channel import is impossible as video upload via HTTP is not enabled on the server'
168 if (body.videoChannelSyncId && !await doesVideoChannelSyncIdExist(body.videoChannelSyncId, res)) return
174 // ---------------------------------------------------------------------------
176 async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
177 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
181 status: HttpStatusCode.CONFLICT_409,
182 message: 'Cannot remove the last channel of this user'