1 import * as express from 'express'
2 import validator from 'validator'
3 import { VideoCommentModel } from '@server/models/video/video-comment'
4 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
5 import { MVideoId } from '@server/types/models'
7 const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS
9 function isValidVideoCommentText (value: string) {
10 return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT)
13 async function doesVideoCommentThreadExist (idArg: number | string, video: MVideoId, res: express.Response) {
14 const id = parseInt(idArg + '', 10)
15 const videoComment = await VideoCommentModel.loadById(id)
19 .json({ error: 'Video comment thread not found' })
25 if (videoComment.videoId !== video.id) {
27 .json({ error: 'Video comment is not associated to this video.' })
33 if (videoComment.inReplyToCommentId !== null) {
35 .json({ error: 'Video comment is not a thread.' })
41 res.locals.videoCommentThread = videoComment
45 async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) {
46 const id = parseInt(idArg + '', 10)
47 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
51 .json({ error: 'Video comment thread not found' })
57 if (videoComment.videoId !== video.id) {
59 .json({ error: 'Video comment is not associated to this video.' })
65 res.locals.videoCommentFull = videoComment
69 async function doesCommentIdExist (idArg: number | string, res: express.Response) {
70 const id = parseInt(idArg + '', 10)
71 const videoComment = await VideoCommentModel.loadById(id)
75 .json({ error: 'Video comment thread not found' })
80 res.locals.videoComment = videoComment
85 // ---------------------------------------------------------------------------
88 isValidVideoCommentText,
89 doesVideoCommentThreadExist,
90 doesVideoCommentExist,