]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/pods.js
Server: error if we add a pod that already exists
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pods.js
1 'use strict'
2
3 const checkErrors = require('./utils').checkErrors
4 const constants = require('../../initializers/constants')
5 const db = require('../../initializers/database')
6 const friends = require('../../lib/friends')
7 const logger = require('../../helpers/logger')
8 const utils = require('../../helpers/utils')
9
10 const validatorsPod = {
11 makeFriends,
12 podsAdd
13 }
14
15 function makeFriends (req, res, next) {
16 // Force https if the administrator wants to make friends
17 if (utils.isTestInstance() === false && constants.CONFIG.WEBSERVER.SCHEME === 'http') {
18 return res.status(400).send('Cannot make friends with a non HTTPS webserver.')
19 }
20
21 req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
22
23 logger.debug('Checking makeFriends parameters', { parameters: req.body })
24
25 checkErrors(req, res, function () {
26 friends.hasFriends(function (err, hasFriends) {
27 if (err) {
28 logger.error('Cannot know if we have friends.', { error: err })
29 res.sendStatus(500)
30 }
31
32 if (hasFriends === true) {
33 // We need to quit our friends before make new ones
34 return res.sendStatus(409)
35 }
36
37 return next()
38 })
39 })
40 }
41
42 function podsAdd (req, res, next) {
43 req.checkBody('host', 'Should have an host').isHostValid()
44 req.checkBody('publicKey', 'Should have a public key').notEmpty()
45 logger.debug('Checking podsAdd parameters', { parameters: req.body })
46
47 checkErrors(req, res, function () {
48 db.Pod.loadByHost(req.body.host, function (err, pod) {
49 if (err) {
50 logger.error('Cannot load pod by host.', { error: err })
51 res.sendStatus(500)
52 }
53
54 // Pod with this host already exists
55 if (pod) {
56 return res.sendStatus(409)
57 }
58
59 return next()
60 })
61 })
62 }
63
64 // ---------------------------------------------------------------------------
65
66 module.exports = validatorsPod