1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { MUserAccountUrl } from '@server/types/models'
4 import { HttpStatusCode, UserRight } from '@shared/models'
5 import { exists, isBooleanValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
6 import { isValidVideoCommentText } from '../../../helpers/custom-validators/video-comments'
7 import { logger } from '../../../helpers/logger'
8 import { AcceptResult, isLocalVideoCommentReplyAccepted, isLocalVideoThreadAccepted } from '../../../lib/moderation'
9 import { Hooks } from '../../../lib/plugins/hooks'
10 import { MCommentOwnerVideoReply, MVideo, MVideoFullLight } from '../../../types/models/video'
14 doesVideoCommentExist,
15 doesVideoCommentThreadExist,
20 const listVideoCommentsValidator = [
23 .customSanitizer(toBooleanOrNull)
24 .custom(isBooleanValid)
25 .withMessage('Should have a valid isLocal boolean'),
29 .customSanitizer(toBooleanOrNull)
30 .custom(isBooleanValid)
31 .withMessage('Should have a valid onLocalVideo boolean'),
37 query('searchAccount')
45 (req: express.Request, res: express.Response, next: express.NextFunction) => {
46 if (areValidationErrors(req, res)) return
52 const listVideoCommentThreadsValidator = [
53 isValidVideoIdParam('videoId'),
55 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
56 if (areValidationErrors(req, res)) return
57 if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
59 if (!await checkCanSeeVideo({ req, res, paramId: req.params.videoId, video: res.locals.onlyVideo })) return
65 const listVideoThreadCommentsValidator = [
66 isValidVideoIdParam('videoId'),
71 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72 if (areValidationErrors(req, res)) return
73 if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
74 if (!await doesVideoCommentThreadExist(req.params.threadId, res.locals.onlyVideo, res)) return
76 if (!await checkCanSeeVideo({ req, res, paramId: req.params.videoId, video: res.locals.onlyVideo })) return
82 const addVideoCommentThreadValidator = [
83 isValidVideoIdParam('videoId'),
86 .custom(isValidVideoCommentText),
88 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
89 if (areValidationErrors(req, res)) return
90 if (!await doesVideoExist(req.params.videoId, res)) return
92 if (!await checkCanSeeVideo({ req, res, paramId: req.params.videoId, video: res.locals.videoAll })) return
94 if (!isVideoCommentsEnabled(res.locals.videoAll, res)) return
95 if (!await isVideoCommentAccepted(req, res, res.locals.videoAll, false)) return
101 const addVideoCommentReplyValidator = [
102 isValidVideoIdParam('videoId'),
104 param('commentId').custom(isIdValid),
106 body('text').custom(isValidVideoCommentText),
108 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 if (areValidationErrors(req, res)) return
110 if (!await doesVideoExist(req.params.videoId, res)) return
112 if (!await checkCanSeeVideo({ req, res, paramId: req.params.videoId, video: res.locals.videoAll })) return
114 if (!isVideoCommentsEnabled(res.locals.videoAll, res)) return
115 if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoAll, res)) return
116 if (!await isVideoCommentAccepted(req, res, res.locals.videoAll, true)) return
122 const videoCommentGetValidator = [
123 isValidVideoIdParam('videoId'),
128 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
129 if (areValidationErrors(req, res)) return
130 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
131 if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoId, res)) return
137 const removeVideoCommentValidator = [
138 isValidVideoIdParam('videoId'),
143 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
144 if (areValidationErrors(req, res)) return
145 if (!await doesVideoExist(req.params.videoId, res)) return
146 if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoAll, res)) return
148 // Check if the user who did the request is able to delete the video
149 if (!checkUserCanDeleteVideoComment(res.locals.oauth.token.User, res.locals.videoCommentFull, res)) return
155 // ---------------------------------------------------------------------------
158 listVideoCommentThreadsValidator,
159 listVideoThreadCommentsValidator,
160 addVideoCommentThreadValidator,
161 listVideoCommentsValidator,
162 addVideoCommentReplyValidator,
163 videoCommentGetValidator,
164 removeVideoCommentValidator
167 // ---------------------------------------------------------------------------
169 function isVideoCommentsEnabled (video: MVideo, res: express.Response) {
170 if (video.commentsEnabled !== true) {
172 status: HttpStatusCode.CONFLICT_409,
173 message: 'Video comments are disabled for this video.'
181 function checkUserCanDeleteVideoComment (user: MUserAccountUrl, videoComment: MCommentOwnerVideoReply, res: express.Response) {
182 if (videoComment.isDeleted()) {
184 status: HttpStatusCode.CONFLICT_409,
185 message: 'This comment is already deleted'
190 const userAccount = user.Account
193 user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT) === false && // Not a moderator
194 videoComment.accountId !== userAccount.id && // Not the comment owner
195 videoComment.Video.VideoChannel.accountId !== userAccount.id // Not the video owner
198 status: HttpStatusCode.FORBIDDEN_403,
199 message: 'Cannot remove video comment of another user'
207 async function isVideoCommentAccepted (req: express.Request, res: express.Response, video: MVideoFullLight, isReply: boolean) {
208 const acceptParameters = {
210 commentBody: req.body,
211 user: res.locals.oauth.token.User,
215 let acceptedResult: AcceptResult
218 const acceptReplyParameters = Object.assign(acceptParameters, { parentComment: res.locals.videoCommentFull })
220 acceptedResult = await Hooks.wrapFun(
221 isLocalVideoCommentReplyAccepted,
222 acceptReplyParameters,
223 'filter:api.video-comment-reply.create.accept.result'
226 acceptedResult = await Hooks.wrapFun(
227 isLocalVideoThreadAccepted,
229 'filter:api.video-thread.create.accept.result'
233 if (!acceptedResult || acceptedResult.accepted !== true) {
234 logger.info('Refused local comment.', { acceptedResult, acceptParameters })
237 status: HttpStatusCode.FORBIDDEN_403,
238 message: acceptedResult?.errorMessage || 'Comment has been rejected.'