aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/friends.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r--server/lib/friends.js34
1 files changed, 16 insertions, 18 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 6e1516b94..556d2e773 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -1,6 +1,5 @@
1'use strict' 1'use strict'
2 2
3const config = require('config')
4const each = require('async/each') 3const each = require('async/each')
5const eachLimit = require('async/eachLimit') 4const eachLimit = require('async/eachLimit')
6const eachSeries = require('async/eachSeries') 5const eachSeries = require('async/eachSeries')
@@ -11,24 +10,20 @@ const waterfall = require('async/waterfall')
11 10
12const constants = require('../initializers/constants') 11const constants = require('../initializers/constants')
13const logger = require('../helpers/logger') 12const logger = require('../helpers/logger')
14const peertubeCrypto = require('../helpers/peertube-crypto')
15const requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
16 14
17const http = config.get('webserver.https') ? 'https' : 'http'
18const host = config.get('webserver.host')
19const port = config.get('webserver.port')
20const Pod = mongoose.model('Pod') 15const Pod = mongoose.model('Pod')
21const Request = mongoose.model('Request') 16const Request = mongoose.model('Request')
22const Video = mongoose.model('Video') 17const Video = mongoose.model('Video')
23 18
24const friends = { 19const friends = {
25 addVideoToFriends: addVideoToFriends, 20 addVideoToFriends,
26 hasFriends: hasFriends, 21 hasFriends,
27 getMyCertificate: getMyCertificate, 22 getMyCertificate,
28 makeFriends: makeFriends, 23 makeFriends,
29 quitFriends: quitFriends, 24 quitFriends,
30 removeVideoToFriends: removeVideoToFriends, 25 removeVideoToFriends,
31 sendOwnedVideosToPod: sendOwnedVideosToPod 26 sendOwnedVideosToPod
32} 27}
33 28
34function addVideoToFriends (video) { 29function addVideoToFriends (video) {
@@ -45,10 +40,10 @@ function hasFriends (callback) {
45} 40}
46 41
47function getMyCertificate (callback) { 42function getMyCertificate (callback) {
48 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback) 43 fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
49} 44}
50 45
51function makeFriends (callback) { 46function makeFriends (urls, callback) {
52 const podsScore = {} 47 const podsScore = {}
53 48
54 logger.info('Make friends!') 49 logger.info('Make friends!')
@@ -58,8 +53,6 @@ function makeFriends (callback) {
58 return callback(err) 53 return callback(err)
59 } 54 }
60 55
61 const urls = config.get('network.friends')
62
63 eachSeries(urls, function (url, callbackEach) { 56 eachSeries(urls, function (url, callbackEach) {
64 computeForeignPodsList(url, podsScore, callbackEach) 57 computeForeignPodsList(url, podsScore, callbackEach)
65 }, function (err) { 58 }, function (err) {
@@ -205,7 +198,12 @@ function getForeignPodsList (url, callback) {
205 request.get(url + path, function (err, response, body) { 198 request.get(url + path, function (err, response, body) {
206 if (err) return callback(err) 199 if (err) return callback(err)
207 200
208 callback(null, JSON.parse(body)) 201 try {
202 const json = JSON.parse(body)
203 return callback(null, json)
204 } catch (err) {
205 return callback(err)
206 }
209 }) 207 })
210} 208}
211 209
@@ -220,7 +218,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
220 url: pod.url + '/api/' + constants.API_VERSION + '/pods/', 218 url: pod.url + '/api/' + constants.API_VERSION + '/pods/',
221 method: 'POST', 219 method: 'POST',
222 json: { 220 json: {
223 url: http + '://' + host + ':' + port, 221 url: constants.CONFIG.WEBSERVER.URL,
224 publicKey: cert 222 publicKey: cert
225 } 223 }
226 } 224 }