]> 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 9ce8120b24c1022277ef9b6e1c7120faf996d0e9..2bdfe0c923b6c623cef73a8ae90facf4d9a8eab3 100644 (file)
@@ -1,24 +1,35 @@
 '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 admin = middlewares.admin
 const oAuth = middlewares.oauth
-const reqValidator = middlewares.reqValidators.pods
-const signatureValidator = middlewares.reqValidators.remote.signature
+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('/', listPodsUrl)
-router.post('/', reqValidator.podsAdd, addPods)
-router.get('/makefriends', oAuth.authenticate, reqValidator.makeFriends, makeFriends)
-router.get('/quitfriends', oAuth.authenticate, quitFriends)
+router.get('/', listPods)
+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', signatureValidator, removePods)
 
@@ -31,7 +42,7 @@ module.exports = router
 function addPods (req, res, next) {
   const informations = req.body
 
-  async.waterfall([
+  waterfall([
     function addPod (callback) {
       const pod = new Pod(informations)
       pod.save(function (err, podCreated) {
@@ -63,26 +74,33 @@ function addPods (req, res, next) {
   })
 }
 
-function listPodsUrl (req, res, next) {
-  Pod.listOnlyUrls(function (err, podsUrlList) {
+function listPods (req, res, next) {
+  Pod.list(function (err, podsUrlList) {
     if (err) return next(err)
 
-    res.json(podsUrlList)
+    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([
+  waterfall([
     function loadPod (callback) {
       Pod.loadByUrl(url, callback)
     },
@@ -106,7 +124,7 @@ function removePods (req, res, next) {
     },
 
     function removeTheRemoteVideos (videosList, callback) {
-      async.each(videosList, function (video, callbackEach) {
+      each(videosList, function (video, callbackEach) {
         video.remove(callbackEach)
       }, callback)
     }
@@ -124,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
+}