diff options
Diffstat (limited to 'server/middlewares/validators/shared/video-comments.ts')
-rw-r--r-- | server/middlewares/validators/shared/video-comments.ts | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/server/middlewares/validators/shared/video-comments.ts b/server/middlewares/validators/shared/video-comments.ts new file mode 100644 index 000000000..83ea15c98 --- /dev/null +++ b/server/middlewares/validators/shared/video-comments.ts | |||
@@ -0,0 +1,73 @@ | |||
1 | import * as express from 'express' | ||
2 | import { VideoCommentModel } from '@server/models/video/video-comment' | ||
3 | import { MVideoId } from '@server/types/models' | ||
4 | import { HttpStatusCode } from '@shared/core-utils' | ||
5 | |||
6 | async 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 | |||
32 | async 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 | |||
53 | async 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 | |||
69 | export { | ||
70 | doesVideoCommentThreadExist, | ||
71 | doesVideoCommentExist, | ||
72 | doesCommentIdExist | ||
73 | } | ||