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