1 import { database as db } from '../../initializers/database'
2 import { checkErrors } from './utils'
3 import { logger } from '../../helpers'
4 import { CONFIG } from '../../initializers'
5 import { hasFriends } from '../../lib'
6 import { isTestInstance } from '../../helpers'
8 function 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.')
14 req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
16 logger.debug('Checking makeFriends parameters', { parameters: req.body })
18 checkErrors(req, res, function () {
19 hasFriends(function (err, heHasFriends) {
21 logger.error('Cannot know if we have friends.', { error: err })
25 if (heHasFriends === true) {
26 // We need to quit our friends before make new ones
27 return res.sendStatus(409)
35 function 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 })
41 checkErrors(req, res, function () {
42 db.Pod.loadByHost(req.body.host, function (err, pod) {
44 logger.error('Cannot load pod by host.', { error: err })
48 // Pod with this host already exists
50 return res.sendStatus(409)
58 // ---------------------------------------------------------------------------