PeerTube/server/helpers/custom-validators/video-channels.ts

80 lines
2.2 KiB
TypeScript
Raw Normal View History

import * as Bluebird from 'bluebird'
2017-10-24 19:41:09 +02:00
import * as express from 'express'
import 'express-validator'
import 'multer'
import * as validator from 'validator'
2017-10-24 19:41:09 +02:00
import { CONSTRAINTS_FIELDS, database as db } from '../../initializers'
2017-10-24 19:41:09 +02:00
import { VideoChannelInstance } from '../../models'
import { logger } from '../logger'
2017-11-14 10:57:56 +01:00
import { isActivityPubUrlValid } from './index'
import { exists } from './misc'
2017-10-24 19:41:09 +02:00
const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
2017-11-14 10:57:56 +01:00
function isVideoChannelUrlValid (value: string) {
return isActivityPubUrlValid(value)
}
2017-10-24 19:41:09 +02:00
function isVideoChannelDescriptionValid (value: string) {
return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
}
function isVideoChannelNameValid (value: string) {
return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
}
function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) {
let promise: Bluebird<VideoChannelInstance>
2017-10-24 19:41:09 +02:00
if (validator.isInt(id)) {
2017-11-10 14:48:08 +01:00
promise = db.VideoChannel.loadAndPopulateAccount(+id)
2017-10-24 19:41:09 +02:00
} else { // UUID
2017-11-10 14:48:08 +01:00
promise = db.VideoChannel.loadByUUIDAndPopulateAccount(id)
2017-10-24 19:41:09 +02:00
}
promise.then(videoChannel => {
if (!videoChannel) {
return res.status(404)
.json({ error: 'Video channel not found' })
.end()
}
res.locals.videoChannel = videoChannel
callback()
})
.catch(err => {
logger.error('Error in video channel request validator.', err)
return res.sendStatus(500)
})
}
async function isVideoChannelExistsPromise (id: string, res: express.Response) {
let videoChannel: VideoChannelInstance
if (validator.isInt(id)) {
videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id)
} else { // UUID
videoChannel = await db.VideoChannel.loadByUUIDAndPopulateAccount(id)
}
if (!videoChannel) {
res.status(404)
.json({ error: 'Video channel not found' })
.end()
return false
}
res.locals.videoChannel = videoChannel
return true
}
2017-10-24 19:41:09 +02:00
// ---------------------------------------------------------------------------
export {
isVideoChannelDescriptionValid,
2017-11-14 10:57:56 +01:00
checkVideoChannelExists,
isVideoChannelNameValid,
isVideoChannelExistsPromise,
2017-11-14 10:57:56 +01:00
isVideoChannelUrlValid
2017-10-24 19:41:09 +02:00
}