]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/v1/pods.js
reqValidators --> validators
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
index ecaeba666726f1226a5373421ce50a0037b0debe..4413fbc1e49aaa732a8720b2e816c388584a3660 100644 (file)
@@ -2,26 +2,25 @@
 
 const async = require('async')
 const express = require('express')
+const mongoose = require('mongoose')
 
 const logger = require('../../../helpers/logger')
 const friends = require('../../../lib/friends')
 const middlewares = require('../../../middlewares')
-const Pods = require('../../../models/pods')
-const oAuth2 = middlewares.oauth2
-const reqValidator = middlewares.reqValidators.pods
-const secureMiddleware = middlewares.secure
-const secureRequest = middlewares.reqValidators.remote.secureRequest
-const videos = require('../../../lib/videos')
-const Videos = require('../../../models/videos')
+const oAuth = middlewares.oauth
+const validators = middlewares.validators.pods
+const signatureValidator = middlewares.validators.remote.signature
 
 const router = express.Router()
+const Pod = mongoose.model('Pod')
+const Video = mongoose.model('Video')
 
-router.get('/', listPods)
-router.post('/', reqValidator.podsAdd, addPods)
-router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends)
-router.get('/quitfriends', oAuth2.authenticate, quitFriends)
+router.get('/', listPodsUrl)
+router.post('/', validators.podsAdd, addPods)
+router.get('/makefriends', oAuth.authenticate, validators.makeFriends, makeFriends)
+router.get('/quitfriends', oAuth.authenticate, quitFriends)
 // Post because this is a secured request
-router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
+router.post('/remove', signatureValidator, removePods)
 
 // ---------------------------------------------------------------------------
 
@@ -30,22 +29,21 @@ module.exports = router
 // ---------------------------------------------------------------------------
 
 function addPods (req, res, next) {
-  const informations = req.body.data
+  const informations = req.body
 
   async.waterfall([
     function addPod (callback) {
-      Pods.add(informations, function (err) {
-        return callback(err)
+      const pod = new Pod(informations)
+      pod.save(function (err, podCreated) {
+        // Be sure about the number of parameters for the callback
+        return callback(err, podCreated)
       })
     },
 
-    function createVideosOfThisPod (callback) {
-      // Create the remote videos from the new pod
-      videos.createRemoteVideos(informations.videos, function (err) {
-        if (err) logger.error('Cannot create remote videos.', { error: err })
+    function sendMyVideos (podCreated, callback) {
+      friends.sendOwnedVideosToPod(podCreated._id)
 
-        return callback(err)
-      })
+      callback(null)
     },
 
     function fetchMyCertificate (callback) {
@@ -57,30 +55,19 @@ function addPods (req, res, next) {
 
         return callback(null, cert)
       })
-    },
-
-    function getListOfMyVideos (cert, callback) {
-      Videos.listOwned(function (err, videosList) {
-        if (err) {
-          logger.error('Cannot get the list of owned videos.')
-          return callback(err)
-        }
-
-        return callback(null, cert, videosList)
-      })
     }
-  ], function (err, cert, videosList) {
+  ], function (err, cert) {
     if (err) return next(err)
 
-    return res.json({ cert: cert, videos: videosList })
+    return res.json({ cert: cert })
   })
 }
 
-function listPods (req, res, next) {
-  Pods.list(function (err, podsList) {
+function listPodsUrl (req, res, next) {
+  Pod.listOnlyUrls(function (err, podsUrlList) {
     if (err) return next(err)
 
-    res.json(podsList)
+    res.json(podsUrlList)
   })
 }
 
@@ -96,14 +83,19 @@ function removePods (req, res, next) {
   const url = req.body.signature.url
 
   async.waterfall([
-    function (callback) {
-      Pods.remove(url, function (err) {
+    function loadPod (callback) {
+      Pod.loadByUrl(url, callback)
+    },
+
+    function removePod (pod, callback) {
+      pod.remove(function (err) {
+        // Be sure we only return one argument in the callback
         return callback(err)
       })
     },
 
     function (callback) {
-      Videos.listFromUrl(url, function (err, videosList) {
+      Video.listByUrls([ url ], function (err, videosList) {
         if (err) {
           logger.error('Cannot list videos from url.', { error: err })
           return callback(err)
@@ -114,14 +106,9 @@ function removePods (req, res, next) {
     },
 
     function removeTheRemoteVideos (videosList, callback) {
-      videos.removeRemoteVideos(videosList, function (err) {
-        if (err) {
-          logger.error('Cannot remove remote videos.', { error: err })
-          return callback(err)
-        }
-
-        return callback(null)
-      })
+      async.each(videosList, function (video, callbackEach) {
+        video.remove(callbackEach)
+      }, callback)
     }
   ], function (err) {
     if (err) return next(err)