1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { UserRight } from '../../../shared'
4 import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
5 import { isValidVideoCommentText } from '../../helpers/custom-validators/video-comments'
6 import { isVideoExist } from '../../helpers/custom-validators/videos'
7 import { logger } from '../../helpers/logger'
8 import { UserModel } from '../../models/account/user'
9 import { VideoModel } from '../../models/video/video'
10 import { VideoCommentModel } from '../../models/video/video-comment'
11 import { areValidationErrors } from './utils'
13 const listVideoCommentThreadsValidator = [
14 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
16 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
17 logger.debug('Checking listVideoCommentThreads parameters.', { parameters: req.params })
19 if (areValidationErrors(req, res)) return
20 if (!await isVideoExist(req.params.videoId, res)) return
26 const listVideoThreadCommentsValidator = [
27 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
28 param('threadId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'),
30 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
31 logger.debug('Checking listVideoThreadComments parameters.', { parameters: req.params })
33 if (areValidationErrors(req, res)) return
34 if (!await isVideoExist(req.params.videoId, res)) return
35 if (!await isVideoCommentThreadExist(req.params.threadId, res.locals.video, res)) return
41 const addVideoCommentThreadValidator = [
42 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
43 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
45 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
46 logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params, body: req.body })
48 if (areValidationErrors(req, res)) return
49 if (!await isVideoExist(req.params.videoId, res)) return
50 if (!isVideoCommentsEnabled(res.locals.video, res)) return
56 const addVideoCommentReplyValidator = [
57 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
58 param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
59 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
61 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
62 logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params, body: req.body })
64 if (areValidationErrors(req, res)) return
65 if (!await isVideoExist(req.params.videoId, res)) return
66 if (!isVideoCommentsEnabled(res.locals.video, res)) return
67 if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
73 const videoCommentGetValidator = [
74 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
75 param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
77 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
78 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
80 if (areValidationErrors(req, res)) return
81 if (!await isVideoExist(req.params.videoId, res)) return
82 if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
88 const removeVideoCommentValidator = [
89 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
90 param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
92 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
93 logger.debug('Checking removeVideoCommentValidator parameters.', { parameters: req.params })
95 if (areValidationErrors(req, res)) return
96 if (!await isVideoExist(req.params.videoId, res)) return
97 if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
99 // Check if the user who did the request is able to delete the video
100 if (!checkUserCanDeleteVideoComment(res.locals.oauth.token.User, res.locals.videoComment, res)) return
106 // ---------------------------------------------------------------------------
109 listVideoCommentThreadsValidator,
110 listVideoThreadCommentsValidator,
111 addVideoCommentThreadValidator,
112 addVideoCommentReplyValidator,
113 videoCommentGetValidator,
114 removeVideoCommentValidator
117 // ---------------------------------------------------------------------------
119 async function isVideoCommentThreadExist (id: number, video: VideoModel, res: express.Response) {
120 const videoComment = await VideoCommentModel.loadById(id)
124 .json({ error: 'Video comment thread not found' })
130 if (videoComment.videoId !== video.id) {
132 .json({ error: 'Video comment is associated to this video.' })
138 if (videoComment.inReplyToCommentId !== null) {
140 .json({ error: 'Video comment is not a thread.' })
146 res.locals.videoCommentThread = videoComment
150 async function isVideoCommentExist (id: number, video: VideoModel, res: express.Response) {
151 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
155 .json({ error: 'Video comment thread not found' })
161 if (videoComment.videoId !== video.id) {
163 .json({ error: 'Video comment is associated to this video.' })
169 res.locals.videoComment = videoComment
173 function isVideoCommentsEnabled (video: VideoModel, res: express.Response) {
174 if (video.commentsEnabled !== true) {
176 .json({ error: 'Video comments are disabled for this video.' })
185 function checkUserCanDeleteVideoComment (user: UserModel, videoComment: VideoCommentModel, res: express.Response) {
186 const account = videoComment.Account
187 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT) === false && account.userId !== user.id) {
189 .json({ error: 'Cannot remove video comment of another user' })