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