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