-import { param } from 'express-validator/check'
import * as express from 'express'
-
-import { database as db } from '../../initializers/database'
-import { checkErrors } from './utils'
-import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers'
+import { body, param } from 'express-validator/check'
+import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
+import { isVideoExist } from '../../helpers/custom-validators/videos'
+import { logger } from '../../helpers/logger'
+import { areValidationErrors } from './utils'
+import { isVideoBlacklistExist, isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist'
const videosBlacklistRemoveValidator = [
- param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
+ param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
- (req: express.Request, res: express.Response, next: express.NextFunction) => {
+ async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
- checkErrors(req, res, () => {
- checkVideoExists(req.params.videoId, res, () => {
- checkVideoIsBlacklisted(req, res, next)
- })
- })
+ if (areValidationErrors(req, res)) return
+ if (!await isVideoExist(req.params.videoId, res)) return
+ if (!await isVideoBlacklistExist(res.locals.video.id, res)) return
+
+ return next()
}
]
const videosBlacklistAddValidator = [
- param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
+ param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
+ body('reason')
+ .optional()
+ .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
- (req: express.Request, res: express.Response, next: express.NextFunction) => {
- logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
+ async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+ logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
- checkErrors(req, res, () => {
- checkVideoExists(req.params.videoId, res, () => {
- checkVideoIsBlacklistable(req, res, next)
- })
- })
+ if (areValidationErrors(req, res)) return
+ if (!await isVideoExist(req.params.videoId, res)) return
+
+ return next()
}
]
-// ---------------------------------------------------------------------------
-
-export {
- videosBlacklistAddValidator,
- videosBlacklistRemoveValidator
-}
-// ---------------------------------------------------------------------------
+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'),
-function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
- if (res.locals.video.isOwned() === true) {
- return res.status(403)
- .json({ error: 'Cannot blacklist a local video' })
- .end()
- }
+ async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+ logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
- callback()
-}
+ if (areValidationErrors(req, res)) return
+ if (!await isVideoExist(req.params.videoId, res)) return
+ if (!await isVideoBlacklistExist(res.locals.video.id, res)) return
-function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) {
- db.BlacklistedVideo.loadByVideoId(res.locals.video.id)
- .then(blacklistedVideo => {
- if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found')
+ return next()
+ }
+]
- res.locals.blacklistedVideo = blacklistedVideo
+// ---------------------------------------------------------------------------
- callback()
- })
- .catch(err => {
- logger.error('Error in blacklistRemove request validator', { error: err })
- return res.sendStatus(500)
- })
+export {
+ videosBlacklistAddValidator,
+ videosBlacklistRemoveValidator,
+ videosBlacklistUpdateValidator
}