]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/friends.js
Server: make friends urls come from the request instead of the
[github/Chocobozzz/PeerTube.git] / server / lib / friends.js
index 91cd69f8611c34d308207efcae361c0f5e734007..667055d4c554f69cc5b8852d5b88ec92a0811323 100644 (file)
@@ -1,24 +1,22 @@
 'use strict'
 
-const async = require('async')
-const config = require('config')
+const each = require('async/each')
+const eachLimit = require('async/eachLimit')
+const eachSeries = require('async/eachSeries')
 const fs = require('fs')
 const mongoose = require('mongoose')
 const request = require('request')
+const waterfall = require('async/waterfall')
 
 const constants = require('../initializers/constants')
 const logger = require('../helpers/logger')
-const peertubeCrypto = require('../helpers/peertubeCrypto')
-const Pods = require('../models/pods')
-const requestsScheduler = require('../lib/requestsScheduler')
 const requests = require('../helpers/requests')
 
-const http = config.get('webserver.https') ? 'https' : 'http'
-const host = config.get('webserver.host')
-const port = config.get('webserver.port')
+const Pod = mongoose.model('Pod')
+const Request = mongoose.model('Request')
 const Video = mongoose.model('Video')
 
-const pods = {
+const friends = {
   addVideoToFriends: addVideoToFriends,
   hasFriends: hasFriends,
   getMyCertificate: getMyCertificate,
@@ -29,14 +27,11 @@ const pods = {
 }
 
 function addVideoToFriends (video) {
-  // ensure namePath is null
-  video.namePath = null
-
-  requestsScheduler.addRequest('add', video)
+  createRequest('add', video)
 }
 
 function hasFriends (callback) {
-  Pods.count(function (err, count) {
+  Pod.countAll(function (err, count) {
     if (err) return callback(err)
 
     const hasFriends = (count !== 0)
@@ -45,10 +40,10 @@ function hasFriends (callback) {
 }
 
 function getMyCertificate (callback) {
-  fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback)
+  fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
 }
 
-function makeFriends (callback) {
+function makeFriends (urls, callback) {
   const podsScore = {}
 
   logger.info('Make friends!')
@@ -58,9 +53,7 @@ function makeFriends (callback) {
       return callback(err)
     }
 
-    const urls = config.get('network.friends')
-
-    async.eachSeries(urls, function (url, callbackEach) {
+    eachSeries(urls, function (url, callbackEach) {
       computeForeignPodsList(url, podsScore, callbackEach)
     }, function (err) {
       if (err) return callback(err)
@@ -76,13 +69,13 @@ function makeFriends (callback) {
 
 function quitFriends (callback) {
   // Stop pool requests
-  requestsScheduler.deactivate()
+  Request.deactivate()
   // Flush pool requests
-  requestsScheduler.flush()
+  Request.flush()
 
-  async.waterfall([
+  waterfall([
     function getPodsList (callbackAsync) {
-      return Pods.list(callbackAsync)
+      return Pod.list(callbackAsync)
     },
 
     function announceIQuitMyFriends (pods, callbackAsync) {
@@ -95,7 +88,7 @@ function quitFriends (callback) {
       // Announce we quit them
       // We don't care if the request fails
       // The other pod will exclude us automatically after a while
-      async.eachLimit(pods, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
+      eachLimit(pods, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
         requestParams.toPod = pod
         requests.makeSecureRequest(requestParams, callbackEach)
       }, function (err) {
@@ -109,7 +102,7 @@ function quitFriends (callback) {
     },
 
     function removePodsFromDB (callbackAsync) {
-      Pods.removeAll(function (err) {
+      Pod.removeAll(function (err) {
         return callbackAsync(err)
       })
     },
@@ -121,13 +114,13 @@ function quitFriends (callback) {
     },
 
     function removeTheRemoteVideos (videosList, callbackAsync) {
-      async.each(videosList, function (video, callbackEach) {
+      each(videosList, function (video, callbackEach) {
         video.remove(callbackEach)
       }, callbackAsync)
     }
   ], function (err) {
     // Don't forget to re activate the scheduler, even if there was an error
-    requestsScheduler.activate()
+    Request.activate()
 
     if (err) return callback(err)
 
@@ -136,8 +129,8 @@ function quitFriends (callback) {
   })
 }
 
-function removeVideoToFriends (video) {
-  requestsScheduler.addRequest('remove', video)
+function removeVideoToFriends (videoParams) {
+  createRequest('remove', videoParams)
 }
 
 function sendOwnedVideosToPod (podId) {
@@ -155,7 +148,7 @@ function sendOwnedVideosToPod (podId) {
           return
         }
 
-        requestsScheduler.addRequestTo([ podId ], 'add', remoteVideo)
+        createRequest('add', remoteVideo, [ podId ])
       })
     })
   })
@@ -163,7 +156,7 @@ function sendOwnedVideosToPod (podId) {
 
 // ---------------------------------------------------------------------------
 
-module.exports = pods
+module.exports = friends
 
 // ---------------------------------------------------------------------------
 
@@ -211,16 +204,16 @@ function getForeignPodsList (url, callback) {
 
 function makeRequestsToWinningPods (cert, podsList, callback) {
   // Stop pool requests
-  requestsScheduler.deactivate()
+  Request.deactivate()
   // Flush pool requests
-  requestsScheduler.forceSend()
+  Request.forceSend()
 
-  async.eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
+  eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
     const params = {
       url: pod.url + '/api/' + constants.API_VERSION + '/pods/',
       method: 'POST',
       json: {
-        url: http + '://' + host + ':' + port,
+        url: constants.CONFIG.WEBSERVER.URL,
         publicKey: cert
       }
     }
@@ -233,8 +226,12 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
       }
 
       if (res.statusCode === 200) {
-        Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err, podCreated) {
-          if (err) logger.error('Cannot add friend %s pod.', pod.url)
+        const podObj = new Pod({ url: pod.url, publicKey: body.cert })
+        podObj.save(function (err, podCreated) {
+          if (err) {
+            logger.error('Cannot add friend %s pod.', pod.url, { error: err })
+            return callbackEach()
+          }
 
           // Add our videos to the request scheduler
           sendOwnedVideosToPod(podCreated._id)
@@ -249,9 +246,26 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
   }, function endRequests () {
     // Final callback, we've ended all the requests
     // Now we made new friends, we can re activate the pool of requests
-    requestsScheduler.activate()
+    Request.activate()
 
     logger.debug('makeRequestsToWinningPods finished.')
     return callback()
   })
 }
+
+function createRequest (type, data, to) {
+  const req = new Request({
+    request: {
+      type: type,
+      data: data
+    }
+  })
+
+  if (to) {
+    req.to = to
+  }
+
+  req.save(function (err) {
+    if (err) logger.error('Cannot save the request.', { error: err })
+  })
+}