1 import 'express-validator'
2 import * as express from 'express'
4 import { database as db } from '../../initializers/database'
5 import { checkErrors } from './utils'
6 import { logger } from '../../helpers'
7 import { CONFIG } from '../../initializers'
8 import { hasFriends } from '../../lib'
9 import { isTestInstance } from '../../helpers'
11 function makeFriendsValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
12 // Force https if the administrator wants to make friends
13 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
14 return res.status(400).send('Cannot make friends with a non HTTPS web server.')
17 req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
19 logger.debug('Checking makeFriends parameters', { parameters: req.body })
21 checkErrors(req, res, () => {
23 .then(heHasFriends => {
24 if (heHasFriends === true) {
25 // We need to quit our friends before make new ones
26 return res.sendStatus(409)
32 logger.error('Cannot know if we have friends.', err)
38 function podsAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
39 req.checkBody('host', 'Should have a host').isHostValid()
40 req.checkBody('email', 'Should have an email').isEmail()
41 req.checkBody('publicKey', 'Should have a public key').notEmpty()
42 logger.debug('Checking podsAdd parameters', { parameters: req.body })
44 checkErrors(req, res, () => {
45 db.Pod.loadByHost(req.body.host)
47 // Pod with this host already exists
49 return res.sendStatus(409)
55 logger.error('Cannot load pod by host.', err)
61 function podRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
62 req.checkParams('id', 'Should have a valid id').notEmpty().isNumeric()
64 logger.debug('Checking podRemoveValidator parameters', { parameters: req.params })
66 checkErrors(req, res, function () {
67 db.Pod.load(req.params.id)
70 logger.error('Cannot find pod %d.', req.params.id)
71 return res.sendStatus(404)
78 logger.error('Cannot load pod %d.', req.params.id, err)
84 // ---------------------------------------------------------------------------