]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-comments.ts
hls-plugin: destroy hls upon third err
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-comments.ts
CommitLineData
57f6896f 1import * as express from 'express'
7cde3b9c 2import validator from 'validator'
57f6896f 3import { VideoCommentModel } from '@server/models/video/video-comment'
74dc3bca 4import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
57f6896f 5import { MVideoId } from '@server/types/models'
bf1f6508
C
6
7const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS
8
9function isValidVideoCommentText (value: string) {
10 return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT)
11}
12
57f6896f
C
13async function doesVideoCommentThreadExist (idArg: number | string, video: MVideoId, res: express.Response) {
14 const id = parseInt(idArg + '', 10)
15 const videoComment = await VideoCommentModel.loadById(id)
16
17 if (!videoComment) {
18 res.status(404)
19 .json({ error: 'Video comment thread not found' })
20 .end()
21
22 return false
23 }
24
25 if (videoComment.videoId !== video.id) {
26 res.status(400)
27 .json({ error: 'Video comment is not associated to this video.' })
28 .end()
29
30 return false
31 }
32
33 if (videoComment.inReplyToCommentId !== null) {
34 res.status(400)
35 .json({ error: 'Video comment is not a thread.' })
36 .end()
37
38 return false
39 }
40
41 res.locals.videoCommentThread = videoComment
42 return true
43}
44
45async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) {
46 const id = parseInt(idArg + '', 10)
47 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
48
49 if (!videoComment) {
50 res.status(404)
51 .json({ error: 'Video comment thread not found' })
52 .end()
53
54 return false
55 }
56
57 if (videoComment.videoId !== video.id) {
58 res.status(400)
59 .json({ error: 'Video comment is not associated to this video.' })
60 .end()
61
62 return false
63 }
64
65 res.locals.videoCommentFull = videoComment
66 return true
67}
68
69async function doesCommentIdExist (idArg: number | string, res: express.Response) {
70 const id = parseInt(idArg + '', 10)
310b5219 71 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
57f6896f
C
72
73 if (!videoComment) {
74 res.status(404)
75 .json({ error: 'Video comment thread not found' })
76
77 return false
78 }
79
310b5219 80 res.locals.videoCommentFull = videoComment
57f6896f
C
81
82 return true
83}
84
bf1f6508
C
85// ---------------------------------------------------------------------------
86
87export {
57f6896f
C
88 isValidVideoCommentText,
89 doesVideoCommentThreadExist,
90 doesVideoCommentExist,
91 doesCommentIdExist
bf1f6508 92}