aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/accounts.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators/accounts.ts')
-rw-r--r--server/helpers/custom-validators/accounts.ts38
1 files changed, 25 insertions, 13 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}