1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { logger } from '../../helpers'
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 { VideoCommentModel } from '../../models/video/video-comment'
8 import { areValidationErrors } from './utils'
10 const listVideoCommentThreadsValidator = [
11 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
13 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
16 if (areValidationErrors(req, res)) return
17 if (!await isVideoExist(req.params.videoId, res)) return
23 const listVideoThreadCommentsValidator = [
24 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
25 param('threadId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'),
27 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
30 if (areValidationErrors(req, res)) return
31 if (!await isVideoExist(req.params.videoId, res)) return
32 if (!await isVideoCommentThreadExist(req.params.threadId, req.params.videoId, res)) return
38 const addVideoCommentThreadValidator = [
39 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
40 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
42 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
45 if (areValidationErrors(req, res)) return
46 if (!await isVideoExist(req.params.videoId, res)) return
52 const addVideoCommentReplyValidator = [
53 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
54 param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
55 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
60 if (areValidationErrors(req, res)) return
61 if (!await isVideoExist(req.params.videoId, res)) return
62 if (!await isVideoCommentExist(req.params.commentId, req.params.videoId, res)) return
68 // ---------------------------------------------------------------------------
71 listVideoCommentThreadsValidator,
72 listVideoThreadCommentsValidator,
73 addVideoCommentThreadValidator,
74 addVideoCommentReplyValidator
77 // ---------------------------------------------------------------------------
79 async function isVideoCommentThreadExist (id: number, videoId: number, res: express.Response) {
80 const videoComment = await VideoCommentModel.loadById(id)
84 .json({ error: 'Video comment thread not found' })
90 if (videoComment.videoId !== videoId) {
92 .json({ error: 'Video comment is associated to this video.' })
98 if (videoComment.inReplyToCommentId !== null) {
100 .json({ error: 'Video comment is not a thread.' })
106 res.locals.videoCommentThread = videoComment
110 async function isVideoCommentExist (id: number, videoId: number, res: express.Response) {
111 const videoComment = await VideoCommentModel.loadById(id)
115 .json({ error: 'Video comment thread not found' })
121 if (videoComment.videoId !== videoId) {
123 .json({ error: 'Video comment is associated to this video.' })
129 res.locals.videoComment = videoComment