diff options
Diffstat (limited to 'server/helpers/custom-validators/accounts.ts')
-rw-r--r-- | server/helpers/custom-validators/accounts.ts | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index cc8641d6b..00dea9039 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts | |||
@@ -4,16 +4,21 @@ import 'express-validator' | |||
4 | import * as validator from 'validator' | 4 | import * as validator from 'validator' |
5 | import { AccountModel } from '../../models/account/account' | 5 | import { AccountModel } from '../../models/account/account' |
6 | import { isUserDescriptionValid, isUserUsernameValid } from './users' | 6 | import { isUserDescriptionValid, isUserUsernameValid } from './users' |
7 | import { exists } from './misc' | ||
7 | 8 | ||
8 | function isAccountNameValid (value: string) { | 9 | function isAccountNameValid (value: string) { |
9 | return isUserUsernameValid(value) | 10 | return isUserUsernameValid(value) |
10 | } | 11 | } |
11 | 12 | ||
13 | function isAccountIdValid (value: string) { | ||
14 | return exists(value) | ||
15 | } | ||
16 | |||
12 | function isAccountDescriptionValid (value: string) { | 17 | function isAccountDescriptionValid (value: string) { |
13 | return isUserDescriptionValid(value) | 18 | return isUserDescriptionValid(value) |
14 | } | 19 | } |
15 | 20 | ||
16 | function isAccountIdExist (id: number | string, res: Response) { | 21 | function isAccountIdExist (id: number | string, res: Response, sendNotFound = true) { |
17 | let promise: Bluebird<AccountModel> | 22 | let promise: Bluebird<AccountModel> |
18 | 23 | ||
19 | if (validator.isInt('' + id)) { | 24 | if (validator.isInt('' + id)) { |
@@ -22,32 +27,34 @@ function isAccountIdExist (id: number | string, res: Response) { | |||
22 | promise = AccountModel.loadByUUID('' + id) | 27 | promise = AccountModel.loadByUUID('' + id) |
23 | } | 28 | } |
24 | 29 | ||
25 | return isAccountExist(promise, res) | 30 | return isAccountExist(promise, res, sendNotFound) |
26 | } | 31 | } |
27 | 32 | ||
28 | function isLocalAccountNameExist (name: string, res: Response) { | 33 | function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) { |
29 | const promise = AccountModel.loadLocalByName(name) | 34 | const promise = AccountModel.loadLocalByName(name) |
30 | 35 | ||
31 | return isAccountExist(promise, res) | 36 | return isAccountExist(promise, res, sendNotFound) |
32 | } | 37 | } |
33 | 38 | ||
34 | function isAccountNameWithHostExist (nameWithDomain: string, res: Response) { | 39 | function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { |
35 | const [ accountName, host ] = nameWithDomain.split('@') | 40 | const [ accountName, host ] = nameWithDomain.split('@') |
36 | 41 | ||
37 | let promise: Bluebird<AccountModel> | 42 | let promise: Bluebird<AccountModel> |
38 | if (!host) promise = AccountModel.loadLocalByName(accountName) | 43 | if (!host) promise = AccountModel.loadLocalByName(accountName) |
39 | else promise = AccountModel.loadLocalByNameAndHost(accountName, host) | 44 | else promise = AccountModel.loadLocalByNameAndHost(accountName, host) |
40 | 45 | ||
41 | return isAccountExist(promise, res) | 46 | return isAccountExist(promise, res, sendNotFound) |
42 | } | 47 | } |
43 | 48 | ||
44 | async function isAccountExist (p: Bluebird<AccountModel>, res: Response) { | 49 | async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) { |
45 | const account = await p | 50 | const account = await p |
46 | 51 | ||
47 | if (!account) { | 52 | if (!account) { |
48 | res.status(404) | 53 | if (sendNotFound === true) { |
49 | .send({ error: 'Account not found' }) | 54 | res.status(404) |
50 | .end() | 55 | .send({ error: 'Account not found' }) |
56 | .end() | ||
57 | } | ||
51 | 58 | ||
52 | return false | 59 | return false |
53 | } | 60 | } |
@@ -60,6 +67,7 @@ async function isAccountExist (p: Bluebird<AccountModel>, res: Response) { | |||
60 | // --------------------------------------------------------------------------- | 67 | // --------------------------------------------------------------------------- |
61 | 68 | ||
62 | export { | 69 | export { |
70 | isAccountIdValid, | ||
63 | isAccountIdExist, | 71 | isAccountIdExist, |
64 | isLocalAccountNameExist, | 72 | isLocalAccountNameExist, |
65 | isAccountDescriptionValid, | 73 | isAccountDescriptionValid, |