diff options
Diffstat (limited to 'server/middlewares/validators/video-blacklist.ts')
-rw-r--r-- | server/middlewares/validators/video-blacklist.ts | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/server/middlewares/validators/video-blacklist.ts b/server/middlewares/validators/video-blacklist.ts index 3c8c31519..f1cc04950 100644 --- a/server/middlewares/validators/video-blacklist.ts +++ b/server/middlewares/validators/video-blacklist.ts | |||
@@ -1,35 +1,36 @@ | |||
1 | import { param } from 'express-validator/check' | ||
2 | import * as express from 'express' | 1 | import * as express from 'express' |
3 | 2 | import { param } from 'express-validator/check' | |
3 | import { isIdOrUUIDValid, logger } from '../../helpers' | ||
4 | import { isVideoExist } from '../../helpers/custom-validators/videos' | ||
4 | import { database as db } from '../../initializers/database' | 5 | import { database as db } from '../../initializers/database' |
5 | import { checkErrors } from './utils' | 6 | import { VideoInstance } from '../../models/video/video-interface' |
6 | import { logger, isIdOrUUIDValid, checkVideoExists } from '../../helpers' | 7 | import { areValidationErrors } from './utils' |
7 | 8 | ||
8 | const videosBlacklistRemoveValidator = [ | 9 | const videosBlacklistRemoveValidator = [ |
9 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | 10 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), |
10 | 11 | ||
11 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | 12 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
12 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) | 13 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) |
13 | 14 | ||
14 | checkErrors(req, res, () => { | 15 | if (areValidationErrors(req, res)) return |
15 | checkVideoExists(req.params.videoId, res, () => { | 16 | if (!await isVideoExist(req.params.videoId, res)) return |
16 | checkVideoIsBlacklisted(req, res, next) | 17 | if (!await checkVideoIsBlacklisted(res.locals.video, res)) return |
17 | }) | 18 | |
18 | }) | 19 | return next() |
19 | } | 20 | } |
20 | ] | 21 | ] |
21 | 22 | ||
22 | const videosBlacklistAddValidator = [ | 23 | const videosBlacklistAddValidator = [ |
23 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | 24 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), |
24 | 25 | ||
25 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | 26 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
26 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) | 27 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) |
27 | 28 | ||
28 | checkErrors(req, res, () => { | 29 | if (areValidationErrors(req, res)) return |
29 | checkVideoExists(req.params.videoId, res, () => { | 30 | if (!await isVideoExist(req.params.videoId, res)) return |
30 | checkVideoIsBlacklistable(req, res, next) | 31 | if (!checkVideoIsBlacklistable(res.locals.video, res)) return |
31 | }) | 32 | |
32 | }) | 33 | return next() |
33 | } | 34 | } |
34 | ] | 35 | ] |
35 | 36 | ||
@@ -41,27 +42,27 @@ export { | |||
41 | } | 42 | } |
42 | // --------------------------------------------------------------------------- | 43 | // --------------------------------------------------------------------------- |
43 | 44 | ||
44 | function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { | 45 | function checkVideoIsBlacklistable (video: VideoInstance, res: express.Response) { |
45 | if (res.locals.video.isOwned() === true) { | 46 | if (video.isOwned() === true) { |
46 | return res.status(403) | 47 | res.status(403) |
47 | .json({ error: 'Cannot blacklist a local video' }) | 48 | .json({ error: 'Cannot blacklist a local video' }) |
48 | .end() | 49 | .end() |
50 | |||
51 | return false | ||
49 | } | 52 | } |
50 | 53 | ||
51 | callback() | 54 | return true |
52 | } | 55 | } |
53 | 56 | ||
54 | function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) { | 57 | async function checkVideoIsBlacklisted (video: VideoInstance, res: express.Response) { |
55 | db.BlacklistedVideo.loadByVideoId(res.locals.video.id) | 58 | const blacklistedVideo = await db.BlacklistedVideo.loadByVideoId(video.id) |
56 | .then(blacklistedVideo => { | 59 | if (!blacklistedVideo) { |
57 | if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found') | 60 | res.status(404) |
61 | .send('Blacklisted video not found') | ||
58 | 62 | ||
59 | res.locals.blacklistedVideo = blacklistedVideo | 63 | return false |
64 | } | ||
60 | 65 | ||
61 | callback() | 66 | res.locals.blacklistedVideo = blacklistedVideo |
62 | }) | 67 | return true |
63 | .catch(err => { | ||
64 | logger.error('Error in blacklistRemove request validator', { error: err }) | ||
65 | return res.sendStatus(500) | ||
66 | }) | ||
67 | } | 68 | } |