3 const each = require('async/each')
4 const eachLimit = require('async/eachLimit')
5 const eachSeries = require('async/eachSeries')
6 const fs = require('fs')
7 const mongoose = require('mongoose')
8 const request = require('request')
9 const waterfall = require('async/waterfall')
11 const constants = require('../initializers/constants')
12 const logger = require('../helpers/logger')
13 const requests = require('../helpers/requests')
15 const Pod = mongoose.model('Pod')
16 const Request = mongoose.model('Request')
17 const Video = mongoose.model('Video')
29 function addVideoToFriends (video) {
30 createRequest('add', video)
33 function hasFriends (callback) {
34 Pod.countAll(function (err, count) {
35 if (err) return callback(err)
37 const hasFriends = (count !== 0)
38 callback(null, hasFriends)
42 function getMyCertificate (callback) {
43 fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
46 function makeFriends (urls, callback) {
49 logger.info('Make friends!')
50 getMyCertificate(function (err, cert) {
52 logger.error('Cannot read public cert.')
56 eachSeries(urls, function (url, callbackEach) {
57 computeForeignPodsList(url, podsScore, callbackEach)
59 if (err) return callback(err)
61 logger.debug('Pods scores computed.', { podsScore: podsScore })
62 const podsList = computeWinningPods(urls, podsScore)
63 logger.debug('Pods that we keep.', { podsToKeep: podsList })
65 makeRequestsToWinningPods(cert, podsList, callback)
70 function quitFriends (callback) {
73 // Flush pool requests
77 function getPodsList (callbackAsync) {
78 return Pod.list(callbackAsync)
81 function announceIQuitMyFriends (pods, callbackAsync) {
82 const requestParams = {
84 path: '/api/' + constants.API_VERSION + '/pods/remove',
88 // Announce we quit them
89 // We don't care if the request fails
90 // The other pod will exclude us automatically after a while
91 eachLimit(pods, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
92 requestParams.toPod = pod
93 requests.makeSecureRequest(requestParams, callbackEach)
96 logger.error('Some errors while quitting friends.', { err: err })
97 // Don't stop the process
100 return callbackAsync(null, pods)
104 function removePodsFromDB (pods, callbackAsync) {
105 each(pods, function (pod, callbackEach) {
106 pod.remove(callbackEach)
110 // Don't forget to re activate the scheduler, even if there was an error
113 if (err) return callback(err)
115 logger.info('Removed all remote videos.')
116 return callback(null)
120 function removeVideoToFriends (videoParams) {
121 createRequest('remove', videoParams)
124 function sendOwnedVideosToPod (podId) {
125 Video.listOwned(function (err, videosList) {
127 logger.error('Cannot get the list of videos we own.')
131 videosList.forEach(function (video) {
132 video.toRemoteJSON(function (err, remoteVideo) {
134 logger.error('Cannot convert video to remote.', { error: err })
135 // Don't break the process
139 createRequest('add', remoteVideo, [ podId ])
145 // ---------------------------------------------------------------------------
147 module.exports = friends
149 // ---------------------------------------------------------------------------
151 function computeForeignPodsList (url, podsScore, callback) {
152 getForeignPodsList(url, function (err, foreignPodsList) {
153 if (err) return callback(err)
155 if (!foreignPodsList) foreignPodsList = []
157 // Let's give 1 point to the pod we ask the friends list
158 foreignPodsList.push({ url: url })
160 foreignPodsList.forEach(function (foreignPod) {
161 const foreignPodUrl = foreignPod.url
163 if (podsScore[foreignPodUrl]) podsScore[foreignPodUrl]++
164 else podsScore[foreignPodUrl] = 1
171 function computeWinningPods (urls, podsScore) {
172 // Build the list of pods to add
173 // Only add a pod if it exists in more than a half base pods
175 const baseScore = urls.length / 2
176 Object.keys(podsScore).forEach(function (pod) {
177 if (podsScore[pod] > baseScore) podsList.push({ url: pod })
183 function getForeignPodsList (url, callback) {
184 const path = '/api/' + constants.API_VERSION + '/pods'
186 request.get(url + path, function (err, response, body) {
187 if (err) return callback(err)
190 const json = JSON.parse(body)
191 return callback(null, json)
198 function makeRequestsToWinningPods (cert, podsList, callback) {
199 // Stop pool requests
201 // Flush pool requests
204 eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
206 url: pod.url + '/api/' + constants.API_VERSION + '/pods/',
209 url: constants.CONFIG.WEBSERVER.URL,
214 requests.makeRetryRequest(params, function (err, res, body) {
216 logger.error('Error with adding %s pod.', pod.url, { error: err })
217 // Don't break the process
218 return callbackEach()
221 if (res.statusCode === 200) {
222 const podObj = new Pod({ url: pod.url, publicKey: body.cert })
223 podObj.save(function (err, podCreated) {
225 logger.error('Cannot add friend %s pod.', pod.url, { error: err })
226 return callbackEach()
229 // Add our videos to the request scheduler
230 sendOwnedVideosToPod(podCreated._id)
232 return callbackEach()
235 logger.error('Status not 200 for %s pod.', pod.url)
236 return callbackEach()
239 }, function endRequests () {
240 // Final callback, we've ended all the requests
241 // Now we made new friends, we can re activate the pool of requests
244 logger.debug('makeRequestsToWinningPods finished.')
249 function createRequest (type, data, to) {
250 const req = new Request({
261 req.save(function (err) {
262 if (err) logger.error('Cannot save the request.', { error: err })