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