From 4e50b6a1c9a3eb261e04ede73241648e6edf21d6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 27 Nov 2017 14:44:51 +0100 Subject: Add shares forward and collection on videos/video channels --- server/middlewares/async.ts | 14 +++++++-- server/middlewares/validators/account.ts | 26 ++-------------- server/middlewares/validators/utils.ts | 16 +++++++++- server/middlewares/validators/video-channels.ts | 41 +++++++++++++++++++++---- server/middlewares/validators/videos.ts | 26 +++++++++++++++- 5 files changed, 88 insertions(+), 35 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/async.ts b/server/middlewares/async.ts index 29ebd169d..9692f9be7 100644 --- a/server/middlewares/async.ts +++ b/server/middlewares/async.ts @@ -1,10 +1,18 @@ -import { Request, Response, NextFunction } from 'express' +import { Request, Response, NextFunction, RequestHandler } from 'express' +import { eachSeries } from 'async' // Syntactic sugar to avoid try/catch in express controllers // Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016 -function asyncMiddleware (fn: (req: Request, res: Response, next: NextFunction) => Promise) { +function asyncMiddleware (fun: RequestHandler | RequestHandler[]) { return (req: Request, res: Response, next: NextFunction) => { - return Promise.resolve(fn(req, res, next)) + if (Array.isArray(fun) === true) { + return eachSeries(fun as RequestHandler[], (f, cb) => { + Promise.resolve(f(req, res, cb)) + .catch(next) + }, next) + } + + return Promise.resolve((fun as RequestHandler)(req, res, next)) .catch(next) } } diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts index 07ae76b63..47ed6a7bb 100644 --- a/server/middlewares/validators/account.ts +++ b/server/middlewares/validators/account.ts @@ -1,9 +1,7 @@ import * as express from 'express' import { param } from 'express-validator/check' import { logger } from '../../helpers' -import { isAccountNameValid } from '../../helpers/custom-validators/accounts' -import { database as db } from '../../initializers/database' -import { AccountInstance } from '../../models' +import { checkLocalAccountNameExists, isAccountNameValid } from '../../helpers/custom-validators/accounts' import { checkErrors } from './utils' const localAccountValidator = [ @@ -13,7 +11,7 @@ const localAccountValidator = [ logger.debug('Checking localAccountValidator parameters', { parameters: req.params }) checkErrors(req, res, () => { - checkLocalAccountExists(req.params.name, res, next) + checkLocalAccountNameExists(req.params.name, res, next) }) } ] @@ -23,23 +21,3 @@ const localAccountValidator = [ export { localAccountValidator } - -// --------------------------------------------------------------------------- - -function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) { - db.Account.loadLocalByName(name) - .then(account => { - if (!account) { - return res.status(404) - .send({ error: 'Account not found' }) - .end() - } - - res.locals.account = account - return callback(null, account) - }) - .catch(err => { - logger.error('Error in account request validator.', err) - return res.sendStatus(500) - }) -} diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts index ea107bbe8..77a1a0d4b 100644 --- a/server/middlewares/validators/utils.ts +++ b/server/middlewares/validators/utils.ts @@ -14,8 +14,22 @@ function checkErrors (req: express.Request, res: express.Response, next: express return next() } +function areValidationErrors (req: express.Request, res: express.Response) { + const errors = validationResult(req) + + if (!errors.isEmpty()) { + logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() }) + res.status(400).json({ errors: errors.mapped() }) + + return true + } + + return false +} + // --------------------------------------------------------------------------- export { - checkErrors + checkErrors, + areValidationErrors } diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts index c6fd3b59d..f30fbf0dc 100644 --- a/server/middlewares/validators/video-channels.ts +++ b/server/middlewares/validators/video-channels.ts @@ -1,13 +1,19 @@ import * as express from 'express' import { body, param } from 'express-validator/check' import { UserRight } from '../../../shared' -import { checkVideoAccountExists } from '../../helpers/custom-validators/accounts' -import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' -import { checkVideoChannelExists, isIdOrUUIDValid } from '../../helpers/index' +import { checkAccountIdExists } from '../../helpers/custom-validators/accounts' +import { isIdValid } from '../../helpers/custom-validators/misc' +import { + checkVideoChannelExists, + isVideoChannelDescriptionValid, + isVideoChannelExistsPromise, + isVideoChannelNameValid +} from '../../helpers/custom-validators/video-channels' +import { isIdOrUUIDValid } from '../../helpers/index' import { logger } from '../../helpers/logger' import { database as db } from '../../initializers' import { UserInstance } from '../../models' -import { checkErrors } from './utils' +import { areValidationErrors, checkErrors } from './utils' const listVideoAccountChannelsValidator = [ param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), @@ -16,7 +22,7 @@ const listVideoAccountChannelsValidator = [ logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) checkErrors(req, res, () => { - checkVideoAccountExists(req.params.accountId, res, next) + checkAccountIdExists(req.params.accountId, res, next) }) } ] @@ -90,6 +96,28 @@ const videoChannelsGetValidator = [ } ] +const videoChannelsShareValidator = [ + param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), + param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoChannelShare parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + if (!await isVideoChannelExistsPromise(req.params.id, res)) return + + const share = await db.VideoChannelShare.load(res.locals.video.id, req.params.accountId) + if (!share) { + return res.status(404) + .end() + } + + res.locals.videoChannelShare = share + + return next() + } +] + // --------------------------------------------------------------------------- export { @@ -97,7 +125,8 @@ export { videoChannelsAddValidator, videoChannelsUpdateValidator, videoChannelsRemoveValidator, - videoChannelsGetValidator + videoChannelsGetValidator, + videoChannelsShareValidator } // --------------------------------------------------------------------------- diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index df0eb7b96..5ffc85210 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -24,7 +24,8 @@ import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers' import { database as db } from '../../initializers/database' import { UserInstance } from '../../models/account/user-interface' import { authenticate } from '../oauth' -import { checkErrors } from './utils' +import { areValidationErrors, checkErrors } from './utils' +import { isVideoExistsPromise } from '../../helpers/index' const videosAddValidator = [ body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage( @@ -230,6 +231,28 @@ const videoRateValidator = [ } ] +const videosShareValidator = [ + param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), + param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoShare parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + if (!await isVideoExistsPromise(req.params.id, res)) return + + const share = await db.VideoShare.load(req.params.accountId, res.locals.video.id) + if (!share) { + return res.status(404) + .end() + } + + res.locals.videoShare = share + + return next() + } +] + // --------------------------------------------------------------------------- export { @@ -238,6 +261,7 @@ export { videosGetValidator, videosRemoveValidator, videosSearchValidator, + videosShareValidator, videoAbuseReportValidator, -- cgit v1.2.3