aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/accounts.ts
diff options
context:
space:
mode:
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}