diff options
Diffstat (limited to 'server/helpers/custom-validators/accounts.ts')
-rw-r--r-- | server/helpers/custom-validators/accounts.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts new file mode 100644 index 000000000..6d6219a95 --- /dev/null +++ b/server/helpers/custom-validators/accounts.ts | |||
@@ -0,0 +1,53 @@ | |||
1 | import * as Promise from 'bluebird' | ||
2 | import * as validator from 'validator' | ||
3 | import * as express from 'express' | ||
4 | import 'express-validator' | ||
5 | |||
6 | import { database as db } from '../../initializers' | ||
7 | import { AccountInstance } from '../../models' | ||
8 | import { logger } from '../logger' | ||
9 | |||
10 | import { isUserUsernameValid } from './users' | ||
11 | import { isHostValid } from './pods' | ||
12 | |||
13 | function isAccountNameValid (value: string) { | ||
14 | return isUserUsernameValid(value) | ||
15 | } | ||
16 | |||
17 | function isAccountNameWithHostValid (value: string) { | ||
18 | const [ name, host ] = value.split('@') | ||
19 | |||
20 | return isAccountNameValid(name) && isHostValid(host) | ||
21 | } | ||
22 | |||
23 | function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) { | ||
24 | let promise: Promise<AccountInstance> | ||
25 | if (validator.isInt(id)) { | ||
26 | promise = db.Account.load(+id) | ||
27 | } else { // UUID | ||
28 | promise = db.Account.loadByUUID(id) | ||
29 | } | ||
30 | |||
31 | promise.then(account => { | ||
32 | if (!account) { | ||
33 | return res.status(404) | ||
34 | .json({ error: 'Video account not found' }) | ||
35 | .end() | ||
36 | } | ||
37 | |||
38 | res.locals.account = account | ||
39 | callback() | ||
40 | }) | ||
41 | .catch(err => { | ||
42 | logger.error('Error in video account request validator.', err) | ||
43 | return res.sendStatus(500) | ||
44 | }) | ||
45 | } | ||
46 | |||
47 | // --------------------------------------------------------------------------- | ||
48 | |||
49 | export { | ||
50 | checkVideoAccountExists, | ||
51 | isAccountNameWithHostValid, | ||
52 | isAccountNameValid | ||
53 | } | ||