diff options
Diffstat (limited to 'server/helpers/custom-validators/accounts.ts')
-rw-r--r-- | server/helpers/custom-validators/accounts.ts | 46 |
1 files changed, 22 insertions, 24 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 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import * as express from 'express' | 2 | import { Response } from 'express' |
3 | import 'express-validator' | 3 | import 'express-validator' |
4 | import * as validator from 'validator' | 4 | import * as validator from 'validator' |
5 | import { database as db } from '../../initializers' | 5 | import { database as db } from '../../initializers' |
6 | import { AccountInstance } from '../../models' | 6 | import { AccountInstance } from '../../models' |
7 | import { logger } from '../logger' | ||
8 | import { isUserUsernameValid } from './users' | 7 | import { isUserUsernameValid } from './users' |
9 | 8 | ||
10 | function isAccountNameValid (value: string) { | 9 | function isAccountNameValid (value: string) { |
11 | return isUserUsernameValid(value) | 10 | return isUserUsernameValid(value) |
12 | } | 11 | } |
13 | 12 | ||
14 | function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | 13 | function 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 | ||
26 | function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | 25 | function 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 | ||
32 | function checkAccountExists (p: Bluebird<AccountInstance>, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | 31 | async 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 | ||
51 | export { | 49 | export { |
52 | checkAccountIdExists, | 50 | isAccountIdExist, |
53 | checkLocalAccountNameExists, | 51 | isLocalAccountNameExist, |
54 | isAccountNameValid | 52 | isAccountNameValid |
55 | } | 53 | } |