PeerTube/server/middlewares/validators/videos/video-blacklist.ts

97 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-08-27 07:32:44 -05:00
import express from 'express'
import { body, query } from 'express-validator'
2021-07-16 03:42:24 -05:00
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
import { isBooleanValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
2019-07-23 03:40:39 -05:00
import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
2020-08-06 09:14:58 -05:00
import { logger } from '../../../helpers/logger'
import { areValidationErrors, doesVideoBlacklistExist, doesVideoExist, isValidVideoIdParam } from '../shared'
2017-10-10 03:02:18 -05:00
const videosBlacklistRemoveValidator = [
isValidVideoIdParam('videoId'),
2017-10-10 03:02:18 -05:00
2017-11-27 10:30:46 -06:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2017-10-10 03:02:18 -05:00
logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
2017-11-27 10:30:46 -06:00
if (areValidationErrors(req, res)) return
2019-03-19 03:26:50 -05:00
if (!await doesVideoExist(req.params.videoId, res)) return
2019-08-15 04:53:26 -05:00
if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
2017-11-27 10:30:46 -06:00
return next()
2017-10-10 03:02:18 -05:00
}
]
const videosBlacklistAddValidator = [
isValidVideoIdParam('videoId'),
body('unfederate')
.optional()
2019-07-25 09:23:44 -05:00
.customSanitizer(toBooleanOrNull)
.custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
2018-08-13 09:57:13 -05:00
body('reason')
.optional()
.custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
2017-10-10 03:02:18 -05:00
2017-11-27 10:30:46 -06:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2018-08-13 09:57:13 -05:00
logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
2017-10-10 03:02:18 -05:00
2017-11-27 10:30:46 -06:00
if (areValidationErrors(req, res)) return
2019-03-19 03:26:50 -05:00
if (!await doesVideoExist(req.params.videoId, res)) return
2017-11-27 10:30:46 -06:00
2019-08-15 04:53:26 -05:00
const video = res.locals.videoAll
if (req.body.unfederate === true && video.remote === true) {
return res.fail({
status: HttpStatusCode.CONFLICT_409,
message: 'You cannot unfederate a remote video.'
})
}
2017-11-27 10:30:46 -06:00
return next()
2017-10-10 03:02:18 -05:00
}
]
2018-08-13 09:57:13 -05:00
const videosBlacklistUpdateValidator = [
isValidVideoIdParam('videoId'),
2018-08-13 09:57:13 -05:00
body('reason')
.optional()
.custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
2017-10-10 03:02:18 -05:00
2018-08-13 09:57:13 -05:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
2017-10-10 03:02:18 -05:00
2018-08-13 09:57:13 -05:00
if (areValidationErrors(req, res)) return
2019-03-19 03:26:50 -05:00
if (!await doesVideoExist(req.params.videoId, res)) return
2019-08-15 04:53:26 -05:00
if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
2017-11-27 10:30:46 -06:00
2018-08-13 09:57:13 -05:00
return next()
2017-10-10 03:02:18 -05:00
}
2018-08-13 09:57:13 -05:00
]
2017-10-10 03:02:18 -05:00
const videosBlacklistFiltersValidator = [
query('type')
2020-08-06 09:14:58 -05:00
.optional()
.customSanitizer(toIntOrNull)
.custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'),
query('search')
.optional()
.not()
.isEmpty().withMessage('Should have a valid search'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videos blacklist filters query', { parameters: req.query })
if (areValidationErrors(req, res)) return
return next()
}
]
2018-08-13 09:57:13 -05:00
// ---------------------------------------------------------------------------
2017-10-10 03:02:18 -05:00
2018-08-13 09:57:13 -05:00
export {
videosBlacklistAddValidator,
videosBlacklistRemoveValidator,
videosBlacklistUpdateValidator,
videosBlacklistFiltersValidator
2017-10-10 03:02:18 -05:00
}