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