1 import * as Bluebird from 'bluebird'
2 import * as express from 'express'
3 import 'express-validator'
5 import * as validator from 'validator'
7 import { CONSTRAINTS_FIELDS, database as db } from '../../initializers'
8 import { VideoChannelInstance } from '../../models'
9 import { logger } from '../logger'
10 import { isActivityPubUrlValid } from './index'
11 import { exists } from './misc'
13 const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
15 function isVideoChannelUrlValid (value: string) {
16 return isActivityPubUrlValid(value)
19 function isVideoChannelDescriptionValid (value: string) {
20 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
23 function isVideoChannelNameValid (value: string) {
24 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
27 function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) {
28 let promise: Bluebird<VideoChannelInstance>
29 if (validator.isInt(id)) {
30 promise = db.VideoChannel.loadAndPopulateAccount(+id)
32 promise = db.VideoChannel.loadByUUIDAndPopulateAccount(id)
35 promise.then(videoChannel => {
37 return res.status(404)
38 .json({ error: 'Video channel not found' })
42 res.locals.videoChannel = videoChannel
46 logger.error('Error in video channel request validator.', err)
47 return res.sendStatus(500)
51 async function isVideoChannelExistsPromise (id: string, res: express.Response) {
52 let videoChannel: VideoChannelInstance
53 if (validator.isInt(id)) {
54 videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id)
56 videoChannel = await db.VideoChannel.loadByUUIDAndPopulateAccount(id)
61 .json({ error: 'Video channel not found' })
67 res.locals.videoChannel = videoChannel
71 // ---------------------------------------------------------------------------
74 isVideoChannelDescriptionValid,
75 checkVideoChannelExists,
76 isVideoChannelNameValid,
77 isVideoChannelExistsPromise,
78 isVideoChannelUrlValid