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'
6 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
8 const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS
10 function isValidVideoCommentText (value: string) {
11 return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT)
14 async function doesVideoCommentThreadExist (idArg: number | string, video: MVideoId, res: express.Response) {
15 const id = parseInt(idArg + '', 10)
16 const videoComment = await VideoCommentModel.loadById(id)
19 res.status(HttpStatusCode.NOT_FOUND_404)
20 .json({ error: 'Video comment thread not found' })
26 if (videoComment.videoId !== video.id) {
27 res.status(HttpStatusCode.BAD_REQUEST_400)
28 .json({ error: 'Video comment is not associated to this video.' })
34 if (videoComment.inReplyToCommentId !== null) {
35 res.status(HttpStatusCode.BAD_REQUEST_400)
36 .json({ error: 'Video comment is not a thread.' })
42 res.locals.videoCommentThread = videoComment
46 async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) {
47 const id = parseInt(idArg + '', 10)
48 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
51 res.status(HttpStatusCode.NOT_FOUND_404)
52 .json({ error: 'Video comment thread not found' })
58 if (videoComment.videoId !== video.id) {
59 res.status(HttpStatusCode.BAD_REQUEST_400)
60 .json({ error: 'Video comment is not associated to this video.' })
66 res.locals.videoCommentFull = videoComment
70 async function doesCommentIdExist (idArg: number | string, res: express.Response) {
71 const id = parseInt(idArg + '', 10)
72 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
75 res.status(HttpStatusCode.NOT_FOUND_404)
76 .json({ error: 'Video comment thread not found' })
81 res.locals.videoCommentFull = videoComment
86 // ---------------------------------------------------------------------------
89 isValidVideoCommentText,
90 doesVideoCommentThreadExist,
91 doesVideoCommentExist,