aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/friends.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/friends.js')
-rw-r--r--lib/friends.js356
1 files changed, 177 insertions, 179 deletions
diff --git a/lib/friends.js b/lib/friends.js
index c05ccedb1..8cc1a3151 100644
--- a/lib/friends.js
+++ b/lib/friends.js
@@ -1,224 +1,222 @@
1;(function () { 1'use strict'
2 'use strict' 2
3 3var async = require('async')
4 var async = require('async') 4var config = require('config')
5 var config = require('config') 5var fs = require('fs')
6 var fs = require('fs') 6var request = require('request')
7 var request = require('request') 7
8 8var constants = require('../initializers/constants')
9 var constants = require('../initializers/constants') 9var logger = require('../helpers/logger')
10 var logger = require('../helpers/logger') 10var peertubeCrypto = require('../helpers/peertubeCrypto')
11 var peertubeCrypto = require('../helpers/peertubeCrypto') 11var Pods = require('../models/pods')
12 var Pods = require('../models/pods') 12var poolRequests = require('../lib/poolRequests')
13 var poolRequests = require('../lib/poolRequests') 13var requests = require('../helpers/requests')
14 var requests = require('../helpers/requests') 14var Videos = require('../models/videos')
15 var Videos = require('../models/videos') 15
16 16var http = config.get('webserver.https') ? 'https' : 'http'
17 var http = config.get('webserver.https') ? 'https' : 'http' 17var host = config.get('webserver.host')
18 var host = config.get('webserver.host') 18var port = config.get('webserver.port')
19 var port = config.get('webserver.port') 19
20 20var pods = {
21 var pods = { 21 addVideoToFriends: addVideoToFriends,
22 addVideoToFriends: addVideoToFriends, 22 hasFriends: hasFriends,
23 hasFriends: hasFriends, 23 makeFriends: makeFriends,
24 makeFriends: makeFriends, 24 quitFriends: quitFriends,
25 quitFriends: quitFriends, 25 removeVideoToFriends: removeVideoToFriends
26 removeVideoToFriends: removeVideoToFriends 26}
27 } 27
28function addVideoToFriends (video) {
29 // To avoid duplicates
30 var id = video.name + video.magnetUri
31 // ensure namePath is null
32 video.namePath = null
33 poolRequests.addRequest(id, 'add', video)
34}
35
36function hasFriends (callback) {
37 Pods.count(function (err, count) {
38 if (err) return callback(err)
39
40 var has_friends = (count !== 0)
41 callback(null, has_friends)
42 })
43}
44
45function makeFriends (callback) {
46 var pods_score = {}
47
48 logger.info('Make friends!')
49 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
50 if (err) {
51 logger.error('Cannot read public cert.')
52 return callback(err)
53 }
28 54
29 function addVideoToFriends (video) { 55 var urls = config.get('network.friends')
30 // To avoid duplicates
31 var id = video.name + video.magnetUri
32 // ensure namePath is null
33 video.namePath = null
34 poolRequests.addRequest(id, 'add', video)
35 }
36 56
37 function hasFriends (callback) { 57 async.each(urls, computeForeignPodsList, function (err) {
38 Pods.count(function (err, count) {
39 if (err) return callback(err) 58 if (err) return callback(err)
40 59
41 var has_friends = (count !== 0) 60 logger.debug('Pods scores computed.', { pods_score: pods_score })
42 callback(null, has_friends) 61 var pods_list = computeWinningPods(urls, pods_score)
62 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
63
64 makeRequestsToWinningPods(cert, pods_list)
43 }) 65 })
44 } 66 })
45 67
46 function makeFriends (callback) { 68 // -----------------------------------------------------------------------
47 var pods_score = {}
48 69
49 logger.info('Make friends!') 70 function computeForeignPodsList (url, callback) {
50 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { 71 // Let's give 1 point to the pod we ask the friends list
51 if (err) { 72 pods_score[url] = 1
52 logger.error('Cannot read public cert.')
53 return callback(err)
54 }
55 73
56 var urls = config.get('network.friends') 74 getForeignPodsList(url, function (err, foreign_pods_list) {
75 if (err) return callback(err)
76 if (foreign_pods_list.length === 0) return callback()
57 77
58 async.each(urls, computeForeignPodsList, function (err) { 78 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
59 if (err) return callback(err) 79 var foreign_url = foreign_pod.url
60 80
61 logger.debug('Pods scores computed.', { pods_score: pods_score }) 81 if (pods_score[foreign_url]) pods_score[foreign_url]++
62 var pods_list = computeWinningPods(urls, pods_score) 82 else pods_score[foreign_url] = 1
63 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
64 83
65 makeRequestsToWinningPods(cert, pods_list) 84 callback_each()
85 }, function () {
86 callback()
66 }) 87 })
67 }) 88 })
89 }
68 90
69 // ----------------------------------------------------------------------- 91 function computeWinningPods (urls, pods_score) {
70 92 // Build the list of pods to add
71 function computeForeignPodsList (url, callback) { 93 // Only add a pod if it exists in more than a half base pods
72 // Let's give 1 point to the pod we ask the friends list 94 var pods_list = []
73 pods_score[url] = 1 95 var base_score = urls.length / 2
74 96 Object.keys(pods_score).forEach(function (pod) {
75 getForeignPodsList(url, function (err, foreign_pods_list) { 97 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
76 if (err) return callback(err) 98 })
77 if (foreign_pods_list.length === 0) return callback()
78
79 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
80 var foreign_url = foreign_pod.url
81
82 if (pods_score[foreign_url]) pods_score[foreign_url]++
83 else pods_score[foreign_url] = 1
84
85 callback_each()
86 }, function () {
87 callback()
88 })
89 })
90 }
91 99
92 function computeWinningPods (urls, pods_score) { 100 return pods_list
93 // Build the list of pods to add 101 }
94 // Only add a pod if it exists in more than a half base pods
95 var pods_list = []
96 var base_score = urls.length / 2
97 Object.keys(pods_score).forEach(function (pod) {
98 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
99 })
100 102
101 return pods_list 103 function makeRequestsToWinningPods (cert, pods_list) {
102 } 104 // Stop pool requests
105 poolRequests.deactivate()
106 // Flush pool requests
107 poolRequests.forceSend()
103 108
104 function makeRequestsToWinningPods (cert, pods_list) { 109 // Get the list of our videos to send to our new friends
105 // Stop pool requests 110 Videos.listOwned(function (err, videos_list) {
106 poolRequests.deactivate() 111 if (err) {
107 // Flush pool requests 112 logger.error('Cannot get the list of videos we own.')
108 poolRequests.forceSend() 113 return callback(err)
109 114 }
110 // Get the list of our videos to send to our new friends
111 Videos.listOwned(function (err, videos_list) {
112 if (err) {
113 logger.error('Cannot get the list of videos we own.')
114 return callback(err)
115 }
116 115
117 var data = { 116 var data = {
118 url: http + '://' + host + ':' + port, 117 url: http + '://' + host + ':' + port,
119 publicKey: cert, 118 publicKey: cert,
120 videos: videos_list 119 videos: videos_list
121 } 120 }
122 121
123 requests.makeMultipleRetryRequest( 122 requests.makeMultipleRetryRequest(
124 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, 123 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
125 124
126 pods_list, 125 pods_list,
127 126
128 function eachRequest (err, response, body, url, pod, callback_each_request) { 127 function eachRequest (err, response, body, url, pod, callback_each_request) {
129 // We add the pod if it responded correctly with its public certificate 128 // We add the pod if it responded correctly with its public certificate
130 if (!err && response.statusCode === 200) { 129 if (!err && response.statusCode === 200) {
131 Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { 130 Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
132 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err }) 131 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
133 132
134 Videos.addRemotes(body.videos, function (err) { 133 Videos.addRemotes(body.videos, function (err) {
135 if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err }) 134 if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err })
136 135
137 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) 136 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
138 return callback_each_request() 137 return callback_each_request()
139 })
140 }) 138 })
141 } else { 139 })
142 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) 140 } else {
143 return callback_each_request() 141 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
144 } 142 return callback_each_request()
145 },
146
147 function endRequests (err) {
148 // Now we made new friends, we can re activate the pool of requests
149 poolRequests.activate()
150
151 if (err) {
152 logger.error('There was some errors when we wanted to make friends.')
153 return callback(err)
154 }
155
156 logger.debug('makeRequestsToWinningPods finished.')
157 return callback(null)
158 } 143 }
159 ) 144 },
160 })
161 }
162 }
163 145
164 function quitFriends (callback) { 146 function endRequests (err) {
165 // Stop pool requests 147 // Now we made new friends, we can re activate the pool of requests
166 poolRequests.deactivate() 148 poolRequests.activate()
167 // Flush pool requests
168 poolRequests.forceSend()
169 149
170 Pods.list(function (err, pods) { 150 if (err) {
171 if (err) return callback(err) 151 logger.error('There was some errors when we wanted to make friends.')
152 return callback(err)
153 }
172 154
173 var request = { 155 logger.debug('makeRequestsToWinningPods finished.')
174 method: 'POST', 156 return callback(null)
175 path: '/api/' + constants.API_VERSION + '/pods/remove',
176 sign: true,
177 encrypt: true,
178 data: {
179 url: 'me' // Fake data
180 } 157 }
158 )
159 })
160 }
161}
162
163function quitFriends (callback) {
164 // Stop pool requests
165 poolRequests.deactivate()
166 // Flush pool requests
167 poolRequests.forceSend()
168
169 Pods.list(function (err, pods) {
170 if (err) return callback(err)
171
172 var request = {
173 method: 'POST',
174 path: '/api/' + constants.API_VERSION + '/pods/remove',
175 sign: true,
176 encrypt: true,
177 data: {
178 url: 'me' // Fake data
181 } 179 }
180 }
182 181
183 // Announce we quit them 182 // Announce we quit them
184 requests.makeMultipleRetryRequest(request, pods, function () { 183 requests.makeMultipleRetryRequest(request, pods, function () {
185 Pods.removeAll(function (err) { 184 Pods.removeAll(function (err) {
186 poolRequests.activate() 185 poolRequests.activate()
187 186
188 if (err) return callback(err) 187 if (err) return callback(err)
189 188
190 logger.info('Broke friends, so sad :(') 189 logger.info('Broke friends, so sad :(')
191 190
192 Videos.removeAllRemotes(function (err) { 191 Videos.removeAllRemotes(function (err) {
193 if (err) return callback(err) 192 if (err) return callback(err)
194 193
195 logger.info('Removed all remote videos.') 194 logger.info('Removed all remote videos.')
196 callback(null) 195 callback(null)
197 })
198 }) 196 })
199 }) 197 })
200 }) 198 })
201 } 199 })
200}
202 201
203 function removeVideoToFriends (video) { 202function removeVideoToFriends (video) {
204 // To avoid duplicates 203 // To avoid duplicates
205 var id = video.name + video.magnetUri 204 var id = video.name + video.magnetUri
206 poolRequests.addRequest(id, 'remove', video) 205 poolRequests.addRequest(id, 'remove', video)
207 } 206}
208 207
209 // --------------------------------------------------------------------------- 208// ---------------------------------------------------------------------------
210 209
211 module.exports = pods 210module.exports = pods
212 211
213 // --------------------------------------------------------------------------- 212// ---------------------------------------------------------------------------
214 213
215 function getForeignPodsList (url, callback) { 214function getForeignPodsList (url, callback) {
216 var path = '/api/' + constants.API_VERSION + '/pods' 215 var path = '/api/' + constants.API_VERSION + '/pods'
217 216
218 request.get(url + path, function (err, response, body) { 217 request.get(url + path, function (err, response, body) {
219 if (err) return callback(err) 218 if (err) return callback(err)
220 219
221 callback(null, JSON.parse(body)) 220 callback(null, JSON.parse(body))
222 }) 221 })
223 } 222}
224})()