aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/video-comments.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-03 17:33:44 +0200
committerChocobozzz <me@florianbigard.com>2021-06-03 18:03:36 +0200
commit10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4 (patch)
tree008f8dad8032684f46105a261b27b2d6f05b36eb /server/helpers/custom-validators/video-comments.ts
parent5e08989ede1a340b9edb94465a11b1e04bf24094 (diff)
downloadPeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.gz
PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.zst
PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.zip
Move middleware utils in middlewares
helpers modules should not import models
Diffstat (limited to 'server/helpers/custom-validators/video-comments.ts')
-rw-r--r--server/helpers/custom-validators/video-comments.ts72
1 files changed, 1 insertions, 71 deletions
diff --git a/server/helpers/custom-validators/video-comments.ts b/server/helpers/custom-validators/video-comments.ts
index 5c88447ad..94bdf237a 100644
--- a/server/helpers/custom-validators/video-comments.ts
+++ b/server/helpers/custom-validators/video-comments.ts
@@ -1,9 +1,5 @@
1import * as express from 'express'
2import validator from 'validator' 1import validator from 'validator'
3import { VideoCommentModel } from '@server/models/video/video-comment'
4import { CONSTRAINTS_FIELDS } from '../../initializers/constants' 2import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
5import { MVideoId } from '@server/types/models'
6import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
7 3
8const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS 4const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS
9 5
@@ -11,74 +7,8 @@ function isValidVideoCommentText (value: string) {
11 return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT) 7 return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT)
12} 8}
13 9
14async 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.fail({
20 status: HttpStatusCode.NOT_FOUND_404,
21 message: 'Video comment thread not found'
22 })
23 return false
24 }
25
26 if (videoComment.videoId !== video.id) {
27 res.fail({ message: 'Video comment is not associated to this video.' })
28 return false
29 }
30
31 if (videoComment.inReplyToCommentId !== null) {
32 res.fail({ message: 'Video comment is not a thread.' })
33 return false
34 }
35
36 res.locals.videoCommentThread = videoComment
37 return true
38}
39
40async 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) {
45 res.fail({
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'Video comment thread not found'
48 })
49 return false
50 }
51
52 if (videoComment.videoId !== video.id) {
53 res.fail({ message: 'Video comment is not associated to this video.' })
54 return false
55 }
56
57 res.locals.videoCommentFull = videoComment
58 return true
59}
60
61async function doesCommentIdExist (idArg: number | string, res: express.Response) {
62 const id = parseInt(idArg + '', 10)
63 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
64
65 if (!videoComment) {
66 res.fail({
67 status: HttpStatusCode.NOT_FOUND_404,
68 message: 'Video comment thread not found'
69 })
70 return false
71 }
72
73 res.locals.videoCommentFull = videoComment
74 return true
75}
76
77// --------------------------------------------------------------------------- 10// ---------------------------------------------------------------------------
78 11
79export { 12export {
80 isValidVideoCommentText, 13 isValidVideoCommentText
81 doesVideoCommentThreadExist,
82 doesVideoCommentExist,
83 doesCommentIdExist
84} 14}