]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param, query } from 'express-validator/check' | |
3 | import { isBooleanValid, isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' | |
4 | import { doesVideoExist } from '../../../helpers/custom-validators/videos' | |
5 | import { logger } from '../../../helpers/logger' | |
6 | import { areValidationErrors } from '../utils' | |
7 | import { | |
8 | doesVideoBlacklistExist, | |
9 | isVideoBlacklistReasonValid, | |
10 | isVideoBlacklistTypeValid | |
11 | } from '../../../helpers/custom-validators/video-blacklist' | |
12 | ||
13 | const videosBlacklistRemoveValidator = [ | |
14 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
15 | ||
16 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
17 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) | |
18 | ||
19 | if (areValidationErrors(req, res)) return | |
20 | if (!await doesVideoExist(req.params.videoId, res)) return | |
21 | if (!await doesVideoBlacklistExist(res.locals.video.id, res)) return | |
22 | ||
23 | return next() | |
24 | } | |
25 | ] | |
26 | ||
27 | const videosBlacklistAddValidator = [ | |
28 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
29 | body('unfederate') | |
30 | .optional() | |
31 | .toBoolean() | |
32 | .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'), | |
33 | body('reason') | |
34 | .optional() | |
35 | .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'), | |
36 | ||
37 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
38 | logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params }) | |
39 | ||
40 | if (areValidationErrors(req, res)) return | |
41 | if (!await doesVideoExist(req.params.videoId, res)) return | |
42 | ||
43 | const video = res.locals.video | |
44 | if (req.body.unfederate === true && video.remote === true) { | |
45 | return res | |
46 | .status(409) | |
47 | .send({ error: 'You cannot unfederate a remote video.' }) | |
48 | .end() | |
49 | } | |
50 | ||
51 | return next() | |
52 | } | |
53 | ] | |
54 | ||
55 | const videosBlacklistUpdateValidator = [ | |
56 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
57 | body('reason') | |
58 | .optional() | |
59 | .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'), | |
60 | ||
61 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
62 | logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params }) | |
63 | ||
64 | if (areValidationErrors(req, res)) return | |
65 | if (!await doesVideoExist(req.params.videoId, res)) return | |
66 | if (!await doesVideoBlacklistExist(res.locals.video.id, res)) return | |
67 | ||
68 | return next() | |
69 | } | |
70 | ] | |
71 | ||
72 | const videosBlacklistFiltersValidator = [ | |
73 | query('type') | |
74 | .optional() | |
75 | .custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'), | |
76 | ||
77 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
78 | logger.debug('Checking videos blacklist filters query', { parameters: req.query }) | |
79 | ||
80 | if (areValidationErrors(req, res)) return | |
81 | ||
82 | return next() | |
83 | } | |
84 | ] | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | ||
88 | export { | |
89 | videosBlacklistAddValidator, | |
90 | videosBlacklistRemoveValidator, | |
91 | videosBlacklistUpdateValidator, | |
92 | videosBlacklistFiltersValidator | |
93 | } |