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