X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Ffriends.js;h=eafffaab01571676072e5eaa19a62c4a163654ba;hb=4b08096b2c58f801e5aad19e38d24ec59dd3e14e;hp=6c4383d8edbcc548d03560c82e20c8c3b533600b;hpb=39f87cb21689a912559d0498641db7d2de4a784d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/friends.js b/server/lib/friends.js index 6c4383d8e..eafffaab0 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -6,6 +6,7 @@ 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') @@ -17,17 +18,17 @@ 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) { - createRequest('add', video) + createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, video) } function hasFriends (callback) { @@ -97,25 +98,13 @@ function quitFriends (callback) { // Don't stop the process } - return callbackAsync() + return callbackAsync(null, pods) }) }, - function removePodsFromDB (callbackAsync) { - Pod.removeAll(function (err) { - return callbackAsync(err) - }) - }, - - 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) { @@ -130,7 +119,7 @@ function quitFriends (callback) { } function removeVideoToFriends (videoParams) { - createRequest('remove', videoParams) + createRequest('remove', constants.REQUEST_ENDPOINTS.VIDEOS, videoParams) } function sendOwnedVideosToPod (podId) { @@ -148,7 +137,7 @@ function sendOwnedVideosToPod (podId) { return } - createRequest('add', remoteVideo, [ podId ]) + createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, remoteVideo, [ podId ]) }) }) }) @@ -185,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 @@ -258,8 +250,9 @@ function makeRequestsToWinningPods (cert, podsList, callback) { }) } -function createRequest (type, data, to) { +function createRequest (type, endpoint, data, to) { const req = new Request({ + endpoint, request: { type: type, data: data @@ -274,3 +267,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 +}