}
function addVideoToFriends (video) {
- createRequest('add', video)
+ createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, video)
}
function hasFriends (callback) {
fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
}
-function makeFriends (urls, callback) {
+function makeFriends (hosts, callback) {
const podsScore = {}
logger.info('Make friends!')
return callback(err)
}
- eachSeries(urls, function (url, callbackEach) {
- computeForeignPodsList(url, podsScore, callbackEach)
+ eachSeries(hosts, function (host, callbackEach) {
+ computeForeignPodsList(host, podsScore, callbackEach)
}, function (err) {
if (err) return callback(err)
logger.debug('Pods scores computed.', { podsScore: podsScore })
- const podsList = computeWinningPods(urls, podsScore)
+ const podsList = computeWinningPods(hosts, podsScore)
logger.debug('Pods that we keep.', { podsToKeep: podsList })
makeRequestsToWinningPods(cert, podsList, callback)
}
function removeVideoToFriends (videoParams) {
- createRequest('remove', videoParams)
+ createRequest('remove', constants.REQUEST_ENDPOINTS.VIDEOS, videoParams)
}
function sendOwnedVideosToPod (podId) {
return
}
- createRequest('add', remoteVideo, [ podId ])
+ createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, remoteVideo, [ podId ])
})
})
})
// ---------------------------------------------------------------------------
-function computeForeignPodsList (url, podsScore, callback) {
- getForeignPodsList(url, function (err, foreignPodsList) {
+function computeForeignPodsList (host, podsScore, callback) {
+ getForeignPodsList(host, function (err, foreignPodsList) {
if (err) return callback(err)
if (!foreignPodsList) foreignPodsList = []
// Let's give 1 point to the pod we ask the friends list
- foreignPodsList.push({ url: url })
+ foreignPodsList.push({ host })
foreignPodsList.forEach(function (foreignPod) {
- const foreignPodUrl = foreignPod.url
+ const foreignPodHost = foreignPod.host
- if (podsScore[foreignPodUrl]) podsScore[foreignPodUrl]++
- else podsScore[foreignPodUrl] = 1
+ if (podsScore[foreignPodHost]) podsScore[foreignPodHost]++
+ else podsScore[foreignPodHost] = 1
})
callback()
})
}
-function computeWinningPods (urls, podsScore) {
+function computeWinningPods (hosts, podsScore) {
// Build the list of pods to add
// 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 })
+ const baseScore = hosts.length / 2
+ Object.keys(podsScore).forEach(function (podHost) {
+ // If the pod is not me and with a good score we add it
+ if (isMe(podHost) === false && podsScore[podHost] > baseScore) {
+ podsList.push({ host: podHost })
+ }
})
return podsList
}
-function getForeignPodsList (url, callback) {
+function getForeignPodsList (host, callback) {
const path = '/api/' + constants.API_VERSION + '/pods'
- request.get(url + path, function (err, response, body) {
+ request.get(constants.REMOTE_SCHEME.HTTP + '://' + host + path, function (err, response, body) {
if (err) return callback(err)
try {
eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
const params = {
- url: pod.url + '/api/' + constants.API_VERSION + '/pods/',
+ url: constants.REMOTE_SCHEME.HTTP + '://' + pod.host + '/api/' + constants.API_VERSION + '/pods/',
method: 'POST',
json: {
- url: constants.CONFIG.WEBSERVER.URL,
+ host: constants.CONFIG.WEBSERVER.HOST,
publicKey: cert
}
}
requests.makeRetryRequest(params, function (err, res, body) {
if (err) {
- logger.error('Error with adding %s pod.', pod.url, { error: err })
+ logger.error('Error with adding %s pod.', pod.host, { error: err })
// Don't break the process
return callbackEach()
}
if (res.statusCode === 200) {
- const podObj = new Pod({ url: pod.url, publicKey: body.cert })
+ const podObj = new Pod({ host: pod.host, publicKey: body.cert })
podObj.save(function (err, podCreated) {
if (err) {
- logger.error('Cannot add friend %s pod.', pod.url, { error: err })
+ logger.error('Cannot add friend %s pod.', pod.host, { error: err })
return callbackEach()
}
return callbackEach()
})
} else {
- logger.error('Status not 200 for %s pod.', pod.url)
+ logger.error('Status not 200 for %s pod.', pod.host)
return callbackEach()
}
})
})
}
-function createRequest (type, data, to) {
+function createRequest (type, endpoint, data, to) {
const req = new Request({
+ endpoint,
request: {
type: type,
data: data
if (err) logger.error('Cannot save the request.', { error: err })
})
}
+
+function isMe (host) {
+ return host === constants.CONFIG.WEBSERVER.HOST
+}