]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-comments.ts
replace numbers with typed http status codes (#3409)
[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'
2d53be02 6import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
bf1f6508
C
7
8const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS
9
10function isValidVideoCommentText (value: string) {
11 return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT)
12}
13
57f6896f
C
14async 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) {
2d53be02 19 res.status(HttpStatusCode.NOT_FOUND_404)
57f6896f
C
20 .json({ error: 'Video comment thread not found' })
21 .end()
22
23 return false
24 }
25
26 if (videoComment.videoId !== video.id) {
2d53be02 27 res.status(HttpStatusCode.BAD_REQUEST_400)
57f6896f
C
28 .json({ error: 'Video comment is not associated to this video.' })
29 .end()
30
31 return false
32 }
33
34 if (videoComment.inReplyToCommentId !== null) {
2d53be02 35 res.status(HttpStatusCode.BAD_REQUEST_400)
57f6896f
C
36 .json({ error: 'Video comment is not a thread.' })
37 .end()
38
39 return false
40 }
41
42 res.locals.videoCommentThread = videoComment
43 return true
44}
45
46async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) {
47 const id = parseInt(idArg + '', 10)
48 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
49
50 if (!videoComment) {
2d53be02 51 res.status(HttpStatusCode.NOT_FOUND_404)
57f6896f
C
52 .json({ error: 'Video comment thread not found' })
53 .end()
54
55 return false
56 }
57
58 if (videoComment.videoId !== video.id) {
2d53be02 59 res.status(HttpStatusCode.BAD_REQUEST_400)
57f6896f
C
60 .json({ error: 'Video comment is not associated to this video.' })
61 .end()
62
63 return false
64 }
65
66 res.locals.videoCommentFull = videoComment
67 return true
68}
69
70async function doesCommentIdExist (idArg: number | string, res: express.Response) {
71 const id = parseInt(idArg + '', 10)
310b5219 72 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
57f6896f
C
73
74 if (!videoComment) {
2d53be02 75 res.status(HttpStatusCode.NOT_FOUND_404)
57f6896f
C
76 .json({ error: 'Video comment thread not found' })
77
78 return false
79 }
80
310b5219 81 res.locals.videoCommentFull = videoComment
57f6896f
C
82
83 return true
84}
85
bf1f6508
C
86// ---------------------------------------------------------------------------
87
88export {
57f6896f
C
89 isValidVideoCommentText,
90 doesVideoCommentThreadExist,
91 doesVideoCommentExist,
92 doesCommentIdExist
bf1f6508 93}