From 268eebed921ac13a9ce0f4717f4923aa24190657 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 10 Aug 2018 16:54:01 +0200 Subject: Add state and moderationComment for abuses on server side --- server/middlewares/validators/index.ts | 1 + server/middlewares/validators/video-abuses.ts | 71 +++++++++++++++++++++++++++ server/middlewares/validators/videos.ts | 17 ------- 3 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 server/middlewares/validators/video-abuses.ts (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index c5400c8f5..ccbedd57d 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts @@ -7,6 +7,7 @@ export * from './feeds' export * from './sort' export * from './users' export * from './videos' +export * from './video-abuses' export * from './video-blacklist' export * from './video-channels' export * from './webfinger' diff --git a/server/middlewares/validators/video-abuses.ts b/server/middlewares/validators/video-abuses.ts new file mode 100644 index 000000000..f15d55a75 --- /dev/null +++ b/server/middlewares/validators/video-abuses.ts @@ -0,0 +1,71 @@ +import * as express from 'express' +import 'express-validator' +import { body, param } from 'express-validator/check' +import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' +import { isVideoExist } from '../../helpers/custom-validators/videos' +import { logger } from '../../helpers/logger' +import { areValidationErrors } from './utils' +import { + isVideoAbuseExist, + isVideoAbuseModerationCommentValid, + isVideoAbuseReasonValid, + isVideoAbuseStateValid +} from '../../helpers/custom-validators/video-abuses' + +const videoAbuseReportValidator = [ + param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoAbuseReport parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await isVideoExist(req.params.videoId, res)) return + + return next() + } +] + +const videoAbuseGetValidator = [ + param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoAbuseGetValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await isVideoExist(req.params.videoId, res)) return + if (!await isVideoAbuseExist(req.params.id, res.locals.video.id, res)) return + + return next() + } +] + +const videoAbuseUpdateValidator = [ + param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'), + body('state') + .optional() + .custom(isVideoAbuseStateValid).withMessage('Should have a valid video abuse state'), + body('moderationComment') + .optional() + .custom(isVideoAbuseModerationCommentValid).withMessage('Should have a valid video moderation comment'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoAbuseUpdateValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await isVideoExist(req.params.videoId, res)) return + if (!await isVideoAbuseExist(req.params.id, res.locals.video.id, res)) return + + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + videoAbuseReportValidator, + videoAbuseGetValidator, + videoAbuseUpdateValidator +} diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index c812d4677..203a00876 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -14,7 +14,6 @@ import { import { checkUserCanManageVideo, isScheduleVideoUpdatePrivacyValid, - isVideoAbuseReasonValid, isVideoCategoryValid, isVideoChannelOfAccountExist, isVideoDescriptionValid, @@ -174,20 +173,6 @@ const videosRemoveValidator = [ } ] -const videoAbuseReportValidator = [ - param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), - body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'), - - async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoAbuseReport parameters', { parameters: req.body }) - - if (areValidationErrors(req, res)) return - if (!await isVideoExist(req.params.id, res)) return - - return next() - } -] - const videoRateValidator = [ param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'), @@ -299,8 +284,6 @@ export { videosRemoveValidator, videosShareValidator, - videoAbuseReportValidator, - videoRateValidator, getCommonVideoAttributes -- cgit v1.2.3