]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-blacklist.ts
Fix big play button
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-blacklist.ts
1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
4 import { isBooleanValid, isIdOrUUIDValid, 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 } from '../shared'
8
9 const videosBlacklistRemoveValidator = [
10 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid 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 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
25 body('unfederate')
26 .optional()
27 .customSanitizer(toBooleanOrNull)
28 .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
29 body('reason')
30 .optional()
31 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
32
33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
35
36 if (areValidationErrors(req, res)) return
37 if (!await doesVideoExist(req.params.videoId, res)) return
38
39 const video = res.locals.videoAll
40 if (req.body.unfederate === true && video.remote === true) {
41 return res.fail({
42 status: HttpStatusCode.CONFLICT_409,
43 message: 'You cannot unfederate a remote video.'
44 })
45 }
46
47 return next()
48 }
49 ]
50
51 const videosBlacklistUpdateValidator = [
52 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
53 body('reason')
54 .optional()
55 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
56
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
59
60 if (areValidationErrors(req, res)) return
61 if (!await doesVideoExist(req.params.videoId, res)) return
62 if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
63
64 return next()
65 }
66 ]
67
68 const videosBlacklistFiltersValidator = [
69 query('type')
70 .optional()
71 .customSanitizer(toIntOrNull)
72 .custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'),
73 query('search')
74 .optional()
75 .not()
76 .isEmpty().withMessage('Should have a valid search'),
77
78 (req: express.Request, res: express.Response, next: express.NextFunction) => {
79 logger.debug('Checking videos blacklist filters query', { parameters: req.query })
80
81 if (areValidationErrors(req, res)) return
82
83 return next()
84 }
85 ]
86
87 // ---------------------------------------------------------------------------
88
89 export {
90 videosBlacklistAddValidator,
91 videosBlacklistRemoveValidator,
92 videosBlacklistUpdateValidator,
93 videosBlacklistFiltersValidator
94 }