From 35bf0c83c80f59ca79f4b84fac8700f17adeb22d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 10 Oct 2017 10:02:18 +0200 Subject: Video blacklist refractoring --- server/middlewares/validators/blacklist.ts | 35 ------------- server/middlewares/validators/index.ts | 2 +- server/middlewares/validators/video-blacklist.ts | 67 ++++++++++++++++++++++++ server/middlewares/validators/videos.ts | 58 ++------------------ 4 files changed, 71 insertions(+), 91 deletions(-) delete mode 100644 server/middlewares/validators/blacklist.ts create mode 100644 server/middlewares/validators/video-blacklist.ts (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/blacklist.ts b/server/middlewares/validators/blacklist.ts deleted file mode 100644 index fe8fa40a4..000000000 --- a/server/middlewares/validators/blacklist.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { param } from 'express-validator/check' -import * as express from 'express' - -import { database as db } from '../../initializers/database' -import { checkErrors } from './utils' -import { logger } from '../../helpers' - -const blacklistRemoveValidator = [ - param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) - - checkErrors(req, res, () => { - db.BlacklistedVideo.loadById(req.params.id) - .then(entry => { - if (!entry) return res.status(404).send('Blacklisted video not found') - - res.locals.blacklistEntryToRemove = entry - - next() - }) - .catch(err => { - logger.error('Error in blacklistRemove request validator', { error: err }) - return res.sendStatus(500) - }) - }) - } -] - -// --------------------------------------------------------------------------- - -export { - blacklistRemoveValidator -} diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index a6198e22c..418fa5f1d 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts @@ -4,4 +4,4 @@ export * from './pods' export * from './sort' export * from './users' export * from './videos' -export * from './blacklist' +export * from './video-blacklist' diff --git a/server/middlewares/validators/video-blacklist.ts b/server/middlewares/validators/video-blacklist.ts new file mode 100644 index 000000000..30c6d4bd9 --- /dev/null +++ b/server/middlewares/validators/video-blacklist.ts @@ -0,0 +1,67 @@ +import { param } from 'express-validator/check' +import * as express from 'express' + +import { database as db } from '../../initializers/database' +import { checkErrors } from './utils' +import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers' + +const videosBlacklistRemoveValidator = [ + param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) + + checkErrors(req, res, () => { + checkVideoExists(req.params.videoId, res, () => { + checkVideoIsBlacklisted(req, res, next) + }) + }) + } +] + +const videosBlacklistAddValidator = [ + param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) + + checkErrors(req, res, () => { + checkVideoExists(req.params.videoId, res, () => { + checkVideoIsBlacklistable(req, res, next) + }) + }) + } +] + +// --------------------------------------------------------------------------- + +export { + videosBlacklistAddValidator, + videosBlacklistRemoveValidator +} +// --------------------------------------------------------------------------- + +function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { + if (res.locals.video.isOwned() === true) { + return res.status(403) + .json({ error: 'Cannot blacklist a local video' }) + .end() + } + + callback() +} + +function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) { + db.BlacklistedVideo.loadByVideoId(res.locals.video.id) + .then(blacklistedVideo => { + if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found') + + res.locals.blacklistedVideo = blacklistedVideo + + callback() + }) + .catch(err => { + logger.error('Error in blacklistRemove request validator', { error: err }) + return res.sendStatus(500) + }) +} diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index 5f213f974..deed07524 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -1,7 +1,5 @@ import { body, param, query } from 'express-validator/check' import * as express from 'express' -import * as Promise from 'bluebird' -import * as validator from 'validator' import { database as db } from '../../initializers/database' import { checkErrors } from './utils' @@ -20,9 +18,9 @@ import { isVideoIdOrUUIDValid, isVideoAbuseReasonValid, isVideoRatingTypeValid, - getDurationFromVideoFile + getDurationFromVideoFile, + checkVideoExists } from '../../helpers' -import { VideoInstance } from '../../models' const videosAddValidator = [ body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'), @@ -186,20 +184,6 @@ const videoRateValidator = [ } ] -const videosBlacklistValidator = [ - param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) - - checkErrors(req, res, () => { - checkVideoExists(req.params.id, res, () => { - checkVideoIsBlacklistable(req, res, next) - }) - }) - } -] - // --------------------------------------------------------------------------- export { @@ -211,37 +195,11 @@ export { videoAbuseReportValidator, - videoRateValidator, - - videosBlacklistValidator + videoRateValidator } // --------------------------------------------------------------------------- -function checkVideoExists (id: string, res: express.Response, callback: () => void) { - let promise: Promise - if (validator.isInt(id)) { - promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id) - } else { // UUID - promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id) - } - - promise.then(video => { - if (!video) { - return res.status(404) - .json({ error: 'Video not found' }) - .end() - } - - res.locals.video = video - callback() - }) - .catch(err => { - logger.error('Error in video request validator.', err) - return res.sendStatus(500) - }) -} - function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) { // Retrieve the user who did the request db.User.loadById(userId) @@ -269,13 +227,3 @@ function checkUserCanDeleteVideo (userId: number, res: express.Response, callbac return res.sendStatus(500) }) } - -function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { - if (res.locals.video.isOwned() === true) { - return res.status(403) - .json({ error: 'Cannot blacklist a local video' }) - .end() - } - - callback() -} -- cgit v1.2.3