aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/pods.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/middlewares/validators/pods.ts
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
Diffstat (limited to 'server/middlewares/validators/pods.ts')
-rw-r--r--server/middlewares/validators/pods.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/server/middlewares/validators/pods.ts b/server/middlewares/validators/pods.ts
new file mode 100644
index 000000000..fbfd268d0
--- /dev/null
+++ b/server/middlewares/validators/pods.ts
@@ -0,0 +1,63 @@
1const db = require('../../initializers/database')
2import { checkErrors } from './utils'
3import { logger } from '../../helpers'
4import { CONFIG } from '../../initializers'
5import { hasFriends } from '../../lib'
6import { isTestInstance } from '../../helpers'
7
8function makeFriendsValidator (req, res, next) {
9 // Force https if the administrator wants to make friends
10 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
11 return res.status(400).send('Cannot make friends with a non HTTPS webserver.')
12 }
13
14 req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
15
16 logger.debug('Checking makeFriends parameters', { parameters: req.body })
17
18 checkErrors(req, res, function () {
19 hasFriends(function (err, heHasFriends) {
20 if (err) {
21 logger.error('Cannot know if we have friends.', { error: err })
22 res.sendStatus(500)
23 }
24
25 if (heHasFriends === true) {
26 // We need to quit our friends before make new ones
27 return res.sendStatus(409)
28 }
29
30 return next()
31 })
32 })
33}
34
35function podsAddValidator (req, res, next) {
36 req.checkBody('host', 'Should have a host').isHostValid()
37 req.checkBody('email', 'Should have an email').isEmail()
38 req.checkBody('publicKey', 'Should have a public key').notEmpty()
39 logger.debug('Checking podsAdd parameters', { parameters: req.body })
40
41 checkErrors(req, res, function () {
42 db.Pod.loadByHost(req.body.host, function (err, pod) {
43 if (err) {
44 logger.error('Cannot load pod by host.', { error: err })
45 res.sendStatus(500)
46 }
47
48 // Pod with this host already exists
49 if (pod) {
50 return res.sendStatus(409)
51 }
52
53 return next()
54 })
55 })
56}
57
58// ---------------------------------------------------------------------------
59
60export {
61 makeFriendsValidator,
62 podsAddValidator
63}