aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pods.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/pods.js')
-rw-r--r--src/pods.js152
1 files changed, 123 insertions, 29 deletions
diff --git a/src/pods.js b/src/pods.js
index 8da216a55..defa9b1c1 100644
--- a/src/pods.js
+++ b/src/pods.js
@@ -43,7 +43,9 @@
43 } 43 }
44 44
45 // { url } 45 // { url }
46 // TODO: check if the pod is not already a friend
46 pods.add = function (data, callback) { 47 pods.add = function (data, callback) {
48 var videos = require('./videos')
47 logger.info('Adding pod: %s', data.url) 49 logger.info('Adding pod: %s', data.url)
48 50
49 var params = { 51 var params = {
@@ -58,13 +60,38 @@
58 return callback(err) 60 return callback(err)
59 } 61 }
60 62
63 videos.addRemotes(data.videos)
64
61 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) { 65 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
62 if (err) { 66 if (err) {
63 logger.error('Cannot read cert file.', { error: err }) 67 logger.error('Cannot read cert file.', { error: err })
64 return callback(err) 68 return callback(err)
65 } 69 }
66 70
67 return callback(null, { cert: cert }) 71 videos.listOwned(function (err, videos_list) {
72 if (err) {
73 logger.error('Cannot get the list of owned videos.', { error: err })
74 return callback(err)
75 }
76
77 return callback(null, { cert: cert, videos: videos_list })
78 })
79 })
80 })
81 }
82
83 pods.remove = function (url, callback) {
84 var videos = require('./videos')
85 logger.info('Removing %s pod.', url)
86
87 videos.removeAllRemotesOf(url, function (err) {
88 if (err) logger.error('Cannot remove all remote videos of %s.', url)
89
90 PodsDB.remove({ url: url }, function (err) {
91 if (err) return callback(err)
92
93 logger.info('%s pod removed.', url)
94 callback(null)
68 }) 95 })
69 }) 96 })
70 } 97 }
@@ -82,6 +109,7 @@
82 } 109 }
83 110
84 pods.makeFriends = function (callback) { 111 pods.makeFriends = function (callback) {
112 var videos = require('./videos')
85 var pods_score = {} 113 var pods_score = {}
86 114
87 logger.info('Make friends!') 115 logger.info('Make friends!')
@@ -137,43 +165,109 @@
137 } 165 }
138 166
139 function makeRequestsToWinningPods (cert, pods_list) { 167 function makeRequestsToWinningPods (cert, pods_list) {
140 var data = { 168 // Stop pool requests
141 url: http + '://' + host + ':' + port, 169 poolRequests.deactivate()
142 publicKey: cert 170 // Flush pool requests
143 } 171 poolRequests.forceSend()
172
173 // Get the list of our videos to send to our new friends
174 videos.listOwned(function (err, videos_list) {
175 if (err) throw err
176
177 var data = {
178 url: http + '://' + host + ':' + port,
179 publicKey: cert,
180 videos: videos_list
181 }
144 182
145 utils.makeMultipleRetryRequest( 183 utils.makeMultipleRetryRequest(
146 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, 184 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
147 185
148 pods_list, 186 pods_list,
149 187
150 function eachRequest (err, response, body, url, pod, callback_each_request) { 188 function eachRequest (err, response, body, url, pod, callback_each_request) {
151 // We add the pod if it responded correctly with its public certificate 189 // We add the pod if it responded correctly with its public certificate
152 if (!err && response.statusCode === 200) { 190 if (!err && response.statusCode === 200) {
153 pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { 191 pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
154 if (err) { 192 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
155 logger.error('Error with adding %s pod.', pod.url, { error: err })
156 }
157 193
194 videos.addRemotes(body.videos, function (err) {
195 if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err })
196
197 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
198 return callback_each_request()
199 })
200 })
201 } else {
202 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
158 return callback_each_request() 203 return callback_each_request()
159 }) 204 }
160 } else { 205 },
161 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
162 return callback_each_request()
163 }
164 },
165 206
166 function endRequests (err) { 207 function endRequests (err) {
167 if (err) { 208 // Now we made new friends, we can re activate the pool of requests
168 logger.error('There was some errors when we wanted to make friends.', { error: err }) 209 poolRequests.activate()
169 return callback(err) 210
211 if (err) {
212 logger.error('There was some errors when we wanted to make friends.', { error: err })
213 return callback(err)
214 }
215
216 logger.debug('makeRequestsToWinningPods finished.')
217 return callback(null)
170 } 218 }
219 )
220 })
221 }
222 }
171 223
172 logger.debug('makeRequestsToWinningPods finished.') 224 pods.quitFriends = function (callback) {
173 return callback(null) 225 // Stop pool requests
226 poolRequests.deactivate()
227 // Flush pool requests
228 poolRequests.forceSend()
229
230 PodsDB.find(function (err, pods) {
231 if (err) return callback(err)
232
233 var request = {
234 method: 'POST',
235 path: '/api/' + constants.API_VERSION + '/pods/remove',
236 sign: true,
237 encrypt: true,
238 data: {
239 url: 'me' // Fake data
174 } 240 }
175 ) 241 }
176 } 242
243 // Announce we quit them
244 utils.makeMultipleRetryRequest(request, pods, function () {
245 PodsDB.remove(function (err) {
246 poolRequests.activate()
247
248 if (err) return callback(err)
249
250 logger.info('Broke friends, so sad :(')
251
252 var videos = require('./videos')
253 videos.removeAllRemotes(function (err) {
254 if (err) return callback(err)
255
256 logger.info('Removed all remote videos.')
257 callback(null)
258 })
259 })
260 })
261 })
262 }
263
264 pods.hasFriends = function (callback) {
265 PodsDB.count(function (err, count) {
266 if (err) return callback(err)
267
268 var has_friends = (count !== 0)
269 callback(null, has_friends)
270 })
177 } 271 }
178 272
179 module.exports = pods 273 module.exports = pods