]>
Commit | Line | Data |
---|---|---|
1 | import * as Bluebird from 'bluebird' | |
2 | import { Response } from 'express' | |
3 | import 'express-validator' | |
4 | import * as validator from 'validator' | |
5 | import { AccountModel } from '../../models/account/account' | |
6 | import { isUserDescriptionValid, isUserUsernameValid } from './users' | |
7 | ||
8 | function isAccountNameValid (value: string) { | |
9 | return isUserUsernameValid(value) | |
10 | } | |
11 | ||
12 | function isAccountDescriptionValid (value: string) { | |
13 | return isUserDescriptionValid(value) | |
14 | } | |
15 | ||
16 | function isAccountIdExist (id: number | string, res: Response) { | |
17 | let promise: Bluebird<AccountModel> | |
18 | ||
19 | if (validator.isInt('' + id)) { | |
20 | promise = AccountModel.load(+id) | |
21 | } else { // UUID | |
22 | promise = AccountModel.loadByUUID('' + id) | |
23 | } | |
24 | ||
25 | return isAccountExist(promise, res) | |
26 | } | |
27 | ||
28 | function isLocalAccountNameExist (name: string, res: Response) { | |
29 | const promise = AccountModel.loadLocalByName(name) | |
30 | ||
31 | return isAccountExist(promise, res) | |
32 | } | |
33 | ||
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 | ||
44 | async function isAccountExist (p: Bluebird<AccountModel>, res: Response) { | |
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 | |
58 | } | |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | export { | |
63 | isAccountIdExist, | |
64 | isLocalAccountNameExist, | |
65 | isAccountDescriptionValid, | |
66 | isAccountNameWithHostExist, | |
67 | isAccountNameValid | |
68 | } |