From 47564bbe2eeb2baae9b7e3f9b2b8d16522bc7e04 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 3 Jan 2018 10:12:36 +0100 Subject: Add ability to disable video comments --- server/middlewares/validators/users.ts | 5 ++--- server/middlewares/validators/video-comments.ts | 14 ++++++++++++++ server/middlewares/validators/videos.ts | 10 ++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 42ebddd56..7c77e9a39 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -3,11 +3,10 @@ import 'express-validator' import { body, param } from 'express-validator/check' import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { - isAvatarFile, - isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, + isAvatarFile, isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' -import { isVideoExist, isVideoFile } from '../../helpers/custom-validators/videos' +import { isVideoExist } from '../../helpers/custom-validators/videos' import { logger } from '../../helpers/logger' import { isSignupAllowed } from '../../helpers/utils' import { CONSTRAINTS_FIELDS } from '../../initializers' diff --git a/server/middlewares/validators/video-comments.ts b/server/middlewares/validators/video-comments.ts index fdd092571..ade0b7b9f 100644 --- a/server/middlewares/validators/video-comments.ts +++ b/server/middlewares/validators/video-comments.ts @@ -45,6 +45,7 @@ const addVideoCommentThreadValidator = [ if (areValidationErrors(req, res)) return if (!await isVideoExist(req.params.videoId, res)) return + if (!isVideoCommentsEnabled(res.locals.video, res)) return return next() } @@ -60,6 +61,7 @@ const addVideoCommentReplyValidator = [ if (areValidationErrors(req, res)) return if (!await isVideoExist(req.params.videoId, res)) return + if (!isVideoCommentsEnabled(res.locals.video, res)) return if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return return next() @@ -146,3 +148,15 @@ async function isVideoCommentExist (id: number, video: VideoModel, res: express. res.locals.videoComment = videoComment return true } + +function isVideoCommentsEnabled (video: VideoModel, res: express.Response) { + if (video.commentsEnabled !== true) { + res.status(409) + .json({ error: 'Video comments are disabled for this video.' }) + .end() + + return false + } + + return true +} diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index bffc50322..e8cb2ae03 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -2,10 +2,10 @@ import * as express from 'express' import 'express-validator' import { body, param, query } from 'express-validator/check' import { UserRight, VideoPrivacy } from '../../../shared' -import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' +import { isBooleanValid, isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' import { isVideoAbuseReasonValid, isVideoCategoryValid, isVideoDescriptionValid, isVideoExist, isVideoFile, isVideoLanguageValid, - isVideoLicenceValid, isVideoNameValid, isVideoNSFWValid, isVideoPrivacyValid, isVideoRatingTypeValid, isVideoTagsValid + isVideoLicenceValid, isVideoNameValid, isVideoPrivacyValid, isVideoRatingTypeValid, isVideoTagsValid } from '../../helpers/custom-validators/videos' import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils' import { logger } from '../../helpers/logger' @@ -26,11 +26,12 @@ const videosAddValidator = [ body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'), - body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'), + body('nsfw').custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'), body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'), body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'), + body('commentsEnabled').custom(isBooleanValid).withMessage('Should have comments enabled boolean'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) @@ -85,10 +86,11 @@ const videosUpdateValidator = [ body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'), - body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'), + body('nsfw').optional().custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'), body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'), + body('commentsEnabled').optional().custom(isBooleanValid).withMessage('Should have comments enabled boolean'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking videosUpdate parameters', { parameters: req.body }) -- cgit v1.2.3