]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/shared/video-comments.ts
Refactor video rights checker
[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'
c0e8b12e 4import { HttpStatusCode } 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) {
19 res.fail({ message: 'Video comment is not associated to this video.' })
20 return false
21 }
22
23 if (videoComment.inReplyToCommentId !== null) {
24 res.fail({ message: 'Video comment is not a thread.' })
25 return false
26 }
27
28 res.locals.videoCommentThread = videoComment
29 return true
30}
31
32async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) {
33 const id = parseInt(idArg + '', 10)
34 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
35
36 if (!videoComment) {
37 res.fail({
38 status: HttpStatusCode.NOT_FOUND_404,
39 message: 'Video comment thread not found'
40 })
41 return false
42 }
43
44 if (videoComment.videoId !== video.id) {
45 res.fail({ message: 'Video comment is not associated to this video.' })
46 return false
47 }
48
49 res.locals.videoCommentFull = videoComment
50 return true
51}
52
53async function doesCommentIdExist (idArg: number | string, res: express.Response) {
54 const id = parseInt(idArg + '', 10)
55 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
56
57 if (!videoComment) {
58 res.fail({
59 status: HttpStatusCode.NOT_FOUND_404,
60 message: 'Video comment thread not found'
61 })
62 return false
63 }
64
65 res.locals.videoCommentFull = videoComment
66 return true
67}
68
69export {
70 doesVideoCommentThreadExist,
71 doesVideoCommentExist,
72 doesCommentIdExist
73}