]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-blacklist.ts
refactor API errors to standard error format
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-blacklist.ts
CommitLineData
35bf0c83 1import * as express from 'express'
c8861d5d 2import { body, param, query } from 'express-validator'
a02b93ce 3import { isBooleanValid, isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
3e753302 4import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
a02b93ce 5import { logger } from '../../../helpers/logger'
3e753302 6import { doesVideoBlacklistExist, doesVideoExist } from '../../../helpers/middlewares'
a02b93ce 7import { areValidationErrors } from '../utils'
2d53be02 8import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
35bf0c83
C
9
10const videosBlacklistRemoveValidator = [
72c7248b 11 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
35bf0c83 12
a2431b7d 13 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
35bf0c83
C
14 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
15
a2431b7d 16 if (areValidationErrors(req, res)) return
0f6acda1 17 if (!await doesVideoExist(req.params.videoId, res)) return
453e83ea 18 if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
a2431b7d
C
19
20 return next()
35bf0c83
C
21 }
22]
23
24const videosBlacklistAddValidator = [
72c7248b 25 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
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()
32 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
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
C
52const 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'),
35bf0c83 57
26b7305a
C
58 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
59 logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
35bf0c83 60
26b7305a 61 if (areValidationErrors(req, res)) return
0f6acda1 62 if (!await doesVideoExist(req.params.videoId, res)) return
453e83ea 63 if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
a2431b7d 64
26b7305a 65 return next()
35bf0c83 66 }
26b7305a 67]
35bf0c83 68
7ccddd7b
JM
69const videosBlacklistFiltersValidator = [
70 query('type')
a02b93ce
C
71 .optional()
72 .customSanitizer(toIntOrNull)
7ccddd7b 73 .custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'),
e0a92917
RK
74 query('search')
75 .optional()
76 .not()
77 .isEmpty().withMessage('Should have a valid search'),
7ccddd7b
JM
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
26b7305a 88// ---------------------------------------------------------------------------
35bf0c83 89
26b7305a
C
90export {
91 videosBlacklistAddValidator,
92 videosBlacklistRemoveValidator,
7ccddd7b
JM
93 videosBlacklistUpdateValidator,
94 videosBlacklistFiltersValidator
35bf0c83 95}