-import express = require('express')
-import { waterfall } from 'async'
+import * as express from 'express'
-const db = require('../../initializers/database')
+import { database as db } from '../../initializers/database'
import { CONFIG } from '../../initializers'
import {
logger,
getMyPublicCert,
- getFormatedObjects
+ getFormattedObjects
} from '../../helpers'
import {
sendOwnedVideosToPod,
makeFriends,
- quitFriends
+ quitFriends,
+ removeFriend
} from '../../lib'
import {
podsAddValidator,
ensureIsAdmin,
makeFriendsValidator,
setBodyHostPort,
- setBodyHostsPort
+ setBodyHostsPort,
+ podRemoveValidator
} from '../../middlewares'
+import {
+ PodInstance
+} from '../../models'
+import { Pod as FormattedPod } from '../../../shared'
const podsRouter = express.Router()
podsAddValidator,
addPods
)
-podsRouter.post('/makefriends',
+podsRouter.post('/make-friends',
authenticate,
ensureIsAdmin,
makeFriendsValidator,
setBodyHostsPort,
- makeFriends
+ makeFriendsController
)
-podsRouter.get('/quitfriends',
+podsRouter.get('/quit-friends',
authenticate,
ensureIsAdmin,
- quitFriends
+ quitFriendsController
+)
+podsRouter.delete('/:id',
+ authenticate,
+ ensureIsAdmin,
+ podRemoveValidator,
+ removeFriendController
)
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
-function addPods (req, res, next) {
+function addPods (req: express.Request, res: express.Response, next: express.NextFunction) {
const informations = req.body
- waterfall([
- function addPod (callback) {
- const pod = db.Pod.build(informations)
- pod.save().asCallback(function (err, podCreated) {
- // Be sure about the number of parameters for the callback
- return callback(err, podCreated)
- })
- },
-
- function sendMyVideos (podCreated, callback) {
- sendOwnedVideosToPod(podCreated.id)
-
- callback(null)
- },
-
- function fetchMyCertificate (callback) {
- getMyPublicCert(function (err, cert) {
- if (err) {
- logger.error('Cannot read cert file.')
- return callback(err)
- }
-
- return callback(null, cert)
- })
- }
- ], function (err, cert) {
- if (err) return next(err)
-
- return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
- })
+ const pod = db.Pod.build(informations)
+ pod.save()
+ .then(podCreated => {
+ return sendOwnedVideosToPod(podCreated.id)
+ })
+ .then(() => {
+ return getMyPublicCert()
+ })
+ .then(cert => {
+ return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
+ })
+ .catch(err => next(err))
}
-function listPods (req, res, next) {
- db.Pod.list(function (err, podsList) {
- if (err) return next(err)
-
- res.json(getFormatedObjects(podsList, podsList.length))
- })
+function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
+ db.Pod.list()
+ .then(podsList => res.json(getFormattedObjects<FormattedPod, PodInstance>(podsList, podsList.length)))
+ .catch(err => next(err))
}
-function makeFriendsController (req, res, next) {
- const hosts = req.body.hosts
+function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const hosts = req.body.hosts as string[]
- makeFriends(hosts, function (err) {
- if (err) {
- logger.error('Could not make friends.', { error: err })
- return
- }
-
- logger.info('Made friends!')
- })
+ makeFriends(hosts)
+ .then(() => logger.info('Made friends!'))
+ .catch(err => logger.error('Could not make friends.', err))
+ // Don't wait the process that could be long
res.type('json').status(204).end()
}
-function quitFriendsController (req, res, next) {
- quitFriends(function (err) {
- if (err) return next(err)
+function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
+ quitFriends()
+ .then(() => res.type('json').status(204).end())
+ .catch(err => next(err))
+}
+
+function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const pod = res.locals.pod as PodInstance
- res.type('json').status(204).end()
- })
+ removeFriend(pod)
+ .then(() => (res.type('json').status(204).end()))
+ .catch(err => next(err))
}