]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/pods.js
0bf4b18446fb04791e258ef3df44bc45f7545a1a
[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 a host').isHostValid()
44 req.checkBody('email', 'Should have an email').isEmail()
45 req.checkBody('publicKey', 'Should have a public key').notEmpty()
46 logger.debug('Checking podsAdd parameters', { parameters: req.body })
47
48 checkErrors(req, res, function () {
49 db.Pod.loadByHost(req.body.host, function (err, pod) {
50 if (err) {
51 logger.error('Cannot load pod by host.', { error: err })
52 res.sendStatus(500)
53 }
54
55 // Pod with this host already exists
56 if (pod) {
57 return res.sendStatus(409)
58 }
59
60 return next()
61 })
62 })
63 }
64
65 // ---------------------------------------------------------------------------
66
67 module.exports = validatorsPod