2017-10-10 10:02:18 +02:00
|
|
|
import * as express from 'express'
|
2019-07-25 16:23:44 +02:00
|
|
|
import { body, param, query } from 'express-validator'
|
|
|
|
import { isBooleanValid, isIdOrUUIDValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
|
2018-10-05 11:15:06 +02:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { areValidationErrors } from '../utils'
|
2019-07-23 10:40:39 +02:00
|
|
|
import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
|
|
|
|
import { doesVideoBlacklistExist, doesVideoExist } from '../../../helpers/middlewares'
|
2017-10-10 10:02:18 +02:00
|
|
|
|
|
|
|
const videosBlacklistRemoveValidator = [
|
2017-10-24 19:41:09 +02:00
|
|
|
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
|
2017-10-10 10:02:18 +02:00
|
|
|
|
2017-11-27 17:30:46 +01:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-10-10 10:02:18 +02:00
|
|
|
logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
|
|
|
|
|
2017-11-27 17:30:46 +01:00
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesVideoExist(req.params.videoId, res)) return
|
2019-08-15 11:53:26 +02:00
|
|
|
if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
|
2017-11-27 17:30:46 +01:00
|
|
|
|
|
|
|
return next()
|
2017-10-10 10:02:18 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const videosBlacklistAddValidator = [
|
2017-10-24 19:41:09 +02:00
|
|
|
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
|
2019-01-10 15:39:51 +01:00
|
|
|
body('unfederate')
|
|
|
|
.optional()
|
2019-07-25 16:23:44 +02:00
|
|
|
.customSanitizer(toBooleanOrNull)
|
2019-01-10 15:39:51 +01:00
|
|
|
.custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
|
2018-08-13 16:57:13 +02:00
|
|
|
body('reason')
|
|
|
|
.optional()
|
|
|
|
.custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
|
2017-10-10 10:02:18 +02:00
|
|
|
|
2017-11-27 17:30:46 +01:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2018-08-13 16:57:13 +02:00
|
|
|
logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
|
2017-10-10 10:02:18 +02:00
|
|
|
|
2017-11-27 17:30:46 +01:00
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesVideoExist(req.params.videoId, res)) return
|
2017-11-27 17:30:46 +01:00
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
const video = res.locals.videoAll
|
2019-01-10 15:39:51 +01:00
|
|
|
if (req.body.unfederate === true && video.remote === true) {
|
|
|
|
return res
|
|
|
|
.status(409)
|
|
|
|
.send({ error: 'You cannot unfederate a remote video.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2017-11-27 17:30:46 +01:00
|
|
|
return next()
|
2017-10-10 10:02:18 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
const videosBlacklistUpdateValidator = [
|
|
|
|
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
|
|
|
|
body('reason')
|
|
|
|
.optional()
|
|
|
|
.custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
|
2017-10-10 10:02:18 +02:00
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
|
2017-10-10 10:02:18 +02:00
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesVideoExist(req.params.videoId, res)) return
|
2019-08-15 11:53:26 +02:00
|
|
|
if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
|
2017-11-27 17:30:46 +01:00
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
return next()
|
2017-10-10 10:02:18 +02:00
|
|
|
}
|
2018-08-13 16:57:13 +02:00
|
|
|
]
|
2017-10-10 10:02:18 +02:00
|
|
|
|
2019-04-02 11:26:47 +02:00
|
|
|
const videosBlacklistFiltersValidator = [
|
|
|
|
query('type')
|
|
|
|
.optional()
|
|
|
|
.custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'),
|
2020-04-19 14:11:40 +02:00
|
|
|
query('search')
|
|
|
|
.optional()
|
|
|
|
.not()
|
|
|
|
.isEmpty().withMessage('Should have a valid search'),
|
2019-04-02 11:26:47 +02:00
|
|
|
|
|
|
|
(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 16:57:13 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
2017-10-10 10:02:18 +02:00
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
export {
|
|
|
|
videosBlacklistAddValidator,
|
|
|
|
videosBlacklistRemoveValidator,
|
2019-04-02 11:26:47 +02:00
|
|
|
videosBlacklistUpdateValidator,
|
|
|
|
videosBlacklistFiltersValidator
|
2017-10-10 10:02:18 +02:00
|
|
|
}
|