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