]>
Commit | Line | Data |
---|---|---|
4e50b6a1 | 1 | import * as Bluebird from 'bluebird' |
a2431b7d | 2 | import { Response } from 'express' |
38fa2065 | 3 | import 'express-validator' |
d4f1e94c | 4 | import * as validator from 'validator' |
3fd3ab2d | 5 | import { AccountModel } from '../../models/account/account' |
2422c46b | 6 | import { isUserDescriptionValid, isUserUsernameValid } from './users' |
38fa2065 | 7 | |
350e31d6 | 8 | function isAccountNameValid (value: string) { |
38fa2065 C |
9 | return isUserUsernameValid(value) |
10 | } | |
11 | ||
2422c46b C |
12 | function isAccountDescriptionValid (value: string) { |
13 | return isUserDescriptionValid(value) | |
14 | } | |
15 | ||
a2431b7d | 16 | function isAccountIdExist (id: number | string, res: Response) { |
3fd3ab2d | 17 | let promise: Bluebird<AccountModel> |
4e50b6a1 C |
18 | |
19 | if (validator.isInt('' + id)) { | |
3fd3ab2d | 20 | promise = AccountModel.load(+id) |
38fa2065 | 21 | } else { // UUID |
3fd3ab2d | 22 | promise = AccountModel.loadByUUID('' + id) |
38fa2065 C |
23 | } |
24 | ||
a2431b7d | 25 | return isAccountExist(promise, res) |
4e50b6a1 C |
26 | } |
27 | ||
a2431b7d | 28 | function isLocalAccountNameExist (name: string, res: Response) { |
3fd3ab2d | 29 | const promise = AccountModel.loadLocalByName(name) |
4e50b6a1 | 30 | |
a2431b7d | 31 | return isAccountExist(promise, res) |
4e50b6a1 C |
32 | } |
33 | ||
e8cb4409 C |
34 | function isAccountNameWithHostExist (nameWithDomain: string, res: Response) { |
35 | const [ accountName, host ] = nameWithDomain.split('@') | |
36 | ||
37 | let promise: Bluebird<AccountModel> | |
38 | if (!host) promise = AccountModel.loadLocalByName(accountName) | |
39 | else promise = AccountModel.loadLocalByNameAndHost(accountName, host) | |
40 | ||
41 | return isAccountExist(promise, res) | |
42 | } | |
43 | ||
3fd3ab2d | 44 | async function isAccountExist (p: Bluebird<AccountModel>, res: Response) { |
a2431b7d C |
45 | const account = await p |
46 | ||
47 | if (!account) { | |
48 | res.status(404) | |
49 | .send({ error: 'Account not found' }) | |
50 | .end() | |
51 | ||
52 | return false | |
53 | } | |
54 | ||
55 | res.locals.account = account | |
56 | ||
57 | return true | |
38fa2065 C |
58 | } |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | export { | |
a2431b7d C |
63 | isAccountIdExist, |
64 | isLocalAccountNameExist, | |
2422c46b | 65 | isAccountDescriptionValid, |
e8cb4409 | 66 | isAccountNameWithHostExist, |
350e31d6 | 67 | isAccountNameValid |
38fa2065 | 68 | } |