1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
4 import { isValidVideoCommentText } from '../../helpers/custom-validators/video-comments'
5 import { isVideoExist } from '../../helpers/custom-validators/videos'
6 import { logger } from '../../helpers/logger'
7 import { VideoModel } from '../../models/video/video'
8 import { VideoCommentModel } from '../../models/video/video-comment'
9 import { areValidationErrors } from './utils'
11 const listVideoCommentThreadsValidator = [
12 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
14 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking listVideoCommentThreads parameters.', { parameters: req.params })
17 if (areValidationErrors(req, res)) return
18 if (!await isVideoExist(req.params.videoId, res)) return
24 const listVideoThreadCommentsValidator = [
25 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
26 param('threadId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'),
28 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
29 logger.debug('Checking listVideoThreadComments parameters.', { parameters: req.params })
31 if (areValidationErrors(req, res)) return
32 if (!await isVideoExist(req.params.videoId, res)) return
33 if (!await isVideoCommentThreadExist(req.params.threadId, res.locals.video, res)) return
39 const addVideoCommentThreadValidator = [
40 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
41 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
43 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params })
46 if (areValidationErrors(req, res)) return
47 if (!await isVideoExist(req.params.videoId, res)) return
48 if (!isVideoCommentsEnabled(res.locals.video, res)) return
54 const addVideoCommentReplyValidator = [
55 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
56 param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
57 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
59 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
60 logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params })
62 if (areValidationErrors(req, res)) return
63 if (!await isVideoExist(req.params.videoId, res)) return
64 if (!isVideoCommentsEnabled(res.locals.video, res)) return
65 if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
71 const videoCommentGetValidator = [
72 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
73 param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
75 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
76 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
78 if (areValidationErrors(req, res)) return
79 if (!await isVideoExist(req.params.videoId, res)) return
80 if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
86 // ---------------------------------------------------------------------------
89 listVideoCommentThreadsValidator,
90 listVideoThreadCommentsValidator,
91 addVideoCommentThreadValidator,
92 addVideoCommentReplyValidator,
93 videoCommentGetValidator
96 // ---------------------------------------------------------------------------
98 async function isVideoCommentThreadExist (id: number, video: VideoModel, res: express.Response) {
99 const videoComment = await VideoCommentModel.loadById(id)
103 .json({ error: 'Video comment thread not found' })
109 if (videoComment.videoId !== video.id) {
111 .json({ error: 'Video comment is associated to this video.' })
117 if (videoComment.inReplyToCommentId !== null) {
119 .json({ error: 'Video comment is not a thread.' })
125 res.locals.videoCommentThread = videoComment
129 async function isVideoCommentExist (id: number, video: VideoModel, res: express.Response) {
130 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
134 .json({ error: 'Video comment thread not found' })
140 if (videoComment.videoId !== video.id) {
142 .json({ error: 'Video comment is associated to this video.' })
148 res.locals.videoComment = videoComment
152 function isVideoCommentsEnabled (video: VideoModel, res: express.Response) {
153 if (video.commentsEnabled !== true) {
155 .json({ error: 'Video comments are disabled for this video.' })