diff options
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r-- | server/lib/friends.js | 34 |
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 | ||
3 | const config = require('config') | ||
4 | const each = require('async/each') | 3 | const each = require('async/each') |
5 | const eachLimit = require('async/eachLimit') | 4 | const eachLimit = require('async/eachLimit') |
6 | const eachSeries = require('async/eachSeries') | 5 | const eachSeries = require('async/eachSeries') |
@@ -11,24 +10,20 @@ const waterfall = require('async/waterfall') | |||
11 | 10 | ||
12 | const constants = require('../initializers/constants') | 11 | const constants = require('../initializers/constants') |
13 | const logger = require('../helpers/logger') | 12 | const logger = require('../helpers/logger') |
14 | const peertubeCrypto = require('../helpers/peertube-crypto') | ||
15 | const requests = require('../helpers/requests') | 13 | const requests = require('../helpers/requests') |
16 | 14 | ||
17 | const http = config.get('webserver.https') ? 'https' : 'http' | ||
18 | const host = config.get('webserver.host') | ||
19 | const port = config.get('webserver.port') | ||
20 | const Pod = mongoose.model('Pod') | 15 | const Pod = mongoose.model('Pod') |
21 | const Request = mongoose.model('Request') | 16 | const Request = mongoose.model('Request') |
22 | const Video = mongoose.model('Video') | 17 | const Video = mongoose.model('Video') |
23 | 18 | ||
24 | const friends = { | 19 | const 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 | ||
34 | function addVideoToFriends (video) { | 29 | function addVideoToFriends (video) { |
@@ -45,10 +40,10 @@ function hasFriends (callback) { | |||
45 | } | 40 | } |
46 | 41 | ||
47 | function getMyCertificate (callback) { | 42 | function 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 | ||
51 | function makeFriends (callback) { | 46 | function 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 | } |