import { UserRight } from '../../../../shared'
import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
import { isValidVideoCommentText } from '../../../helpers/custom-validators/video-comments'
-import { isVideoExist } from '../../../helpers/custom-validators/videos'
+import { doesVideoExist } from '../../../helpers/custom-validators/videos'
import { logger } from '../../../helpers/logger'
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'),
logger.debug('Checking listVideoCommentThreads parameters.', { parameters: req.params })
if (areValidationErrors(req, res)) return
- if (!await isVideoExist(req.params.videoId, res, 'only-video')) return
+ if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
return next()
}
logger.debug('Checking listVideoThreadComments parameters.', { parameters: req.params })
if (areValidationErrors(req, res)) return
- if (!await isVideoExist(req.params.videoId, res, 'only-video')) return
- if (!await isVideoCommentThreadExist(req.params.threadId, res.locals.video, res)) return
+ if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
+ if (!await doesVideoCommentThreadExist(req.params.threadId, res.locals.video, res)) return
return next()
}
logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params, body: req.body })
if (areValidationErrors(req, res)) return
- if (!await isVideoExist(req.params.videoId, 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()
}
logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params, body: req.body })
if (areValidationErrors(req, res)) return
- if (!await isVideoExist(req.params.videoId, res)) return
+ if (!await doesVideoExist(req.params.videoId, res)) return
if (!isVideoCommentsEnabled(res.locals.video, res)) return
- if (!await isVideoCommentExist(req.params.commentId, 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()
}
logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
if (areValidationErrors(req, res)) return
- if (!await isVideoExist(req.params.videoId, res, 'id')) return
- if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
+ if (!await doesVideoExist(req.params.videoId, res, 'id')) return
+ if (!await doesVideoCommentExist(req.params.commentId, res.locals.video, res)) return
return next()
}
logger.debug('Checking removeVideoCommentValidator parameters.', { parameters: req.params })
if (areValidationErrors(req, res)) return
- if (!await isVideoExist(req.params.videoId, res)) return
- if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
+ if (!await doesVideoExist(req.params.videoId, res)) return
+ if (!await doesVideoCommentExist(req.params.commentId, res.locals.video, res)) return
// Check if the user who did the request is able to delete the video
if (!checkUserCanDeleteVideoComment(res.locals.oauth.token.User, res.locals.videoComment, res)) return
// ---------------------------------------------------------------------------
-async function isVideoCommentThreadExist (id: number, video: VideoModel, res: express.Response) {
+async function doesVideoCommentThreadExist (id: number, video: VideoModel, res: express.Response) {
const videoComment = await VideoCommentModel.loadById(id)
if (!videoComment) {
return true
}
-async function isVideoCommentExist (id: number, video: VideoModel, res: express.Response) {
+async function doesVideoCommentExist (id: number, video: VideoModel, res: express.Response) {
const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
if (!videoComment) {
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
+}