]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/v1/pods.js
Server: add pod created date and score to the list controller
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
index e216714d6addf3abcc0d16996236631a5b8f303e..2bdfe0c923b6c623cef73a8ae90facf4d9a8eab3 100644 (file)
@@ -1,27 +1,37 @@
 'use strict'
 
-const async = require('async')
+const each = require('async/each')
 const express = require('express')
+const mongoose = require('mongoose')
+const waterfall = require('async/waterfall')
 
 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 admin = middlewares.admin
+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.post('/', validators.podsAdd, addPods)
+router.post('/makefriends',
+  oAuth.authenticate,
+  admin.ensureIsAdmin,
+  validators.makeFriends,
+  makeFriends
+)
+router.get('/quitfriends',
+  oAuth.authenticate,
+  admin.ensureIsAdmin,
+  quitFriends
+)
 // Post because this is a secured request
-router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
+router.post('/remove', signatureValidator, removePods)
 
 // ---------------------------------------------------------------------------
 
@@ -30,22 +40,21 @@ module.exports = router
 // ---------------------------------------------------------------------------
 
 function addPods (req, res, next) {
-  const informations = req.body.data
+  const informations = req.body
 
-  async.waterfall([
+  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,53 +66,54 @@ 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) {
+  Pod.list(function (err, podsUrlList) {
     if (err) return next(err)
 
-    res.json(podsList)
+    res.json(getFormatedPods(podsUrlList))
   })
 }
 
 function makeFriends (req, res, next) {
-  friends.makeFriends(function (err) {
-    if (err) return next(err)
+  const urls = req.body.urls
 
-    res.type('json').status(204).end()
+  friends.makeFriends(urls, function (err) {
+    if (err) {
+      logger.error('Could not make friends.', { error: err })
+      return
+    }
+
+    logger.info('Made friends!')
   })
+
+  res.type('json').status(204).end()
 }
 
 function removePods (req, res, next) {
   const url = req.body.signature.url
 
-  async.waterfall([
-    function (callback) {
-      Pods.remove(url, function (err) {
+  waterfall([
+    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 +124,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 })
-          callback(err)
-        }
-
-        return callback(null)
-      })
+      each(videosList, function (video, callbackEach) {
+        video.remove(callbackEach)
+      }, callback)
     }
   ], function (err) {
     if (err) return next(err)
@@ -137,3 +142,15 @@ function quitFriends (req, res, next) {
     res.type('json').status(204).end()
   })
 }
+
+// ---------------------------------------------------------------------------
+
+function getFormatedPods (pods) {
+  const formatedPods = []
+
+  pods.forEach(function (pod) {
+    formatedPods.push(pod.toFormatedJSON())
+  })
+
+  return formatedPods
+}