]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/pods.ts
Remove any typing from server
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.ts
index a028c4ab9a1073b0debadf05a557902613199f1d..5210f9fe4a2d28388b055fb3d0d48510bdf00b50 100644 (file)
@@ -1,5 +1,4 @@
 import * as express from 'express'
-import { waterfall } from 'async'
 
 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<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))
 }