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 { ActorModel } from '../../../models/actor/actor'
16 import { VideoChannelModel } from '../../../models/video/video-channel'
17 import { areValidationErrors, checkUserQuota, doesVideoChannelNameWithHostExist } from '../shared'
18 import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs'
20 export const videoChannelsAddValidator = [
22 .custom(isVideoChannelUsernameValid),
24 .custom(isVideoChannelDisplayNameValid),
27 .custom(isVideoChannelDescriptionValid),
30 .custom(isVideoChannelSupportValid),
32 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 if (areValidationErrors(req, res)) return
35 const actor = await ActorModel.loadLocalByName(req.body.name)
38 status: HttpStatusCode.CONFLICT_409,
39 message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
44 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
45 if (count >= CONFIG.VIDEO_CHANNELS.MAX_PER_USER) {
46 res.fail({ message: `You cannot create more than ${CONFIG.VIDEO_CHANNELS.MAX_PER_USER} channels` })
54 export const videoChannelsUpdateValidator = [
60 .custom(isVideoChannelDisplayNameValid),
63 .custom(isVideoChannelDescriptionValid),
66 .custom(isVideoChannelSupportValid),
67 body('bulkVideosSupportUpdate')
69 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
71 (req: express.Request, res: express.Response, next: express.NextFunction) => {
72 if (areValidationErrors(req, res)) return
78 export const videoChannelsRemoveValidator = [
79 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
80 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
86 export const videoChannelsNameWithHostValidator = [
90 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
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 boolean'),
129 (req: express.Request, res: express.Response, next: express.NextFunction) => {
130 if (areValidationErrors(req, res)) return
135 export const videoChannelsListValidator = [
140 (req: express.Request, res: express.Response, next: express.NextFunction) => {
141 if (areValidationErrors(req, res)) return
147 export const videoChannelImportVideosValidator = [
148 body('externalChannelUrl')
151 body('videoChannelSyncId')
155 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
156 if (areValidationErrors(req, res)) return
158 const body: VideosImportInChannelCreate = req.body
160 if (!CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) {
162 status: HttpStatusCode.FORBIDDEN_403,
163 message: 'Channel import is impossible as video upload via HTTP is not enabled on the server'
167 if (body.videoChannelSyncId && !await doesVideoChannelSyncIdExist(body.videoChannelSyncId, res)) return
169 if (res.locals.videoChannelSync && res.locals.videoChannelSync.videoChannelId !== res.locals.videoChannel.id) {
171 status: HttpStatusCode.FORBIDDEN_403,
172 message: 'This channel sync is not owned by this channel'
180 // ---------------------------------------------------------------------------
182 async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
183 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
187 status: HttpStatusCode.CONFLICT_409,
188 message: 'Cannot remove the last channel of this user'