]>
Commit | Line | Data |
---|---|---|
1 | import * as Bluebird from 'bluebird' | |
2 | import { Response } from 'express' | |
3 | import 'express-validator' | |
4 | import { AccountModel } from '../../models/account/account' | |
5 | import { isUserDescriptionValid, isUserUsernameValid } from './users' | |
6 | import { exists } from './misc' | |
7 | ||
8 | function isAccountNameValid (value: string) { | |
9 | return isUserUsernameValid(value) | |
10 | } | |
11 | ||
12 | function isAccountIdValid (value: string) { | |
13 | return exists(value) | |
14 | } | |
15 | ||
16 | function isAccountDescriptionValid (value: string) { | |
17 | return isUserDescriptionValid(value) | |
18 | } | |
19 | ||
20 | function doesAccountIdExist (id: number, res: Response, sendNotFound = true) { | |
21 | const promise = AccountModel.load(id) | |
22 | ||
23 | return doesAccountExist(promise, res, sendNotFound) | |
24 | } | |
25 | ||
26 | function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) { | |
27 | const promise = AccountModel.loadLocalByName(name) | |
28 | ||
29 | return doesAccountExist(promise, res, sendNotFound) | |
30 | } | |
31 | ||
32 | function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { | |
33 | return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound) | |
34 | } | |
35 | ||
36 | async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) { | |
37 | const account = await p | |
38 | ||
39 | if (!account) { | |
40 | if (sendNotFound === true) { | |
41 | res.status(404) | |
42 | .send({ error: 'Account not found' }) | |
43 | .end() | |
44 | } | |
45 | ||
46 | return false | |
47 | } | |
48 | ||
49 | res.locals.account = account | |
50 | ||
51 | return true | |
52 | } | |
53 | ||
54 | // --------------------------------------------------------------------------- | |
55 | ||
56 | export { | |
57 | isAccountIdValid, | |
58 | doesAccountIdExist, | |
59 | doesLocalAccountNameExist, | |
60 | isAccountDescriptionValid, | |
61 | doesAccountNameWithHostExist, | |
62 | isAccountNameValid | |
63 | } |