diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-13 16:57:13 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-14 09:27:18 +0200 |
commit | 26b7305a232e547709f433a6edf700bf495935d8 (patch) | |
tree | b5676090c61df72f864735bcc881d5ee256cffbd /server/middlewares/validators | |
parent | efc9e8450a8bbeeef9cd18e3ad6037abc0f815c3 (diff) | |
download | PeerTube-26b7305a232e547709f433a6edf700bf495935d8.tar.gz PeerTube-26b7305a232e547709f433a6edf700bf495935d8.tar.zst PeerTube-26b7305a232e547709f433a6edf700bf495935d8.zip |
Add blacklist reason field
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r-- | server/middlewares/validators/video-blacklist.ts | 55 |
1 files changed, 24 insertions, 31 deletions
diff --git a/server/middlewares/validators/video-blacklist.ts b/server/middlewares/validators/video-blacklist.ts index 3c1ef1b4e..95a2b9f17 100644 --- a/server/middlewares/validators/video-blacklist.ts +++ b/server/middlewares/validators/video-blacklist.ts | |||
@@ -1,11 +1,10 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { param } from 'express-validator/check' | 2 | import { body, param } from 'express-validator/check' |
3 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' | 3 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' |
4 | import { isVideoExist } from '../../helpers/custom-validators/videos' | 4 | import { isVideoExist } from '../../helpers/custom-validators/videos' |
5 | import { logger } from '../../helpers/logger' | 5 | import { logger } from '../../helpers/logger' |
6 | import { VideoModel } from '../../models/video/video' | ||
7 | import { VideoBlacklistModel } from '../../models/video/video-blacklist' | ||
8 | import { areValidationErrors } from './utils' | 6 | import { areValidationErrors } from './utils' |
7 | import { isVideoBlacklistExist, isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist' | ||
9 | 8 | ||
10 | const videosBlacklistRemoveValidator = [ | 9 | const videosBlacklistRemoveValidator = [ |
11 | 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'), |
@@ -15,7 +14,7 @@ const videosBlacklistRemoveValidator = [ | |||
15 | 14 | ||
16 | if (areValidationErrors(req, res)) return | 15 | if (areValidationErrors(req, res)) return |
17 | if (!await isVideoExist(req.params.videoId, res)) return | 16 | if (!await isVideoExist(req.params.videoId, res)) return |
18 | if (!await checkVideoIsBlacklisted(res.locals.video, res)) return | 17 | if (!await isVideoBlacklistExist(res.locals.video.id, res)) return |
19 | 18 | ||
20 | return next() | 19 | return next() |
21 | } | 20 | } |
@@ -23,47 +22,41 @@ const videosBlacklistRemoveValidator = [ | |||
23 | 22 | ||
24 | const videosBlacklistAddValidator = [ | 23 | const videosBlacklistAddValidator = [ |
25 | 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'), |
25 | body('reason') | ||
26 | .optional() | ||
27 | .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'), | ||
26 | 28 | ||
27 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 29 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
28 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) | 30 | logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params }) |
29 | 31 | ||
30 | if (areValidationErrors(req, res)) return | 32 | if (areValidationErrors(req, res)) return |
31 | if (!await isVideoExist(req.params.videoId, res)) return | 33 | if (!await isVideoExist(req.params.videoId, res)) return |
32 | if (!checkVideoIsBlacklistable(res.locals.video, res)) return | ||
33 | 34 | ||
34 | return next() | 35 | return next() |
35 | } | 36 | } |
36 | ] | 37 | ] |
37 | 38 | ||
38 | // --------------------------------------------------------------------------- | 39 | const videosBlacklistUpdateValidator = [ |
40 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | ||
41 | body('reason') | ||
42 | .optional() | ||
43 | .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'), | ||
39 | 44 | ||
40 | export { | 45 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
41 | videosBlacklistAddValidator, | 46 | logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params }) |
42 | videosBlacklistRemoveValidator | ||
43 | } | ||
44 | // --------------------------------------------------------------------------- | ||
45 | 47 | ||
46 | function checkVideoIsBlacklistable (video: VideoModel, res: express.Response) { | 48 | if (areValidationErrors(req, res)) return |
47 | if (video.isOwned() === true) { | 49 | if (!await isVideoExist(req.params.videoId, res)) return |
48 | res.status(403) | 50 | if (!await isVideoBlacklistExist(res.locals.video.id, res)) return |
49 | .json({ error: 'Cannot blacklist a local video' }) | ||
50 | .end() | ||
51 | 51 | ||
52 | return false | 52 | return next() |
53 | } | 53 | } |
54 | ] | ||
54 | 55 | ||
55 | return true | 56 | // --------------------------------------------------------------------------- |
56 | } | ||
57 | |||
58 | async function checkVideoIsBlacklisted (video: VideoModel, res: express.Response) { | ||
59 | const blacklistedVideo = await VideoBlacklistModel.loadByVideoId(video.id) | ||
60 | if (!blacklistedVideo) { | ||
61 | res.status(404) | ||
62 | .send('Blacklisted video not found') | ||
63 | |||
64 | return false | ||
65 | } | ||
66 | 57 | ||
67 | res.locals.blacklistedVideo = blacklistedVideo | 58 | export { |
68 | return true | 59 | videosBlacklistAddValidator, |
60 | videosBlacklistRemoveValidator, | ||
61 | videosBlacklistUpdateValidator | ||
69 | } | 62 | } |