aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators
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
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')
-rw-r--r--server/helpers/custom-validators/video-comments.ts72
-rw-r--r--server/helpers/custom-validators/video-imports.ts19
-rw-r--r--server/helpers/custom-validators/video-ownership.ts25
3 files changed, 7 insertions, 109 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}
diff --git a/server/helpers/custom-validators/video-imports.ts b/server/helpers/custom-validators/video-imports.ts
index 3ad7a4648..dbf6a3504 100644
--- a/server/helpers/custom-validators/video-imports.ts
+++ b/server/helpers/custom-validators/video-imports.ts
@@ -2,9 +2,6 @@ import 'multer'
2import validator from 'validator' 2import validator from 'validator'
3import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants' 3import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants'
4import { exists, isFileValid } from './misc' 4import { exists, isFileValid } from './misc'
5import * as express from 'express'
6import { VideoImportModel } from '../../models/video/video-import'
7import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
8 5
9function isVideoImportTargetUrlValid (url: string) { 6function isVideoImportTargetUrlValid (url: string) {
10 const isURLOptions = { 7 const isURLOptions = {
@@ -32,26 +29,10 @@ function isVideoImportTorrentFile (files: { [ fieldname: string ]: Express.Multe
32 return isFileValid(files, videoTorrentImportRegex, 'torrentfile', CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.FILE_SIZE.max, true) 29 return isFileValid(files, videoTorrentImportRegex, 'torrentfile', CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.FILE_SIZE.max, true)
33} 30}
34 31
35async function doesVideoImportExist (id: number, res: express.Response) {
36 const videoImport = await VideoImportModel.loadAndPopulateVideo(id)
37
38 if (!videoImport) {
39 res.fail({
40 status: HttpStatusCode.NOT_FOUND_404,
41 message: 'Video import not found'
42 })
43 return false
44 }
45
46 res.locals.videoImport = videoImport
47 return true
48}
49
50// --------------------------------------------------------------------------- 32// ---------------------------------------------------------------------------
51 33
52export { 34export {
53 isVideoImportStateValid, 35 isVideoImportStateValid,
54 isVideoImportTargetUrlValid, 36 isVideoImportTargetUrlValid,
55 doesVideoImportExist,
56 isVideoImportTorrentFile 37 isVideoImportTorrentFile
57} 38}
diff --git a/server/helpers/custom-validators/video-ownership.ts b/server/helpers/custom-validators/video-ownership.ts
index 21a6b7203..0e1c63bad 100644
--- a/server/helpers/custom-validators/video-ownership.ts
+++ b/server/helpers/custom-validators/video-ownership.ts
@@ -1,26 +1,9 @@
1import { Response } from 'express' 1import { Response } from 'express'
2import { VideoChangeOwnershipModel } from '../../models/video/video-change-ownership'
3import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership'
4import { MUserId } from '@server/types/models' 2import { MUserId } from '@server/types/models'
3import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership'
5import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' 4import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
6 5
7export async function doesChangeVideoOwnershipExist (idArg: number | string, res: Response) { 6function checkUserCanTerminateOwnershipChange (user: MUserId, videoChangeOwnership: MVideoChangeOwnershipFull, res: Response) {
8 const id = parseInt(idArg + '', 10)
9 const videoChangeOwnership = await VideoChangeOwnershipModel.load(id)
10
11 if (!videoChangeOwnership) {
12 res.fail({
13 status: HttpStatusCode.NOT_FOUND_404,
14 message: 'Video change ownership not found'
15 })
16 return false
17 }
18
19 res.locals.videoChangeOwnership = videoChangeOwnership
20 return true
21}
22
23export function checkUserCanTerminateOwnershipChange (user: MUserId, videoChangeOwnership: MVideoChangeOwnershipFull, res: Response) {
24 if (videoChangeOwnership.NextOwner.userId === user.id) { 7 if (videoChangeOwnership.NextOwner.userId === user.id) {
25 return true 8 return true
26 } 9 }
@@ -31,3 +14,7 @@ export function checkUserCanTerminateOwnershipChange (user: MUserId, videoChange
31 }) 14 })
32 return false 15 return false
33} 16}
17
18export {
19 checkUserCanTerminateOwnershipChange
20}