aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/friends.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r--server/lib/friends.js42
1 files changed, 28 insertions, 14 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 91cd69f86..617cc1ab4 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -10,12 +10,12 @@ const constants = require('../initializers/constants')
10const logger = require('../helpers/logger') 10const logger = require('../helpers/logger')
11const peertubeCrypto = require('../helpers/peertubeCrypto') 11const peertubeCrypto = require('../helpers/peertubeCrypto')
12const Pods = require('../models/pods') 12const Pods = require('../models/pods')
13const requestsScheduler = require('../lib/requestsScheduler')
14const requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
15 14
16const http = config.get('webserver.https') ? 'https' : 'http' 15const http = config.get('webserver.https') ? 'https' : 'http'
17const host = config.get('webserver.host') 16const host = config.get('webserver.host')
18const port = config.get('webserver.port') 17const port = config.get('webserver.port')
18const Request = mongoose.model('Request')
19const Video = mongoose.model('Video') 19const Video = mongoose.model('Video')
20 20
21const pods = { 21const pods = {
@@ -29,10 +29,7 @@ const pods = {
29} 29}
30 30
31function addVideoToFriends (video) { 31function addVideoToFriends (video) {
32 // ensure namePath is null 32 createRequest('add', video)
33 video.namePath = null
34
35 requestsScheduler.addRequest('add', video)
36} 33}
37 34
38function hasFriends (callback) { 35function hasFriends (callback) {
@@ -76,9 +73,9 @@ function makeFriends (callback) {
76 73
77function quitFriends (callback) { 74function quitFriends (callback) {
78 // Stop pool requests 75 // Stop pool requests
79 requestsScheduler.deactivate() 76 Request.deactivate()
80 // Flush pool requests 77 // Flush pool requests
81 requestsScheduler.flush() 78 Request.flush()
82 79
83 async.waterfall([ 80 async.waterfall([
84 function getPodsList (callbackAsync) { 81 function getPodsList (callbackAsync) {
@@ -127,7 +124,7 @@ function quitFriends (callback) {
127 } 124 }
128 ], function (err) { 125 ], function (err) {
129 // Don't forget to re activate the scheduler, even if there was an error 126 // Don't forget to re activate the scheduler, even if there was an error
130 requestsScheduler.activate() 127 Request.activate()
131 128
132 if (err) return callback(err) 129 if (err) return callback(err)
133 130
@@ -136,8 +133,8 @@ function quitFriends (callback) {
136 }) 133 })
137} 134}
138 135
139function removeVideoToFriends (video) { 136function removeVideoToFriends (videoParams) {
140 requestsScheduler.addRequest('remove', video) 137 createRequest('remove', videoParams)
141} 138}
142 139
143function sendOwnedVideosToPod (podId) { 140function sendOwnedVideosToPod (podId) {
@@ -155,7 +152,7 @@ function sendOwnedVideosToPod (podId) {
155 return 152 return
156 } 153 }
157 154
158 requestsScheduler.addRequestTo([ podId ], 'add', remoteVideo) 155 createRequest('add', remoteVideo, [ podId ])
159 }) 156 })
160 }) 157 })
161 }) 158 })
@@ -211,9 +208,9 @@ function getForeignPodsList (url, callback) {
211 208
212function makeRequestsToWinningPods (cert, podsList, callback) { 209function makeRequestsToWinningPods (cert, podsList, callback) {
213 // Stop pool requests 210 // Stop pool requests
214 requestsScheduler.deactivate() 211 Request.deactivate()
215 // Flush pool requests 212 // Flush pool requests
216 requestsScheduler.forceSend() 213 Request.forceSend()
217 214
218 async.eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) { 215 async.eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
219 const params = { 216 const params = {
@@ -249,9 +246,26 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
249 }, function endRequests () { 246 }, function endRequests () {
250 // Final callback, we've ended all the requests 247 // Final callback, we've ended all the requests
251 // Now we made new friends, we can re activate the pool of requests 248 // Now we made new friends, we can re activate the pool of requests
252 requestsScheduler.activate() 249 Request.activate()
253 250
254 logger.debug('makeRequestsToWinningPods finished.') 251 logger.debug('makeRequestsToWinningPods finished.')
255 return callback() 252 return callback()
256 }) 253 })
257} 254}
255
256function createRequest (type, data, to) {
257 const req = new Request({
258 request: {
259 type: type,
260 data: data
261 }
262 })
263
264 if (to) {
265 req.to = to
266 }
267
268 req.save(function (err) {
269 if (err) logger.error('Cannot save the request.', { error: err })
270 })
271}