-import express = require('express')
-import { waterfall } from 'async'
+import * as express from 'express'
import { database as db } from '../../initializers/database'
import { CONFIG } from '../../initializers'
setBodyHostPort,
setBodyHostsPort
} from '../../middlewares'
+import {
+ PodInstance
+} from '../../models'
+import { Pod as FormatedPod } from '../../../shared'
const podsRouter = express.Router()
// ---------------------------------------------------------------------------
-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(getFormatedObjects<FormatedPod, 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)
-
- res.type('json').status(204).end()
- })
+function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
+ quitFriends()
+ .then(() => res.type('json').status(204).end())
+ .catch(err => next(err))
}