X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fvideos%2Fvideo-comments.ts;h=9c1bfaeaaa307b68266ce073f4373943c587ea24;hb=b4055e1c23eeefb0c8a85a77f312b2827d98f483;hp=ffde208b71209aa03b1e4c03e72a82330f64570e;hpb=66e001c848c009412c65cbce41be344d8985fd83;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/videos/video-comments.ts b/server/middlewares/validators/videos/video-comments.ts index ffde208b7..9c1bfaeaa 100644 --- a/server/middlewares/validators/videos/video-comments.ts +++ b/server/middlewares/validators/videos/video-comments.ts @@ -9,6 +9,8 @@ import { UserModel } from '../../../models/account/user' import { VideoModel } from '../../../models/video/video' import { VideoCommentModel } from '../../../models/video/video-comment' import { areValidationErrors } from '../utils' +import { Hooks } from '../../../lib/plugins/hooks' +import { isLocalVideoThreadAccepted, isLocalVideoCommentReplyAccepted, AcceptResult } from '../../../lib/moderation' const listVideoCommentThreadsValidator = [ param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), @@ -48,6 +50,7 @@ const addVideoCommentThreadValidator = [ if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.params.videoId, res)) return if (!isVideoCommentsEnabled(res.locals.video, res)) return + if (!await isVideoCommentAccepted(req, res, false)) return return next() } @@ -65,6 +68,7 @@ const addVideoCommentReplyValidator = [ if (!await doesVideoExist(req.params.videoId, res)) return if (!isVideoCommentsEnabled(res.locals.video, res)) return if (!await doesVideoCommentExist(req.params.commentId, res.locals.video, res)) return + if (!await isVideoCommentAccepted(req, res, true)) return return next() } @@ -193,3 +197,37 @@ function checkUserCanDeleteVideoComment (user: UserModel, videoComment: VideoCom return true } + +async function isVideoCommentAccepted (req: express.Request, res: express.Response, isReply: boolean) { + const acceptParameters = { + video: res.locals.video, + commentBody: req.body, + user: res.locals.oauth.token.User + } + + let acceptedResult: AcceptResult + + if (isReply) { + const acceptReplyParameters = Object.assign(acceptParameters, { parentComment: res.locals.videoComment }) + + acceptedResult = await Hooks.wrapObject( + isLocalVideoCommentReplyAccepted(acceptReplyParameters), + 'filter:api.video-comment-reply.create.accept.result' + ) + } else { + acceptedResult = await Hooks.wrapObject( + isLocalVideoThreadAccepted(acceptParameters), + 'filter:api.video-thread.create.accept.result' + ) + } + + if (!acceptedResult || acceptedResult.accepted !== true) { + logger.info('Refused local comment.', { acceptedResult, acceptParameters }) + res.status(403) + .json({ error: acceptedResult.errorMessage || 'Refused local comment' }) + + return false + } + + return true +}