aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r--server/helpers/custom-validators/accounts.ts38
-rw-r--r--server/helpers/custom-validators/video-channels.ts33
-rw-r--r--server/helpers/custom-validators/videos.ts24
3 files changed, 75 insertions, 20 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts
index fe0fc650a..a6d7f2b82 100644
--- a/server/helpers/custom-validators/accounts.ts
+++ b/server/helpers/custom-validators/accounts.ts
@@ -1,4 +1,4 @@
1import * as Promise from 'bluebird' 1import * as Bluebird from 'bluebird'
2import * as express from 'express' 2import * as express from 'express'
3import 'express-validator' 3import 'express-validator'
4import * as validator from 'validator' 4import * as validator from 'validator'
@@ -11,33 +11,45 @@ function isAccountNameValid (value: string) {
11 return isUserUsernameValid(value) 11 return isUserUsernameValid(value)
12} 12}
13 13
14function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) { 14function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) {
15 let promise: Promise<AccountInstance> 15 let promise: Bluebird<AccountInstance>
16 if (validator.isInt(id)) { 16
17 if (validator.isInt('' + id)) {
17 promise = db.Account.load(+id) 18 promise = db.Account.load(+id)
18 } else { // UUID 19 } else { // UUID
19 promise = db.Account.loadByUUID(id) 20 promise = db.Account.loadByUUID('' + id)
20 } 21 }
21 22
22 promise.then(account => { 23 return checkAccountExists(promise, res, callback)
24}
25
26function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) {
27 const p = db.Account.loadLocalByName(name)
28
29 return checkAccountExists(p, res, callback)
30}
31
32function checkAccountExists (p: Bluebird<AccountInstance>, res: express.Response, callback: (err: Error, account: AccountInstance) => any) {
33 p.then(account => {
23 if (!account) { 34 if (!account) {
24 return res.status(404) 35 return res.status(404)
25 .json({ error: 'Video account not found' }) 36 .send({ error: 'Account not found' })
26 .end() 37 .end()
27 } 38 }
28 39
29 res.locals.account = account 40 res.locals.account = account
30 callback() 41 return callback(null, account)
31 })
32 .catch(err => {
33 logger.error('Error in video account request validator.', err)
34 return res.sendStatus(500)
35 }) 42 })
43 .catch(err => {
44 logger.error('Error in account request validator.', err)
45 return res.sendStatus(500)
46 })
36} 47}
37 48
38// --------------------------------------------------------------------------- 49// ---------------------------------------------------------------------------
39 50
40export { 51export {
41 checkVideoAccountExists, 52 checkAccountIdExists,
53 checkLocalAccountNameExists,
42 isAccountNameValid 54 isAccountNameValid
43} 55}
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts
index 5de01f74b..267d987fc 100644
--- a/server/helpers/custom-validators/video-channels.ts
+++ b/server/helpers/custom-validators/video-channels.ts
@@ -1,14 +1,14 @@
1import * as Promise from 'bluebird' 1import * as Bluebird from 'bluebird'
2import * as validator from 'validator'
3import * as express from 'express' 2import * as express from 'express'
4import 'express-validator' 3import 'express-validator'
5import 'multer' 4import 'multer'
5import * as validator from 'validator'
6 6
7import { database as db, CONSTRAINTS_FIELDS } from '../../initializers' 7import { CONSTRAINTS_FIELDS, database as db } from '../../initializers'
8import { VideoChannelInstance } from '../../models' 8import { VideoChannelInstance } from '../../models'
9import { logger } from '../logger' 9import { logger } from '../logger'
10import { exists } from './misc'
11import { isActivityPubUrlValid } from './index' 10import { isActivityPubUrlValid } from './index'
11import { exists } from './misc'
12 12
13const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS 13const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
14 14
@@ -25,7 +25,7 @@ function isVideoChannelNameValid (value: string) {
25} 25}
26 26
27function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { 27function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) {
28 let promise: Promise<VideoChannelInstance> 28 let promise: Bluebird<VideoChannelInstance>
29 if (validator.isInt(id)) { 29 if (validator.isInt(id)) {
30 promise = db.VideoChannel.loadAndPopulateAccount(+id) 30 promise = db.VideoChannel.loadAndPopulateAccount(+id)
31 } else { // UUID 31 } else { // UUID
@@ -48,11 +48,32 @@ function checkVideoChannelExists (id: string, res: express.Response, callback: (
48 }) 48 })
49} 49}
50 50
51async function isVideoChannelExistsPromise (id: string, res: express.Response) {
52 let videoChannel: VideoChannelInstance
53 if (validator.isInt(id)) {
54 videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id)
55 } else { // UUID
56 videoChannel = await db.VideoChannel.loadByUUIDAndPopulateAccount(id)
57 }
58
59 if (!videoChannel) {
60 res.status(404)
61 .json({ error: 'Video channel not found' })
62 .end()
63
64 return false
65 }
66
67 res.locals.videoChannel = videoChannel
68 return true
69}
70
51// --------------------------------------------------------------------------- 71// ---------------------------------------------------------------------------
52 72
53export { 73export {
54 isVideoChannelDescriptionValid, 74 isVideoChannelDescriptionValid,
55 isVideoChannelNameValid,
56 checkVideoChannelExists, 75 checkVideoChannelExists,
76 isVideoChannelNameValid,
77 isVideoChannelExistsPromise,
57 isVideoChannelUrlValid 78 isVideoChannelUrlValid
58} 79}
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 205d8c62f..276354626 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -130,6 +130,27 @@ function checkVideoExists (id: string, res: Response, callback: () => void) {
130 }) 130 })
131} 131}
132 132
133async function isVideoExistsPromise (id: string, res: Response) {
134 let video: VideoInstance
135
136 if (validator.isInt(id)) {
137 video = await db.Video.loadAndPopulateAccountAndServerAndTags(+id)
138 } else { // UUID
139 video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id)
140 }
141
142 if (!video) {
143 res.status(404)
144 .json({ error: 'Video not found' })
145 .end()
146
147 return false
148 }
149
150 res.locals.video = video
151 return true
152}
153
133// --------------------------------------------------------------------------- 154// ---------------------------------------------------------------------------
134 155
135export { 156export {
@@ -152,5 +173,6 @@ export {
152 isVideoPrivacyValid, 173 isVideoPrivacyValid,
153 isVideoFileResolutionValid, 174 isVideoFileResolutionValid,
154 isVideoFileSizeValid, 175 isVideoFileSizeValid,
155 checkVideoExists 176 checkVideoExists,
177 isVideoExistsPromise
156} 178}