1 import { body, param } from 'express-validator/check'
2 import * as express from 'express'
4 import { database as db } from '../../initializers/database'
5 import { checkErrors } from './utils'
6 import { logger, isEachUniqueHostValid } from '../../helpers'
7 import { CONFIG } from '../../initializers'
8 import { hasFriends } from '../../lib'
9 import { isTestInstance } from '../../helpers'
11 const makeFriendsValidator = [
12 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
14 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 // Force https if the administrator wants to make friends
16 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
17 return res.status(400)
19 error: 'Cannot make friends with a non HTTPS web server.'
24 logger.debug('Checking makeFriends parameters', { parameters: req.body })
26 checkErrors(req, res, () => {
28 .then(heHasFriends => {
29 if (heHasFriends === true) {
30 // We need to quit our friends before make new ones
31 return res.sendStatus(409)
37 logger.error('Cannot know if we have friends.', err)
44 const podRemoveValidator = [
45 param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'),
47 (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 logger.debug('Checking podRemoveValidator parameters', { parameters: req.params })
50 checkErrors(req, res, () => {
51 db.Pod.load(req.params.id)
54 logger.error('Cannot find pod %d.', req.params.id)
55 return res.sendStatus(404)
62 logger.error('Cannot load pod %d.', req.params.id, err)
69 // ---------------------------------------------------------------------------