]>
Commit | Line | Data |
---|---|---|
57f6896f | 1 | import * as express from 'express' |
7cde3b9c | 2 | import validator from 'validator' |
57f6896f | 3 | import { VideoCommentModel } from '@server/models/video/video-comment' |
74dc3bca | 4 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
57f6896f | 5 | import { MVideoId } from '@server/types/models' |
2d53be02 | 6 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
bf1f6508 C |
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 | ||
57f6896f C |
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) { | |
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 | ||
40 | async 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 | ||
61 | async 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 | ||
79 | export { | |
57f6896f C |
80 | isValidVideoCommentText, |
81 | doesVideoCommentThreadExist, | |
82 | doesVideoCommentExist, | |
83 | doesCommentIdExist | |
bf1f6508 | 84 | } |