1 import * as express from 'express'
2 import 'express-validator'
4 import * as validator from 'validator'
5 import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
6 import { VideoChannelModel } from '../../models/video/video-channel'
7 import { exists } from './misc'
9 const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
11 function isVideoChannelDescriptionValid (value: string) {
12 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
15 function isVideoChannelNameValid (value: string) {
16 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
19 function isVideoChannelSupportValid (value: string) {
20 return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT))
23 async function isLocalVideoChannelNameExist (name: string, res: express.Response) {
24 const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
26 return processVideoChannelExist(videoChannel, res)
29 async function isVideoChannelIdExist (id: string, res: express.Response) {
30 let videoChannel: VideoChannelModel
31 if (validator.isInt(id)) {
32 videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
34 videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount(id)
37 return processVideoChannelExist(videoChannel, res)
40 async function isVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
41 const [ name, host ] = nameWithDomain.split('@')
42 let videoChannel: VideoChannelModel
44 if (!host || host === CONFIG.WEBSERVER.HOST) videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
45 else videoChannel = await VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
47 return processVideoChannelExist(videoChannel, res)
50 // ---------------------------------------------------------------------------
53 isVideoChannelNameWithHostExist,
54 isLocalVideoChannelNameExist,
55 isVideoChannelDescriptionValid,
56 isVideoChannelNameValid,
57 isVideoChannelSupportValid,
61 function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
64 .json({ error: 'Video channel not found' })
70 res.locals.videoChannel = videoChannel