From 72c7248b6fdcdb2175e726ff51b42e7555f2bd84 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 24 Oct 2017 19:41:09 +0200 Subject: Add video channels --- server/helpers/custom-validators/video-authors.ts | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 server/helpers/custom-validators/video-authors.ts (limited to 'server/helpers/custom-validators/video-authors.ts') diff --git a/server/helpers/custom-validators/video-authors.ts b/server/helpers/custom-validators/video-authors.ts new file mode 100644 index 000000000..48ca9b200 --- /dev/null +++ b/server/helpers/custom-validators/video-authors.ts @@ -0,0 +1,45 @@ +import * as Promise from 'bluebird' +import * as validator from 'validator' +import * as express from 'express' +import 'express-validator' + +import { database as db } from '../../initializers' +import { AuthorInstance } from '../../models' +import { logger } from '../logger' + +import { isUserUsernameValid } from './users' + +function isVideoAuthorNameValid (value: string) { + return isUserUsernameValid(value) +} + +function checkVideoAuthorExists (id: string, res: express.Response, callback: () => void) { + let promise: Promise + if (validator.isInt(id)) { + promise = db.Author.load(+id) + } else { // UUID + promise = db.Author.loadByUUID(id) + } + + promise.then(author => { + if (!author) { + return res.status(404) + .json({ error: 'Video author not found' }) + .end() + } + + res.locals.author = author + callback() + }) + .catch(err => { + logger.error('Error in video author request validator.', err) + return res.sendStatus(500) + }) +} + +// --------------------------------------------------------------------------- + +export { + checkVideoAuthorExists, + isVideoAuthorNameValid +} -- cgit v1.2.3