]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1import * as express from 'express'
2import { body, param, query } from 'express-validator'
3import { isBooleanValid, isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
4import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
5import { logger } from '../../../helpers/logger'
6import { doesVideoBlacklistExist, doesVideoExist } from '../../../helpers/middlewares'
7import { areValidationErrors } from '../utils'
8import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
9
10const 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
24const 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
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'),
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
69const 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
90export {
91 videosBlacklistAddValidator,
92 videosBlacklistRemoveValidator,
93 videosBlacklistUpdateValidator,
94 videosBlacklistFiltersValidator
95}