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