aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/accounts.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-14 17:31:26 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad (patch)
treef4191f3c04a5230fcf8ca3d6ca3248643fc4151d /server/helpers/custom-validators/accounts.ts
parente34c85e527100c0b5c44567bd951e95be41b8d7e (diff)
downloadPeerTube-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.ts53
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 @@
1import * as Promise from 'bluebird'
2import * as validator from 'validator'
3import * as express from 'express'
4import 'express-validator'
5
6import { database as db } from '../../initializers'
7import { AccountInstance } from '../../models'
8import { logger } from '../logger'
9
10import { isUserUsernameValid } from './users'
11import { isHostValid } from './pods'
12
13function isAccountNameValid (value: string) {
14 return isUserUsernameValid(value)
15}
16
17function isAccountNameWithHostValid (value: string) {
18 const [ name, host ] = value.split('@')
19
20 return isAccountNameValid(name) && isHostValid(host)
21}
22
23function 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
49export {
50 checkVideoAccountExists,
51 isAccountNameWithHostValid,
52 isAccountNameValid
53}