aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-27 17:30:46 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:43:01 +0100
commita2431b7dcbc72c05101dcdbe631ff84a823aeb51 (patch)
tree09278a822905622a70ff976a75e09d99bc45639a /server/helpers
parentfcaf1e0aa84213a1b1f1b1a44a3276eae35ebe70 (diff)
downloadPeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.gz
PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.zst
PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.zip
Refractor validators
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/custom-validators/accounts.ts46
-rw-r--r--server/helpers/custom-validators/activitypub/account.ts63
-rw-r--r--server/helpers/custom-validators/activitypub/videos.ts13
-rw-r--r--server/helpers/custom-validators/video-channels.ts38
-rw-r--r--server/helpers/custom-validators/videos.ts39
5 files changed, 41 insertions, 158 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts
index a6d7f2b82..e3c477414 100644
--- a/server/helpers/custom-validators/accounts.ts
+++ b/server/helpers/custom-validators/accounts.ts
@@ -1,17 +1,16 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import * as express from 'express' 2import { Response } from 'express'
3import 'express-validator' 3import 'express-validator'
4import * as validator from 'validator' 4import * as validator from 'validator'
5import { database as db } from '../../initializers' 5import { database as db } from '../../initializers'
6import { AccountInstance } from '../../models' 6import { AccountInstance } from '../../models'
7import { logger } from '../logger'
8import { isUserUsernameValid } from './users' 7import { isUserUsernameValid } from './users'
9 8
10function isAccountNameValid (value: string) { 9function isAccountNameValid (value: string) {
11 return isUserUsernameValid(value) 10 return isUserUsernameValid(value)
12} 11}
13 12
14function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { 13function isAccountIdExist (id: number | string, res: Response) {
15 let promise: Bluebird<AccountInstance> 14 let promise: Bluebird<AccountInstance>
16 15
17 if (validator.isInt('' + id)) { 16 if (validator.isInt('' + id)) {
@@ -20,36 +19,35 @@ function checkAccountIdExists (id: number | string, res: express.Response, callb
20 promise = db.Account.loadByUUID('' + id) 19 promise = db.Account.loadByUUID('' + id)
21 } 20 }
22 21
23 return checkAccountExists(promise, res, callback) 22 return isAccountExist(promise, res)
24} 23}
25 24
26function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { 25function isLocalAccountNameExist (name: string, res: Response) {
27 const p = db.Account.loadLocalByName(name) 26 const promise = db.Account.loadLocalByName(name)
28 27
29 return checkAccountExists(p, res, callback) 28 return isAccountExist(promise, res)
30} 29}
31 30
32function checkAccountExists (p: Bluebird<AccountInstance>, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { 31async function isAccountExist (p: Bluebird<AccountInstance>, res: Response) {
33 p.then(account => { 32 const account = await p
34 if (!account) { 33
35 return res.status(404) 34 if (!account) {
36 .send({ error: 'Account not found' }) 35 res.status(404)
37 .end() 36 .send({ error: 'Account not found' })
38 } 37 .end()
39 38
40 res.locals.account = account 39 return false
41 return callback(null, account) 40 }
42 }) 41
43 .catch(err => { 42 res.locals.account = account
44 logger.error('Error in account request validator.', err) 43
45 return res.sendStatus(500) 44 return true
46 })
47} 45}
48 46
49// --------------------------------------------------------------------------- 47// ---------------------------------------------------------------------------
50 48
51export { 49export {
52 checkAccountIdExists, 50 isAccountIdExist,
53 checkLocalAccountNameExists, 51 isLocalAccountNameExist,
54 isAccountNameValid 52 isAccountNameValid
55} 53}
diff --git a/server/helpers/custom-validators/activitypub/account.ts b/server/helpers/custom-validators/activitypub/account.ts
index 645f55a5a..cab39a654 100644
--- a/server/helpers/custom-validators/activitypub/account.ts
+++ b/server/helpers/custom-validators/activitypub/account.ts
@@ -5,31 +5,19 @@ import { exists, isUUIDValid } from '../misc'
5import { isActivityPubUrlValid, isBaseActivityValid } from './misc' 5import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
6 6
7function isAccountEndpointsObjectValid (endpointObject: any) { 7function isAccountEndpointsObjectValid (endpointObject: any) {
8 return isAccountSharedInboxValid(endpointObject.sharedInbox) 8 return isActivityPubUrlValid(endpointObject.sharedInbox)
9}
10
11function isAccountSharedInboxValid (sharedInbox: string) {
12 return isActivityPubUrlValid(sharedInbox)
13} 9}
14 10
15function isAccountPublicKeyObjectValid (publicKeyObject: any) { 11function isAccountPublicKeyObjectValid (publicKeyObject: any) {
16 return isAccountPublicKeyIdValid(publicKeyObject.id) && 12 return isActivityPubUrlValid(publicKeyObject.id) &&
17 isAccountPublicKeyOwnerValid(publicKeyObject.owner) && 13 isActivityPubUrlValid(publicKeyObject.owner) &&
18 isAccountPublicKeyValid(publicKeyObject.publicKeyPem) 14 isAccountPublicKeyValid(publicKeyObject.publicKeyPem)
19} 15}
20 16
21function isAccountPublicKeyIdValid (id: string) {
22 return isActivityPubUrlValid(id)
23}
24
25function isAccountTypeValid (type: string) { 17function isAccountTypeValid (type: string) {
26 return type === 'Person' || type === 'Application' 18 return type === 'Person' || type === 'Application'
27} 19}
28 20
29function isAccountPublicKeyOwnerValid (owner: string) {
30 return isActivityPubUrlValid(owner)
31}
32
33function isAccountPublicKeyValid (publicKey: string) { 21function isAccountPublicKeyValid (publicKey: string) {
34 return exists(publicKey) && 22 return exists(publicKey) &&
35 typeof publicKey === 'string' && 23 typeof publicKey === 'string' &&
@@ -38,34 +26,10 @@ function isAccountPublicKeyValid (publicKey: string) {
38 validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY) 26 validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY)
39} 27}
40 28
41function isAccountIdValid (id: string) {
42 return isActivityPubUrlValid(id)
43}
44
45function isAccountFollowingValid (id: string) {
46 return isActivityPubUrlValid(id)
47}
48
49function isAccountFollowersValid (id: string) {
50 return isActivityPubUrlValid(id)
51}
52
53function isAccountInboxValid (inbox: string) {
54 return isActivityPubUrlValid(inbox)
55}
56
57function isAccountOutboxValid (outbox: string) {
58 return isActivityPubUrlValid(outbox)
59}
60
61function isAccountPreferredUsernameValid (preferredUsername: string) { 29function isAccountPreferredUsernameValid (preferredUsername: string) {
62 return isAccountNameValid(preferredUsername) 30 return isAccountNameValid(preferredUsername)
63} 31}
64 32
65function isAccountUrlValid (url: string) {
66 return isActivityPubUrlValid(url)
67}
68
69function isAccountPrivateKeyValid (privateKey: string) { 33function isAccountPrivateKeyValid (privateKey: string) {
70 return exists(privateKey) && 34 return exists(privateKey) &&
71 typeof privateKey === 'string' && 35 typeof privateKey === 'string' &&
@@ -75,15 +39,15 @@ function isAccountPrivateKeyValid (privateKey: string) {
75} 39}
76 40
77function isRemoteAccountValid (remoteAccount: any) { 41function isRemoteAccountValid (remoteAccount: any) {
78 return isAccountIdValid(remoteAccount.id) && 42 return isActivityPubUrlValid(remoteAccount.id) &&
79 isUUIDValid(remoteAccount.uuid) && 43 isUUIDValid(remoteAccount.uuid) &&
80 isAccountTypeValid(remoteAccount.type) && 44 isAccountTypeValid(remoteAccount.type) &&
81 isAccountFollowingValid(remoteAccount.following) && 45 isActivityPubUrlValid(remoteAccount.following) &&
82 isAccountFollowersValid(remoteAccount.followers) && 46 isActivityPubUrlValid(remoteAccount.followers) &&
83 isAccountInboxValid(remoteAccount.inbox) && 47 isActivityPubUrlValid(remoteAccount.inbox) &&
84 isAccountOutboxValid(remoteAccount.outbox) && 48 isActivityPubUrlValid(remoteAccount.outbox) &&
85 isAccountPreferredUsernameValid(remoteAccount.preferredUsername) && 49 isAccountPreferredUsernameValid(remoteAccount.preferredUsername) &&
86 isAccountUrlValid(remoteAccount.url) && 50 isActivityPubUrlValid(remoteAccount.url) &&
87 isAccountPublicKeyObjectValid(remoteAccount.publicKey) && 51 isAccountPublicKeyObjectValid(remoteAccount.publicKey) &&
88 isAccountEndpointsObjectValid(remoteAccount.endpoints) 52 isAccountEndpointsObjectValid(remoteAccount.endpoints)
89} 53}
@@ -113,19 +77,10 @@ function isAccountAcceptActivityValid (activity: any) {
113 77
114export { 78export {
115 isAccountEndpointsObjectValid, 79 isAccountEndpointsObjectValid,
116 isAccountSharedInboxValid,
117 isAccountPublicKeyObjectValid, 80 isAccountPublicKeyObjectValid,
118 isAccountPublicKeyIdValid,
119 isAccountTypeValid, 81 isAccountTypeValid,
120 isAccountPublicKeyOwnerValid,
121 isAccountPublicKeyValid, 82 isAccountPublicKeyValid,
122 isAccountIdValid,
123 isAccountFollowingValid,
124 isAccountFollowersValid,
125 isAccountInboxValid,
126 isAccountOutboxValid,
127 isAccountPreferredUsernameValid, 83 isAccountPreferredUsernameValid,
128 isAccountUrlValid,
129 isAccountPrivateKeyValid, 84 isAccountPrivateKeyValid,
130 isRemoteAccountValid, 85 isRemoteAccountValid,
131 isAccountFollowingCountValid, 86 isAccountFollowingCountValid,
diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts
index 55e79c4e8..12c672fd2 100644
--- a/server/helpers/custom-validators/activitypub/videos.ts
+++ b/server/helpers/custom-validators/activitypub/videos.ts
@@ -8,7 +8,6 @@ import {
8 isVideoNSFWValid, 8 isVideoNSFWValid,
9 isVideoTagValid, 9 isVideoTagValid,
10 isVideoTruncatedDescriptionValid, 10 isVideoTruncatedDescriptionValid,
11 isVideoUrlValid,
12 isVideoViewsValid 11 isVideoViewsValid
13} from '../videos' 12} from '../videos'
14import { isActivityPubUrlValid, isBaseActivityValid } from './misc' 13import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
@@ -77,12 +76,11 @@ export {
77function setValidRemoteTags (video: any) { 76function setValidRemoteTags (video: any) {
78 if (Array.isArray(video.tag) === false) return false 77 if (Array.isArray(video.tag) === false) return false
79 78
80 const newTag = video.tag.filter(t => { 79 video.tag = video.tag.filter(t => {
81 return t.type === 'Hashtag' && 80 return t.type === 'Hashtag' &&
82 isVideoTagValid(t.name) 81 isVideoTagValid(t.name)
83 }) 82 })
84 83
85 video.tag = newTag
86 return true 84 return true
87} 85}
88 86
@@ -96,7 +94,7 @@ function isRemoteVideoContentValid (mediaType: string, content: string) {
96 94
97function isRemoteVideoIconValid (icon: any) { 95function isRemoteVideoIconValid (icon: any) {
98 return icon.type === 'Image' && 96 return icon.type === 'Image' &&
99 isVideoUrlValid(icon.url) && 97 isActivityPubUrlValid(icon.url) &&
100 icon.mediaType === 'image/jpeg' && 98 icon.mediaType === 'image/jpeg' &&
101 validator.isInt(icon.width + '', { min: 0 }) && 99 validator.isInt(icon.width + '', { min: 0 }) &&
102 validator.isInt(icon.height + '', { min: 0 }) 100 validator.isInt(icon.height + '', { min: 0 })
@@ -105,8 +103,7 @@ function isRemoteVideoIconValid (icon: any) {
105function setValidRemoteVideoUrls (video: any) { 103function setValidRemoteVideoUrls (video: any) {
106 if (Array.isArray(video.url) === false) return false 104 if (Array.isArray(video.url) === false) return false
107 105
108 const newUrl = video.url.filter(u => isRemoteVideoUrlValid(u)) 106 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
109 video.url = newUrl
110 107
111 return true 108 return true
112} 109}
@@ -115,13 +112,13 @@ function isRemoteVideoUrlValid (url: any) {
115 return url.type === 'Link' && 112 return url.type === 'Link' &&
116 ( 113 (
117 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 && 114 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 &&
118 isVideoUrlValid(url.url) && 115 isActivityPubUrlValid(url.url) &&
119 validator.isInt(url.width + '', { min: 0 }) && 116 validator.isInt(url.width + '', { min: 0 }) &&
120 validator.isInt(url.size + '', { min: 0 }) 117 validator.isInt(url.size + '', { min: 0 })
121 ) || 118 ) ||
122 ( 119 (
123 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 && 120 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 &&
124 isVideoUrlValid(url.url) && 121 isActivityPubUrlValid(url.url) &&
125 validator.isInt(url.width + '', { min: 0 }) 122 validator.isInt(url.width + '', { min: 0 })
126 ) || 123 ) ||
127 ( 124 (
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts
index 267d987fc..3de9f041b 100644
--- a/server/helpers/custom-validators/video-channels.ts
+++ b/server/helpers/custom-validators/video-channels.ts
@@ -1,21 +1,13 @@
1import * as Bluebird from 'bluebird'
2import * as express from 'express' 1import * as express from 'express'
3import 'express-validator' 2import 'express-validator'
4import 'multer' 3import 'multer'
5import * as validator from 'validator' 4import * as validator from 'validator'
6
7import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' 5import { CONSTRAINTS_FIELDS, database as db } from '../../initializers'
8import { VideoChannelInstance } from '../../models' 6import { VideoChannelInstance } from '../../models'
9import { logger } from '../logger'
10import { isActivityPubUrlValid } from './index'
11import { exists } from './misc' 7import { exists } from './misc'
12 8
13const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS 9const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
14 10
15function isVideoChannelUrlValid (value: string) {
16 return isActivityPubUrlValid(value)
17}
18
19function isVideoChannelDescriptionValid (value: string) { 11function isVideoChannelDescriptionValid (value: string) {
20 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) 12 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
21} 13}
@@ -24,31 +16,7 @@ function isVideoChannelNameValid (value: string) {
24 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) 16 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
25} 17}
26 18
27function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { 19async function isVideoChannelExist (id: string, res: express.Response) {
28 let promise: Bluebird<VideoChannelInstance>
29 if (validator.isInt(id)) {
30 promise = db.VideoChannel.loadAndPopulateAccount(+id)
31 } else { // UUID
32 promise = db.VideoChannel.loadByUUIDAndPopulateAccount(id)
33 }
34
35 promise.then(videoChannel => {
36 if (!videoChannel) {
37 return res.status(404)
38 .json({ error: 'Video channel not found' })
39 .end()
40 }
41
42 res.locals.videoChannel = videoChannel
43 callback()
44 })
45 .catch(err => {
46 logger.error('Error in video channel request validator.', err)
47 return res.sendStatus(500)
48 })
49}
50
51async function isVideoChannelExistsPromise (id: string, res: express.Response) {
52 let videoChannel: VideoChannelInstance 20 let videoChannel: VideoChannelInstance
53 if (validator.isInt(id)) { 21 if (validator.isInt(id)) {
54 videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) 22 videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id)
@@ -72,8 +40,6 @@ async function isVideoChannelExistsPromise (id: string, res: express.Response) {
72 40
73export { 41export {
74 isVideoChannelDescriptionValid, 42 isVideoChannelDescriptionValid,
75 checkVideoChannelExists,
76 isVideoChannelNameValid, 43 isVideoChannelNameValid,
77 isVideoChannelExistsPromise, 44 isVideoChannelExist
78 isVideoChannelUrlValid
79} 45}
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 276354626..f13178c54 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -1,4 +1,3 @@
1import * as Bluebird from 'bluebird'
2import { Response } from 'express' 1import { Response } from 'express'
3import 'express-validator' 2import 'express-validator'
4import { values } from 'lodash' 3import { values } from 'lodash'
@@ -6,12 +5,10 @@ import 'multer'
6import * as validator from 'validator' 5import * as validator from 'validator'
7import { VideoRateType } from '../../../shared' 6import { VideoRateType } from '../../../shared'
8import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers' 7import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers'
8import { VIDEO_PRIVACIES } from '../../initializers/constants'
9import { database as db } from '../../initializers/database' 9import { database as db } from '../../initializers/database'
10import { VideoInstance } from '../../models/video/video-interface' 10import { VideoInstance } from '../../models/video/video-interface'
11import { logger } from '../logger'
12import { isActivityPubUrlValid } from './activitypub/misc'
13import { exists, isArray } from './misc' 11import { exists, isArray } from './misc'
14import { VIDEO_PRIVACIES } from '../../initializers/constants'
15 12
16const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 13const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
17const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES 14const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
@@ -20,10 +17,6 @@ function isVideoCategoryValid (value: number) {
20 return VIDEO_CATEGORIES[value] !== undefined 17 return VIDEO_CATEGORIES[value] !== undefined
21} 18}
22 19
23function isVideoUrlValid (value: string) {
24 return isActivityPubUrlValid(value)
25}
26
27function isVideoLicenceValid (value: number) { 20function isVideoLicenceValid (value: number) {
28 return VIDEO_LICENCES[value] !== undefined 21 return VIDEO_LICENCES[value] !== undefined
29} 22}
@@ -106,31 +99,7 @@ function isVideoFileSizeValid (value: string) {
106 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) 99 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
107} 100}
108 101
109function checkVideoExists (id: string, res: Response, callback: () => void) { 102async function isVideoExist (id: string, res: Response) {
110 let promise: Bluebird<VideoInstance>
111 if (validator.isInt(id)) {
112 promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id)
113 } else { // UUID
114 promise = db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id)
115 }
116
117 promise.then(video => {
118 if (!video) {
119 return res.status(404)
120 .json({ error: 'Video not found' })
121 .end()
122 }
123
124 res.locals.video = video
125 callback()
126 })
127 .catch(err => {
128 logger.error('Error in video request validator.', err)
129 return res.sendStatus(500)
130 })
131}
132
133async function isVideoExistsPromise (id: string, res: Response) {
134 let video: VideoInstance 103 let video: VideoInstance
135 104
136 if (validator.isInt(id)) { 105 if (validator.isInt(id)) {
@@ -169,10 +138,8 @@ export {
169 isVideoRatingTypeValid, 138 isVideoRatingTypeValid,
170 isVideoDurationValid, 139 isVideoDurationValid,
171 isVideoTagValid, 140 isVideoTagValid,
172 isVideoUrlValid,
173 isVideoPrivacyValid, 141 isVideoPrivacyValid,
174 isVideoFileResolutionValid, 142 isVideoFileResolutionValid,
175 isVideoFileSizeValid, 143 isVideoFileSizeValid,
176 checkVideoExists, 144 isVideoExist
177 isVideoExistsPromise
178} 145}