]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/friends.js
Server: host -> hostname (host = hostname + port)
[github/Chocobozzz/PeerTube.git] / server / lib / friends.js
index 6a2c37fd724b5f081ca43e9e8b81500e9eeacdee..3f100545c28796912cac22877cea0895c54381bb 100644 (file)
@@ -1,12 +1,12 @@
 'use strict'
 
-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 urlUtil = require('url')
 const waterfall = require('async/waterfall')
 
 const constants = require('../initializers/constants')
@@ -18,13 +18,13 @@ const Request = mongoose.model('Request')
 const Video = mongoose.model('Video')
 
 const friends = {
-  addVideoToFriends: addVideoToFriends,
-  hasFriends: hasFriends,
-  getMyCertificate: getMyCertificate,
-  makeFriends: makeFriends,
-  quitFriends: quitFriends,
-  removeVideoToFriends: removeVideoToFriends,
-  sendOwnedVideosToPod: sendOwnedVideosToPod
+  addVideoToFriends,
+  hasFriends,
+  getMyCertificate,
+  makeFriends,
+  quitFriends,
+  removeVideoToFriends,
+  sendOwnedVideosToPod
 }
 
 function addVideoToFriends (video) {
@@ -44,7 +44,7 @@ function getMyCertificate (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!')
@@ -54,8 +54,6 @@ function makeFriends (callback) {
       return callback(err)
     }
 
-    const urls = config.get('network.friends')
-
     eachSeries(urls, function (url, callbackEach) {
       computeForeignPodsList(url, podsScore, callbackEach)
     }, function (err) {
@@ -100,25 +98,13 @@ function quitFriends (callback) {
           // Don't stop the process
         }
 
-        return callbackAsync()
-      })
-    },
-
-    function removePodsFromDB (callbackAsync) {
-      Pod.removeAll(function (err) {
-        return callbackAsync(err)
+        return callbackAsync(null, pods)
       })
     },
 
-    function listRemoteVideos (callbackAsync) {
-      logger.info('Broke friends, so sad :(')
-
-      Video.listRemotes(callbackAsync)
-    },
-
-    function removeTheRemoteVideos (videosList, callbackAsync) {
-      each(videosList, function (video, callbackEach) {
-        video.remove(callbackEach)
+    function removePodsFromDB (pods, callbackAsync) {
+      each(pods, function (pod, callbackEach) {
+        pod.remove(callbackEach)
       }, callbackAsync)
     }
   ], function (err) {
@@ -188,8 +174,11 @@ function computeWinningPods (urls, podsScore) {
   // Only add a pod if it exists in more than a half base pods
   const podsList = []
   const baseScore = urls.length / 2
-  Object.keys(podsScore).forEach(function (pod) {
-    if (podsScore[pod] > baseScore) podsList.push({ url: pod })
+  Object.keys(podsScore).forEach(function (podUrl) {
+    // If the pod is not me and with a good score we add it
+    if (isMe(podUrl) === false && podsScore[podUrl] > baseScore) {
+      podsList.push({ url: podUrl })
+    }
   })
 
   return podsList
@@ -201,7 +190,12 @@ function getForeignPodsList (url, callback) {
   request.get(url + path, function (err, response, body) {
     if (err) return callback(err)
 
-    callback(null, JSON.parse(body))
+    try {
+      const json = JSON.parse(body)
+      return callback(null, json)
+    } catch (err) {
+      return callback(err)
+    }
   })
 }
 
@@ -272,3 +266,15 @@ function createRequest (type, data, to) {
     if (err) logger.error('Cannot save the request.', { error: err })
   })
 }
+
+function isMe (url) {
+  const parsedUrl = urlUtil.parse(url)
+
+  const hostname = parsedUrl.hostname
+  const port = parseInt(parsedUrl.port)
+
+  const myHostname = constants.CONFIG.WEBSERVER.HOSTNAME
+  const myPort = constants.CONFIG.WEBSERVER.PORT
+
+  return hostname === myHostname && port === myPort
+}