]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b | 3 | const checkErrors = require('./utils').checkErrors |
441b66f8 | 4 | const constants = require('../../initializers/constants') |
f0f5567b C |
5 | const friends = require('../../lib/friends') |
6 | const logger = require('../../helpers/logger') | |
441b66f8 | 7 | const utils = require('../../helpers/utils') |
9f10b292 | 8 | |
fc51fde0 | 9 | const validatorsPod = { |
c4403b29 C |
10 | makeFriends, |
11 | podsAdd | |
9f10b292 C |
12 | } |
13 | ||
14 | function makeFriends (req, res, next) { | |
441b66f8 C |
15 | // Force https if the administrator wants to make friends |
16 | if (utils.isTestInstance() === false && constants.CONFIG.WEBSERVER.SCHEME === 'http') { | |
17 | return res.status(400).send('Cannot make friends with a non HTTPS webserver.') | |
18 | } | |
19 | ||
49abbbbe | 20 | req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid() |
1e2564d3 C |
21 | |
22 | logger.debug('Checking makeFriends parameters', { parameters: req.body }) | |
23 | ||
d57d6f26 C |
24 | checkErrors(req, res, function () { |
25 | friends.hasFriends(function (err, hasFriends) { | |
26 | if (err) { | |
27 | logger.error('Cannot know if we have friends.', { error: err }) | |
28 | res.sendStatus(500) | |
29 | } | |
30 | ||
31 | if (hasFriends === true) { | |
32 | // We need to quit our friends before make new ones | |
33 | res.sendStatus(409) | |
34 | } else { | |
35 | return next() | |
36 | } | |
37 | }) | |
9f10b292 C |
38 | }) |
39 | } | |
40 | ||
41 | function podsAdd (req, res, next) { | |
49abbbbe | 42 | req.checkBody('host', 'Should have an host').notEmpty().isURL() |
528a9efa C |
43 | req.checkBody('publicKey', 'Should have a public key').notEmpty() |
44 | ||
45 | // TODO: check we don't have it already | |
9f10b292 C |
46 | |
47 | logger.debug('Checking podsAdd parameters', { parameters: req.body }) | |
48 | ||
49 | checkErrors(req, res, next) | |
50 | } | |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | ||
fc51fde0 | 54 | module.exports = validatorsPod |