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)
20 status: HttpStatusCode.NOT_FOUND_404,
21 message: 'Video comment thread not found'
26 if (videoComment.videoId !== video.id) {
27 res.fail({ message: 'Video comment is not associated to this video.' })
31 if (videoComment.inReplyToCommentId !== null) {
32 res.fail({ message: 'Video comment is not a thread.' })
36 res.locals.videoCommentThread = videoComment
40 async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) {
41 const id = parseInt(idArg + '', 10)
42 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'Video comment thread not found'
52 if (videoComment.videoId !== video.id) {
53 res.fail({ message: 'Video comment is not associated to this video.' })
57 res.locals.videoCommentFull = videoComment
61 async function doesCommentIdExist (idArg: number | string, res: express.Response) {
62 const id = parseInt(idArg + '', 10)
63 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
67 status: HttpStatusCode.NOT_FOUND_404,
68 message: 'Video comment thread not found'
73 res.locals.videoCommentFull = videoComment
77 // ---------------------------------------------------------------------------
80 isValidVideoCommentText,
81 doesVideoCommentThreadExist,
82 doesVideoCommentExist,