'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/peertube-crypto')
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 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) {
}
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!')
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)
// Flush pool requests
Request.flush()
- async.waterfall([
+ waterfall([
function getPodsList (callbackAsync) {
return Pod.list(callbackAsync)
},
// 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) {
},
function removeTheRemoteVideos (videosList, callbackAsync) {
- async.each(videosList, function (video, callbackEach) {
+ each(videosList, function (video, callbackEach) {
video.remove(callbackEach)
}, callbackAsync)
}
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)
+ }
})
}
// Flush pool requests
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
}
}