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 = [
23 .custom(isVideoChannelUsernameValid),
25 .custom(isVideoChannelDisplayNameValid),
28 .custom(isVideoChannelDescriptionValid),
31 .custom(isVideoChannelSupportValid),
33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
36 if (areValidationErrors(req, res)) return
38 const actor = await ActorModel.loadLocalByName(req.body.name)
41 status: HttpStatusCode.CONFLICT_409,
42 message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
47 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
48 if (count >= CONFIG.VIDEO_CHANNELS.MAX_PER_USER) {
49 res.fail({ message: `You cannot create more than ${CONFIG.VIDEO_CHANNELS.MAX_PER_USER} channels` })
57 export const videoChannelsUpdateValidator = [
63 .custom(isVideoChannelDisplayNameValid),
66 .custom(isVideoChannelDescriptionValid),
69 .custom(isVideoChannelSupportValid),
70 body('bulkVideosSupportUpdate')
72 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
74 (req: express.Request, res: express.Response, next: express.NextFunction) => {
75 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
77 if (areValidationErrors(req, res)) return
83 export const videoChannelsRemoveValidator = [
84 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
85 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
87 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
93 export const videoChannelsNameWithHostValidator = [
97 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
98 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
100 if (areValidationErrors(req, res)) return
102 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
108 export const ensureIsLocalChannel = [
109 (req: express.Request, res: express.Response, next: express.NextFunction) => {
110 if (res.locals.videoChannel.Actor.isOwned() === false) {
112 status: HttpStatusCode.FORBIDDEN_403,
113 message: 'This channel is not owned.'
121 export const ensureChannelOwnerCanUpload = [
122 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
123 const channel = res.locals.videoChannel
124 const user = { id: channel.Account.userId }
126 if (!await checkUserQuota(user, 1, res)) return
132 export const videoChannelStatsValidator = [
135 .customSanitizer(toBooleanOrNull)
136 .custom(isBooleanValid).withMessage('Should have a valid stats flag boolean'),
138 (req: express.Request, res: express.Response, next: express.NextFunction) => {
139 if (areValidationErrors(req, res)) return
144 export const videoChannelsListValidator = [
149 (req: express.Request, res: express.Response, next: express.NextFunction) => {
150 logger.debug('Checking video channels search query', { parameters: req.query })
152 if (areValidationErrors(req, res)) return
158 export const videoChannelImportVideosValidator = [
159 body('externalChannelUrl')
162 body('videoChannelSyncId')
166 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
167 logger.debug('Checking videoChannelImport parameters', { parameters: req.body })
169 if (areValidationErrors(req, res)) return
171 const body: VideosImportInChannelCreate = req.body
173 if (!CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) {
175 status: HttpStatusCode.FORBIDDEN_403,
176 message: 'Channel import is impossible as video upload via HTTP is not enabled on the server'
180 if (body.videoChannelSyncId && !await doesVideoChannelSyncIdExist(body.videoChannelSyncId, res)) return
186 // ---------------------------------------------------------------------------
188 async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
189 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
193 status: HttpStatusCode.CONFLICT_409,
194 message: 'Cannot remove the last channel of this user'