]>
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 | import { exists } from './misc' | |
8 | import { CONFIG } from '../../initializers' | |
9 | ||
10 | function isAccountNameValid (value: string) { | |
11 | return isUserUsernameValid(value) | |
12 | } | |
13 | ||
14 | function isAccountIdValid (value: string) { | |
15 | return exists(value) | |
16 | } | |
17 | ||
18 | function isAccountDescriptionValid (value: string) { | |
19 | return isUserDescriptionValid(value) | |
20 | } | |
21 | ||
22 | function isAccountIdExist (id: number | string, res: Response, sendNotFound = true) { | |
23 | let promise: Bluebird<AccountModel> | |
24 | ||
25 | if (validator.isInt('' + id)) { | |
26 | promise = AccountModel.load(+id) | |
27 | } else { // UUID | |
28 | promise = AccountModel.loadByUUID('' + id) | |
29 | } | |
30 | ||
31 | return isAccountExist(promise, res, sendNotFound) | |
32 | } | |
33 | ||
34 | function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) { | |
35 | const promise = AccountModel.loadLocalByName(name) | |
36 | ||
37 | return isAccountExist(promise, res, sendNotFound) | |
38 | } | |
39 | ||
40 | function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { | |
41 | const [ accountName, host ] = nameWithDomain.split('@') | |
42 | ||
43 | let promise: Bluebird<AccountModel> | |
44 | if (!host || host === CONFIG.WEBSERVER.HOST) promise = AccountModel.loadLocalByName(accountName) | |
45 | else promise = AccountModel.loadLocalByNameAndHost(accountName, host) | |
46 | ||
47 | return isAccountExist(promise, res, sendNotFound) | |
48 | } | |
49 | ||
50 | async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) { | |
51 | const account = await p | |
52 | ||
53 | if (!account) { | |
54 | if (sendNotFound === true) { | |
55 | res.status(404) | |
56 | .send({ error: 'Account not found' }) | |
57 | .end() | |
58 | } | |
59 | ||
60 | return false | |
61 | } | |
62 | ||
63 | res.locals.account = account | |
64 | ||
65 | return true | |
66 | } | |
67 | ||
68 | // --------------------------------------------------------------------------- | |
69 | ||
70 | export { | |
71 | isAccountIdValid, | |
72 | isAccountIdExist, | |
73 | isLocalAccountNameExist, | |
74 | isAccountDescriptionValid, | |
75 | isAccountNameWithHostExist, | |
76 | isAccountNameValid | |
77 | } |