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