]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-blacklist.ts
Fix big play button
[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'
10363c74 3import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
a02b93ce 4import { isBooleanValid, isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
3e753302 5import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
a02b93ce 6import { logger } from '../../../helpers/logger'
10363c74 7import { areValidationErrors, doesVideoBlacklistExist, doesVideoExist } from '../shared'
35bf0c83
C
8
9const videosBlacklistRemoveValidator = [
72c7248b 10 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid 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
23const videosBlacklistAddValidator = [
72c7248b 24 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
5abb9fbb
C
25 body('unfederate')
26 .optional()
c8861d5d 27 .customSanitizer(toBooleanOrNull)
5abb9fbb 28 .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
26b7305a
C
29 body('reason')
30 .optional()
31 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
35bf0c83 32
a2431b7d 33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26b7305a 34 logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
35bf0c83 35
a2431b7d 36 if (areValidationErrors(req, res)) return
0f6acda1 37 if (!await doesVideoExist(req.params.videoId, res)) return
a2431b7d 38
453e83ea 39 const video = res.locals.videoAll
5abb9fbb 40 if (req.body.unfederate === true && video.remote === true) {
76148b27
RK
41 return res.fail({
42 status: HttpStatusCode.CONFLICT_409,
43 message: 'You cannot unfederate a remote video.'
44 })
5abb9fbb
C
45 }
46
a2431b7d 47 return next()
35bf0c83
C
48 }
49]
50
26b7305a
C
51const 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'),
35bf0c83 56
26b7305a
C
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
35bf0c83 59
26b7305a 60 if (areValidationErrors(req, res)) return
0f6acda1 61 if (!await doesVideoExist(req.params.videoId, res)) return
453e83ea 62 if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
a2431b7d 63
26b7305a 64 return next()
35bf0c83 65 }
26b7305a 66]
35bf0c83 67
7ccddd7b
JM
68const videosBlacklistFiltersValidator = [
69 query('type')
a02b93ce
C
70 .optional()
71 .customSanitizer(toIntOrNull)
7ccddd7b 72 .custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'),
e0a92917
RK
73 query('search')
74 .optional()
75 .not()
76 .isEmpty().withMessage('Should have a valid search'),
7ccddd7b
JM
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
26b7305a 87// ---------------------------------------------------------------------------
35bf0c83 88
26b7305a
C
89export {
90 videosBlacklistAddValidator,
91 videosBlacklistRemoveValidator,
7ccddd7b
JM
92 videosBlacklistUpdateValidator,
93 videosBlacklistFiltersValidator
35bf0c83 94}