diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-10 10:02:18 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-10 10:18:16 +0200 |
commit | 35bf0c83c80f59ca79f4b84fac8700f17adeb22d (patch) | |
tree | a9b2106096d6ba04d7219051b17fd32cfbe6a885 /server/middlewares/validators/video-blacklist.ts | |
parent | 769d332177a5b02d5c2ffc134687d3b4ed65bae9 (diff) | |
download | PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.gz PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.zst PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.zip |
Video blacklist refractoring
Diffstat (limited to 'server/middlewares/validators/video-blacklist.ts')
-rw-r--r-- | server/middlewares/validators/video-blacklist.ts | 67 |
1 files changed, 67 insertions, 0 deletions
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 @@ | |||
1 | import { param } from 'express-validator/check' | ||
2 | import * as express from 'express' | ||
3 | |||
4 | import { database as db } from '../../initializers/database' | ||
5 | import { checkErrors } from './utils' | ||
6 | import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers' | ||
7 | |||
8 | const videosBlacklistRemoveValidator = [ | ||
9 | param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | ||
10 | |||
11 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
12 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) | ||
13 | |||
14 | checkErrors(req, res, () => { | ||
15 | checkVideoExists(req.params.videoId, res, () => { | ||
16 | checkVideoIsBlacklisted(req, res, next) | ||
17 | }) | ||
18 | }) | ||
19 | } | ||
20 | ] | ||
21 | |||
22 | const videosBlacklistAddValidator = [ | ||
23 | param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | ||
24 | |||
25 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
26 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) | ||
27 | |||
28 | checkErrors(req, res, () => { | ||
29 | checkVideoExists(req.params.videoId, res, () => { | ||
30 | checkVideoIsBlacklistable(req, res, next) | ||
31 | }) | ||
32 | }) | ||
33 | } | ||
34 | ] | ||
35 | |||
36 | // --------------------------------------------------------------------------- | ||
37 | |||
38 | export { | ||
39 | videosBlacklistAddValidator, | ||
40 | videosBlacklistRemoveValidator | ||
41 | } | ||
42 | // --------------------------------------------------------------------------- | ||
43 | |||
44 | function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { | ||
45 | if (res.locals.video.isOwned() === true) { | ||
46 | return res.status(403) | ||
47 | .json({ error: 'Cannot blacklist a local video' }) | ||
48 | .end() | ||
49 | } | ||
50 | |||
51 | callback() | ||
52 | } | ||
53 | |||
54 | function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) { | ||
55 | db.BlacklistedVideo.loadByVideoId(res.locals.video.id) | ||
56 | .then(blacklistedVideo => { | ||
57 | if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found') | ||
58 | |||
59 | res.locals.blacklistedVideo = blacklistedVideo | ||
60 | |||
61 | callback() | ||
62 | }) | ||
63 | .catch(err => { | ||
64 | logger.error('Error in blacklistRemove request validator', { error: err }) | ||
65 | return res.sendStatus(500) | ||
66 | }) | ||
67 | } | ||