diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-14 17:31:26 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | 350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad (patch) | |
tree | f4191f3c04a5230fcf8ca3d6ca3248643fc4151d /server/helpers/custom-validators/accounts.ts | |
parent | e34c85e527100c0b5c44567bd951e95be41b8d7e (diff) | |
download | PeerTube-350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad.tar.gz PeerTube-350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad.tar.zst PeerTube-350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad.zip |
Follow works
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 | } | ||