X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Fcontrollers%2Fapi%2Fpods.ts;h=5210f9fe4a2d28388b055fb3d0d48510bdf00b50;hb=0a6658fdcbd779ada8f3758048c326e997902d5a;hp=4ff1f5d9fa6dd8b237d493f3dfa8fa2b2c663425;hpb=e02643f32e4c97ca307f8fc5b69be79c40d70a3b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/pods.ts b/server/controllers/api/pods.ts index 4ff1f5d9f..5210f9fe4 100644 --- a/server/controllers/api/pods.ts +++ b/server/controllers/api/pods.ts @@ -1,5 +1,4 @@ -import express = require('express') -import { waterfall } from 'async' +import * as express from 'express' import { database as db } from '../../initializers/database' import { CONFIG } from '../../initializers' @@ -21,6 +20,10 @@ import { setBodyHostPort, setBodyHostsPort } from '../../middlewares' +import { + PodInstance +} from '../../models' +import { Pod as FormatedPod } from '../../../shared' const podsRouter = express.Router() @@ -51,68 +54,42 @@ export { // --------------------------------------------------------------------------- -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(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)) }