]>
Commit | Line | Data |
---|---|---|
4e50b6a1 | 1 | import * as Bluebird from 'bluebird' |
38fa2065 C |
2 | import * as express from 'express' |
3 | import 'express-validator' | |
d4f1e94c | 4 | import * as validator from 'validator' |
38fa2065 C |
5 | import { database as db } from '../../initializers' |
6 | import { AccountInstance } from '../../models' | |
7 | import { logger } from '../logger' | |
38fa2065 C |
8 | import { isUserUsernameValid } from './users' |
9 | ||
350e31d6 | 10 | function isAccountNameValid (value: string) { |
38fa2065 C |
11 | return isUserUsernameValid(value) |
12 | } | |
13 | ||
4e50b6a1 C |
14 | function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { |
15 | let promise: Bluebird<AccountInstance> | |
16 | ||
17 | if (validator.isInt('' + id)) { | |
38fa2065 C |
18 | promise = db.Account.load(+id) |
19 | } else { // UUID | |
4e50b6a1 | 20 | promise = db.Account.loadByUUID('' + id) |
38fa2065 C |
21 | } |
22 | ||
4e50b6a1 C |
23 | return checkAccountExists(promise, res, callback) |
24 | } | |
25 | ||
26 | function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | |
27 | const p = db.Account.loadLocalByName(name) | |
28 | ||
29 | return checkAccountExists(p, res, callback) | |
30 | } | |
31 | ||
32 | function checkAccountExists (p: Bluebird<AccountInstance>, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | |
33 | p.then(account => { | |
38fa2065 C |
34 | if (!account) { |
35 | return res.status(404) | |
4e50b6a1 | 36 | .send({ error: 'Account not found' }) |
38fa2065 C |
37 | .end() |
38 | } | |
39 | ||
40 | res.locals.account = account | |
4e50b6a1 | 41 | return callback(null, account) |
350e31d6 | 42 | }) |
4e50b6a1 C |
43 | .catch(err => { |
44 | logger.error('Error in account request validator.', err) | |
45 | return res.sendStatus(500) | |
46 | }) | |
38fa2065 C |
47 | } |
48 | ||
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | export { | |
4e50b6a1 C |
52 | checkAccountIdExists, |
53 | checkLocalAccountNameExists, | |
350e31d6 | 54 | isAccountNameValid |
38fa2065 | 55 | } |