]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import validator from 'validator' | |
3 | import { VideoCommentModel } from '@server/models/video/video-comment' | |
4 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | |
5 | import { MVideoId } from '@server/types/models' | |
6 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
7 | ||
8 | const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS | |
9 | ||
10 | function isValidVideoCommentText (value: string) { | |
11 | return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT) | |
12 | } | |
13 | ||
14 | async 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) { | |
19 | res.status(HttpStatusCode.NOT_FOUND_404) | |
20 | .json({ error: 'Video comment thread not found' }) | |
21 | .end() | |
22 | ||
23 | return false | |
24 | } | |
25 | ||
26 | if (videoComment.videoId !== video.id) { | |
27 | res.status(HttpStatusCode.BAD_REQUEST_400) | |
28 | .json({ error: 'Video comment is not associated to this video.' }) | |
29 | .end() | |
30 | ||
31 | return false | |
32 | } | |
33 | ||
34 | if (videoComment.inReplyToCommentId !== null) { | |
35 | res.status(HttpStatusCode.BAD_REQUEST_400) | |
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 | ||
46 | async 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) { | |
51 | res.status(HttpStatusCode.NOT_FOUND_404) | |
52 | .json({ error: 'Video comment thread not found' }) | |
53 | .end() | |
54 | ||
55 | return false | |
56 | } | |
57 | ||
58 | if (videoComment.videoId !== video.id) { | |
59 | res.status(HttpStatusCode.BAD_REQUEST_400) | |
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 | ||
70 | async function doesCommentIdExist (idArg: number | string, res: express.Response) { | |
71 | const id = parseInt(idArg + '', 10) | |
72 | const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) | |
73 | ||
74 | if (!videoComment) { | |
75 | res.status(HttpStatusCode.NOT_FOUND_404) | |
76 | .json({ error: 'Video comment thread not found' }) | |
77 | ||
78 | return false | |
79 | } | |
80 | ||
81 | res.locals.videoCommentFull = videoComment | |
82 | ||
83 | return true | |
84 | } | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | ||
88 | export { | |
89 | isValidVideoCommentText, | |
90 | doesVideoCommentThreadExist, | |
91 | doesVideoCommentExist, | |
92 | doesCommentIdExist | |
93 | } |