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