]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/shared/video-comments.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / shared / video-comments.ts
CommitLineData
41fb13c3 1import express from 'express'
10363c74
C
2import { VideoCommentModel } from '@server/models/video/video-comment'
3import { MVideoId } from '@server/types/models'
5a9a56b7 4import { HttpStatusCode, ServerErrorCode } from '@shared/models'
10363c74
C
5
6async function doesVideoCommentThreadExist (idArg: number | string, video: MVideoId, res: express.Response) {
7 const id = parseInt(idArg + '', 10)
8 const videoComment = await VideoCommentModel.loadById(id)
9
10 if (!videoComment) {
11 res.fail({
12 status: HttpStatusCode.NOT_FOUND_404,
13 message: 'Video comment thread not found'
14 })
15 return false
16 }
17
18 if (videoComment.videoId !== video.id) {
5a9a56b7
C
19 res.fail({
20 type: ServerErrorCode.COMMENT_NOT_ASSOCIATED_TO_VIDEO,
21 message: 'Video comment is not associated to this video.'
22 })
10363c74
C
23 return false
24 }
25
26 if (videoComment.inReplyToCommentId !== null) {
27 res.fail({ message: 'Video comment is not a thread.' })
28 return false
29 }
30
31 res.locals.videoCommentThread = videoComment
32 return true
33}
34
35async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) {
36 const id = parseInt(idArg + '', 10)
37 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
38
39 if (!videoComment) {
40 res.fail({
41 status: HttpStatusCode.NOT_FOUND_404,
42 message: 'Video comment thread not found'
43 })
44 return false
45 }
46
47 if (videoComment.videoId !== video.id) {
5a9a56b7
C
48 res.fail({
49 type: ServerErrorCode.COMMENT_NOT_ASSOCIATED_TO_VIDEO,
50 message: 'Video comment is not associated to this video.'
51 })
10363c74
C
52 return false
53 }
54
55 res.locals.videoCommentFull = videoComment
56 return true
57}
58
59async function doesCommentIdExist (idArg: number | string, res: express.Response) {
60 const id = parseInt(idArg + '', 10)
61 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
62
63 if (!videoComment) {
64 res.fail({
65 status: HttpStatusCode.NOT_FOUND_404,
66 message: 'Video comment thread not found'
67 })
68 return false
69 }
70
71 res.locals.videoCommentFull = videoComment
72 return true
73}
74
75export {
76 doesVideoCommentThreadExist,
77 doesVideoCommentExist,
78 doesCommentIdExist
79}