aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/friends.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
commit99fe265a5fc077cb66c322e7f3d191ff7110aea0 (patch)
treec9e04ccfcc5496d2300d7c26db5833e494b4cdad /server/lib/friends.js
parentfcc5f77b95d330bfcb439c172b7fcc58f3162e4d (diff)
parent91cc839af88730ba55f84997c56b85ea100070a7 (diff)
downloadPeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.gz
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.zst
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.zip
Merge branch 'postgresql'
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r--server/lib/friends.js145
1 files changed, 109 insertions, 36 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index eaea040ca..f0575ff2f 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -4,20 +4,18 @@ const each = require('async/each')
4const eachLimit = require('async/eachLimit') 4const eachLimit = require('async/eachLimit')
5const eachSeries = require('async/eachSeries') 5const eachSeries = require('async/eachSeries')
6const fs = require('fs') 6const fs = require('fs')
7const mongoose = require('mongoose')
8const request = require('request') 7const request = require('request')
9const waterfall = require('async/waterfall') 8const waterfall = require('async/waterfall')
10 9
11const constants = require('../initializers/constants') 10const constants = require('../initializers/constants')
11const db = require('../initializers/database')
12const logger = require('../helpers/logger') 12const logger = require('../helpers/logger')
13const requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
14 14
15const Pod = mongoose.model('Pod')
16const Request = mongoose.model('Request')
17const Video = mongoose.model('Video')
18
19const friends = { 15const friends = {
20 addVideoToFriends, 16 addVideoToFriends,
17 updateVideoToFriends,
18 reportAbuseVideoToFriend,
21 hasFriends, 19 hasFriends,
22 getMyCertificate, 20 getMyCertificate,
23 makeFriends, 21 makeFriends,
@@ -26,12 +24,47 @@ const friends = {
26 sendOwnedVideosToPod 24 sendOwnedVideosToPod
27} 25}
28 26
29function addVideoToFriends (video) { 27function addVideoToFriends (videoData, transaction, callback) {
30 createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, video) 28 const options = {
29 type: 'add',
30 endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
31 data: videoData,
32 transaction
33 }
34 createRequest(options, callback)
35}
36
37function updateVideoToFriends (videoData, transaction, callback) {
38 const options = {
39 type: 'update',
40 endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
41 data: videoData,
42 transaction
43 }
44 createRequest(options, callback)
45}
46
47function removeVideoToFriends (videoParams) {
48 const options = {
49 type: 'remove',
50 endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
51 data: videoParams
52 }
53 createRequest(options)
54}
55
56function reportAbuseVideoToFriend (reportData, video) {
57 const options = {
58 type: 'report-abuse',
59 endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
60 data: reportData,
61 toIds: [ video.Author.podId ]
62 }
63 createRequest(options)
31} 64}
32 65
33function hasFriends (callback) { 66function hasFriends (callback) {
34 Pod.countAll(function (err, count) { 67 db.Pod.countAll(function (err, count) {
35 if (err) return callback(err) 68 if (err) return callback(err)
36 69
37 const hasFriends = (count !== 0) 70 const hasFriends = (count !== 0)
@@ -69,13 +102,15 @@ function makeFriends (hosts, callback) {
69 102
70function quitFriends (callback) { 103function quitFriends (callback) {
71 // Stop pool requests 104 // Stop pool requests
72 Request.deactivate() 105 db.Request.deactivate()
73 // Flush pool requests
74 Request.flush()
75 106
76 waterfall([ 107 waterfall([
108 function flushRequests (callbackAsync) {
109 db.Request.flush(callbackAsync)
110 },
111
77 function getPodsList (callbackAsync) { 112 function getPodsList (callbackAsync) {
78 return Pod.list(callbackAsync) 113 return db.Pod.list(callbackAsync)
79 }, 114 },
80 115
81 function announceIQuitMyFriends (pods, callbackAsync) { 116 function announceIQuitMyFriends (pods, callbackAsync) {
@@ -103,12 +138,12 @@ function quitFriends (callback) {
103 138
104 function removePodsFromDB (pods, callbackAsync) { 139 function removePodsFromDB (pods, callbackAsync) {
105 each(pods, function (pod, callbackEach) { 140 each(pods, function (pod, callbackEach) {
106 pod.remove(callbackEach) 141 pod.destroy().asCallback(callbackEach)
107 }, callbackAsync) 142 }, callbackAsync)
108 } 143 }
109 ], function (err) { 144 ], function (err) {
110 // Don't forget to re activate the scheduler, even if there was an error 145 // Don't forget to re activate the scheduler, even if there was an error
111 Request.activate() 146 db.Request.activate()
112 147
113 if (err) return callback(err) 148 if (err) return callback(err)
114 149
@@ -117,26 +152,28 @@ function quitFriends (callback) {
117 }) 152 })
118} 153}
119 154
120function removeVideoToFriends (videoParams) {
121 createRequest('remove', constants.REQUEST_ENDPOINTS.VIDEOS, videoParams)
122}
123
124function sendOwnedVideosToPod (podId) { 155function sendOwnedVideosToPod (podId) {
125 Video.listOwned(function (err, videosList) { 156 db.Video.listOwnedAndPopulateAuthorAndTags(function (err, videosList) {
126 if (err) { 157 if (err) {
127 logger.error('Cannot get the list of videos we own.') 158 logger.error('Cannot get the list of videos we own.')
128 return 159 return
129 } 160 }
130 161
131 videosList.forEach(function (video) { 162 videosList.forEach(function (video) {
132 video.toRemoteJSON(function (err, remoteVideo) { 163 video.toAddRemoteJSON(function (err, remoteVideo) {
133 if (err) { 164 if (err) {
134 logger.error('Cannot convert video to remote.', { error: err }) 165 logger.error('Cannot convert video to remote.', { error: err })
135 // Don't break the process 166 // Don't break the process
136 return 167 return
137 } 168 }
138 169
139 createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, remoteVideo, [ podId ]) 170 const options = {
171 type: 'add',
172 endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
173 data: remoteVideo,
174 toIds: [ podId ]
175 }
176 createRequest(options)
140 }) 177 })
141 }) 178 })
142 }) 179 })
@@ -149,10 +186,10 @@ module.exports = friends
149// --------------------------------------------------------------------------- 186// ---------------------------------------------------------------------------
150 187
151function computeForeignPodsList (host, podsScore, callback) { 188function computeForeignPodsList (host, podsScore, callback) {
152 getForeignPodsList(host, function (err, foreignPodsList) { 189 getForeignPodsList(host, function (err, res) {
153 if (err) return callback(err) 190 if (err) return callback(err)
154 191
155 if (!foreignPodsList) foreignPodsList = [] 192 const foreignPodsList = res.data
156 193
157 // Let's give 1 point to the pod we ask the friends list 194 // Let's give 1 point to the pod we ask the friends list
158 foreignPodsList.push({ host }) 195 foreignPodsList.push({ host })
@@ -200,9 +237,9 @@ function getForeignPodsList (host, callback) {
200 237
201function makeRequestsToWinningPods (cert, podsList, callback) { 238function makeRequestsToWinningPods (cert, podsList, callback) {
202 // Stop pool requests 239 // Stop pool requests
203 Request.deactivate() 240 db.Request.deactivate()
204 // Flush pool requests 241 // Flush pool requests
205 Request.forceSend() 242 db.Request.forceSend()
206 243
207 eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) { 244 eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
208 const params = { 245 const params = {
@@ -222,15 +259,15 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
222 } 259 }
223 260
224 if (res.statusCode === 200) { 261 if (res.statusCode === 200) {
225 const podObj = new Pod({ host: pod.host, publicKey: body.cert }) 262 const podObj = db.Pod.build({ host: pod.host, publicKey: body.cert })
226 podObj.save(function (err, podCreated) { 263 podObj.save().asCallback(function (err, podCreated) {
227 if (err) { 264 if (err) {
228 logger.error('Cannot add friend %s pod.', pod.host, { error: err }) 265 logger.error('Cannot add friend %s pod.', pod.host, { error: err })
229 return callbackEach() 266 return callbackEach()
230 } 267 }
231 268
232 // Add our videos to the request scheduler 269 // Add our videos to the request scheduler
233 sendOwnedVideosToPod(podCreated._id) 270 sendOwnedVideosToPod(podCreated.id)
234 271
235 return callbackEach() 272 return callbackEach()
236 }) 273 })
@@ -242,28 +279,64 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
242 }, function endRequests () { 279 }, function endRequests () {
243 // Final callback, we've ended all the requests 280 // Final callback, we've ended all the requests
244 // Now we made new friends, we can re activate the pool of requests 281 // Now we made new friends, we can re activate the pool of requests
245 Request.activate() 282 db.Request.activate()
246 283
247 logger.debug('makeRequestsToWinningPods finished.') 284 logger.debug('makeRequestsToWinningPods finished.')
248 return callback() 285 return callback()
249 }) 286 })
250} 287}
251 288
252function createRequest (type, endpoint, data, to) { 289// Wrapper that populate "toIds" argument with all our friends if it is not specified
253 const req = new Request({ 290// { type, endpoint, data, toIds, transaction }
291function createRequest (options, callback) {
292 if (!callback) callback = function () {}
293 if (options.toIds) return _createRequest(options, callback)
294
295 // If the "toIds" pods is not specified, we send the request to all our friends
296 db.Pod.listAllIds(options.transaction, function (err, podIds) {
297 if (err) {
298 logger.error('Cannot get pod ids', { error: err })
299 return
300 }
301
302 const newOptions = Object.assign(options, { toIds: podIds })
303 return _createRequest(newOptions, callback)
304 })
305}
306
307// { type, endpoint, data, toIds, transaction }
308function _createRequest (options, callback) {
309 const type = options.type
310 const endpoint = options.endpoint
311 const data = options.data
312 const toIds = options.toIds
313 const transaction = options.transaction
314
315 const pods = []
316
317 // If there are no destination pods abort
318 if (toIds.length === 0) return callback(null)
319
320 toIds.forEach(function (toPod) {
321 pods.push(db.Pod.build({ id: toPod }))
322 })
323
324 const createQuery = {
254 endpoint, 325 endpoint,
255 request: { 326 request: {
256 type: type, 327 type: type,
257 data: data 328 data: data
258 } 329 }
259 }) 330 }
260 331
261 if (to) { 332 const dbRequestOptions = {
262 req.to = to 333 transaction
263 } 334 }
264 335
265 req.save(function (err) { 336 return db.Request.create(createQuery, dbRequestOptions).asCallback(function (err, request) {
266 if (err) logger.error('Cannot save the request.', { error: err }) 337 if (err) return callback(err)
338
339 return request.setPods(pods, dbRequestOptions).asCallback(callback)
267 }) 340 })
268} 341}
269 342