]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-blacklist.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-blacklist.ts
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 { logger } from '../../../helpers/logger'
7 import { areValidationErrors, doesVideoBlacklistExist, doesVideoExist, isValidVideoIdParam } from '../shared'
8
9 const videosBlacklistRemoveValidator = [
10 isValidVideoIdParam('videoId'),
11
12 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
13 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
14
15 if (areValidationErrors(req, res)) return
16 if (!await doesVideoExist(req.params.videoId, res)) return
17 if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
18
19 return next()
20 }
21 ]
22
23 const videosBlacklistAddValidator = [
24 isValidVideoIdParam('videoId'),
25
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
52 const videosBlacklistUpdateValidator = [
53 isValidVideoIdParam('videoId'),
54
55 body('reason')
56 .optional()
57 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
58
59 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
60 logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
61
62 if (areValidationErrors(req, res)) return
63 if (!await doesVideoExist(req.params.videoId, res)) return
64 if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
65
66 return next()
67 }
68 ]
69
70 const videosBlacklistFiltersValidator = [
71 query('type')
72 .optional()
73 .customSanitizer(toIntOrNull)
74 .custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'),
75 query('search')
76 .optional()
77 .not()
78 .isEmpty().withMessage('Should have a valid search'),
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
89 // ---------------------------------------------------------------------------
90
91 export {
92 videosBlacklistAddValidator,
93 videosBlacklistRemoveValidator,
94 videosBlacklistUpdateValidator,
95 videosBlacklistFiltersValidator
96 }