]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/pods.ts
require -> import
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pods.ts
CommitLineData
e02643f3 1import { database as db } from '../../initializers/database'
65fcc311
C
2import { checkErrors } from './utils'
3import { logger } from '../../helpers'
4import { CONFIG } from '../../initializers'
5import { hasFriends } from '../../lib'
6import { isTestInstance } from '../../helpers'
9f10b292 7
65fcc311 8function makeFriendsValidator (req, res, next) {
441b66f8 9 // Force https if the administrator wants to make friends
65fcc311 10 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
441b66f8
C
11 return res.status(400).send('Cannot make friends with a non HTTPS webserver.')
12 }
13
49abbbbe 14 req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
1e2564d3
C
15
16 logger.debug('Checking makeFriends parameters', { parameters: req.body })
17
d57d6f26 18 checkErrors(req, res, function () {
65fcc311 19 hasFriends(function (err, heHasFriends) {
d57d6f26
C
20 if (err) {
21 logger.error('Cannot know if we have friends.', { error: err })
22 res.sendStatus(500)
23 }
24
65fcc311 25 if (heHasFriends === true) {
d57d6f26 26 // We need to quit our friends before make new ones
b09ce645 27 return res.sendStatus(409)
d57d6f26 28 }
b09ce645
C
29
30 return next()
d57d6f26 31 })
9f10b292
C
32 })
33}
34
65fcc311 35function podsAddValidator (req, res, next) {
4793c343
C
36 req.checkBody('host', 'Should have a host').isHostValid()
37 req.checkBody('email', 'Should have an email').isEmail()
528a9efa 38 req.checkBody('publicKey', 'Should have a public key').notEmpty()
b09ce645 39 logger.debug('Checking podsAdd parameters', { parameters: req.body })
528a9efa 40
b09ce645
C
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 }
9f10b292 47
b09ce645
C
48 // Pod with this host already exists
49 if (pod) {
50 return res.sendStatus(409)
51 }
9f10b292 52
b09ce645
C
53 return next()
54 })
55 })
9f10b292
C
56}
57
58// ---------------------------------------------------------------------------
59
65fcc311
C
60export {
61 makeFriendsValidator,
62 podsAddValidator
63}