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