]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/remote/pods.ts
Fix spelling (#126)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / remote / pods.ts
index 7a9a0c4f06552273e96699584609e1f81ca8edee..326eb61ac183c712176c0517bc28c178e9973a50 100644 (file)
@@ -1,16 +1,35 @@
-import express = require('express')
-import * as waterfall from 'async/waterfall'
+import * as express from 'express'
 
 import { database as db } from '../../../initializers/database'
-import { checkSignature, signatureValidator } from '../../../middlewares'
+import {
+  checkSignature,
+  signatureValidator,
+  setBodyHostPort,
+  remotePodsAddValidator,
+  asyncMiddleware
+} from '../../../middlewares'
+import { sendOwnedDataToPod } from '../../../lib'
+import { getMyPublicCert, getFormattedObjects } from '../../../helpers'
+import { CONFIG } from '../../../initializers'
+import { PodInstance } from '../../../models'
+import { PodSignature, Pod as FormattedPod } from '../../../../shared'
 
 const remotePodsRouter = express.Router()
 
-// Post because this is a secured request
 remotePodsRouter.post('/remove',
   signatureValidator,
   checkSignature,
-  removePods
+  asyncMiddleware(removePods)
+)
+
+remotePodsRouter.post('/list',
+  asyncMiddleware(remotePodsList)
+)
+
+remotePodsRouter.post('/add',
+  setBodyHostPort, // We need to modify the host before running the validator!
+  remotePodsAddValidator,
+  asyncMiddleware(addPods)
 )
 
 // ---------------------------------------------------------------------------
@@ -21,20 +40,30 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function removePods (req, res, next) {
-  const host = req.body.signature.host
+async function addPods (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const information = req.body
+
+  const pod = db.Pod.build(information)
+  const podCreated = await pod.save()
+
+  await sendOwnedDataToPod(podCreated.id)
+
+  const cert = await getMyPublicCert()
+  return res.json({ cert, email: CONFIG.ADMIN.EMAIL })
+}
+
+async function remotePodsList (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const pods = await db.Pod.list()
+
+  return res.json(getFormattedObjects<FormattedPod, PodInstance>(pods, pods.length))
+}
 
-  waterfall([
-    function loadPod (callback) {
-      db.Pod.loadByHost(host, callback)
-    },
+async function removePods (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const signature: PodSignature = req.body.signature
+  const host = signature.host
 
-    function deletePod (pod, callback) {
-      pod.destroy().asCallback(callback)
-    }
-  ], function (err) {
-    if (err) return next(err)
+  const pod = await db.Pod.loadByHost(host)
+  await pod.destroy()
 
-    return res.type('json').status(204).end()
-  })
+  return res.type('json').status(204).end()
 }