]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/video-comments.ts
5c88447ad1b795209d7a919feaa3316afcdcd63b
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-comments.ts
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'
7
8 const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS
9
10 function isValidVideoCommentText (value: string) {
11 return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT)
12 }
13
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)
17
18 if (!videoComment) {
19 res.fail({
20 status: HttpStatusCode.NOT_FOUND_404,
21 message: 'Video comment thread not found'
22 })
23 return false
24 }
25
26 if (videoComment.videoId !== video.id) {
27 res.fail({ message: 'Video comment is not associated to this video.' })
28 return false
29 }
30
31 if (videoComment.inReplyToCommentId !== null) {
32 res.fail({ message: 'Video comment is not a thread.' })
33 return false
34 }
35
36 res.locals.videoCommentThread = videoComment
37 return true
38 }
39
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)
43
44 if (!videoComment) {
45 res.fail({
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'Video comment thread not found'
48 })
49 return false
50 }
51
52 if (videoComment.videoId !== video.id) {
53 res.fail({ message: 'Video comment is not associated to this video.' })
54 return false
55 }
56
57 res.locals.videoCommentFull = videoComment
58 return true
59 }
60
61 async function doesCommentIdExist (idArg: number | string, res: express.Response) {
62 const id = parseInt(idArg + '', 10)
63 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
64
65 if (!videoComment) {
66 res.fail({
67 status: HttpStatusCode.NOT_FOUND_404,
68 message: 'Video comment thread not found'
69 })
70 return false
71 }
72
73 res.locals.videoCommentFull = videoComment
74 return true
75 }
76
77 // ---------------------------------------------------------------------------
78
79 export {
80 isValidVideoCommentText,
81 doesVideoCommentThreadExist,
82 doesVideoCommentExist,
83 doesCommentIdExist
84 }