]>
Commit | Line | Data |
---|---|---|
c173e565 C |
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') | |
dac0a531 | 11 | var peertubeCrypto = require('../helpers/peertubeCrypto') |
c173e565 | 12 | var Pods = require('../models/pods') |
c173e565 | 13 | var poolRequests = require('../lib/poolRequests') |
dac0a531 | 14 | var requests = require('../helpers/requests') |
c173e565 C |
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 | |
8425cb89 | 32 | // ensure namePath is null |
c173e565 | 33 | video.namePath = null |
1fe5076f | 34 | poolRequests.addRequest(id, 'add', video) |
c173e565 C |
35 | } |
36 | ||
37 | function hasFriends (callback) { | |
38 | Pods.count(function (err, count) { | |
39 | if (err) return callback(err) | |
40 | ||
41 | var has_friends = (count !== 0) | |
42 | callback(null, has_friends) | |
43 | }) | |
44 | } | |
45 | ||
46 | function makeFriends (callback) { | |
47 | var pods_score = {} | |
48 | ||
49 | logger.info('Make friends!') | |
dac0a531 | 50 | fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { |
c173e565 | 51 | if (err) { |
8425cb89 | 52 | logger.error('Cannot read public cert.') |
c173e565 C |
53 | return callback(err) |
54 | } | |
55 | ||
56 | var urls = config.get('network.friends') | |
57 | ||
8425cb89 C |
58 | async.each(urls, computeForeignPodsList, function (err) { |
59 | if (err) return callback(err) | |
60 | ||
c173e565 C |
61 | logger.debug('Pods scores computed.', { pods_score: pods_score }) |
62 | var pods_list = computeWinningPods(urls, pods_score) | |
63 | logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) | |
64 | ||
65 | makeRequestsToWinningPods(cert, pods_list) | |
66 | }) | |
67 | }) | |
68 | ||
69 | // ----------------------------------------------------------------------- | |
70 | ||
71 | function computeForeignPodsList (url, callback) { | |
72 | // Let's give 1 point to the pod we ask the friends list | |
73 | pods_score[url] = 1 | |
74 | ||
8425cb89 C |
75 | getForeignPodsList(url, function (err, foreign_pods_list) { |
76 | if (err) return callback(err) | |
c173e565 C |
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 | ||
92 | function computeWinningPods (urls, pods_score) { | |
93 | // Build the list of pods to add | |
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 | ||
101 | return pods_list | |
102 | } | |
103 | ||
104 | function makeRequestsToWinningPods (cert, pods_list) { | |
105 | // Stop pool requests | |
106 | poolRequests.deactivate() | |
107 | // Flush pool requests | |
108 | poolRequests.forceSend() | |
109 | ||
110 | // Get the list of our videos to send to our new friends | |
111 | Videos.listOwned(function (err, videos_list) { | |
8425cb89 C |
112 | if (err) { |
113 | logger.error('Cannot get the list of videos we own.') | |
114 | return callback(err) | |
115 | } | |
c173e565 C |
116 | |
117 | var data = { | |
118 | url: http + '://' + host + ':' + port, | |
119 | publicKey: cert, | |
120 | videos: videos_list | |
121 | } | |
122 | ||
dac0a531 | 123 | requests.makeMultipleRetryRequest( |
c173e565 C |
124 | { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, |
125 | ||
126 | pods_list, | |
127 | ||
128 | function eachRequest (err, response, body, url, pod, callback_each_request) { | |
129 | // We add the pod if it responded correctly with its public certificate | |
130 | if (!err && response.statusCode === 200) { | |
131 | 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 }) | |
133 | ||
134 | Videos.addRemotes(body.videos, function (err) { | |
135 | if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err }) | |
136 | ||
137 | logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) | |
138 | return callback_each_request() | |
139 | }) | |
140 | }) | |
141 | } else { | |
142 | logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) | |
143 | return callback_each_request() | |
144 | } | |
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) { | |
8425cb89 | 152 | logger.error('There was some errors when we wanted to make friends.') |
c173e565 C |
153 | return callback(err) |
154 | } | |
155 | ||
156 | logger.debug('makeRequestsToWinningPods finished.') | |
157 | return callback(null) | |
158 | } | |
159 | ) | |
160 | }) | |
161 | } | |
162 | } | |
163 | ||
164 | function quitFriends (callback) { | |
165 | // Stop pool requests | |
166 | poolRequests.deactivate() | |
167 | // Flush pool requests | |
168 | poolRequests.forceSend() | |
169 | ||
170 | Pods.list(function (err, pods) { | |
171 | if (err) return callback(err) | |
172 | ||
173 | var request = { | |
174 | method: 'POST', | |
175 | path: '/api/' + constants.API_VERSION + '/pods/remove', | |
176 | sign: true, | |
177 | encrypt: true, | |
178 | data: { | |
179 | url: 'me' // Fake data | |
180 | } | |
181 | } | |
182 | ||
183 | // Announce we quit them | |
dac0a531 | 184 | requests.makeMultipleRetryRequest(request, pods, function () { |
c173e565 C |
185 | Pods.removeAll(function (err) { |
186 | poolRequests.activate() | |
187 | ||
188 | if (err) return callback(err) | |
189 | ||
190 | logger.info('Broke friends, so sad :(') | |
191 | ||
192 | Videos.removeAllRemotes(function (err) { | |
193 | if (err) return callback(err) | |
194 | ||
195 | logger.info('Removed all remote videos.') | |
196 | callback(null) | |
197 | }) | |
198 | }) | |
199 | }) | |
200 | }) | |
201 | } | |
202 | ||
203 | function removeVideoToFriends (video) { | |
204 | // To avoid duplicates | |
205 | var id = video.name + video.magnetUri | |
1fe5076f | 206 | poolRequests.addRequest(id, 'remove', video) |
c173e565 C |
207 | } |
208 | ||
209 | // --------------------------------------------------------------------------- | |
210 | ||
211 | module.exports = pods | |
212 | ||
213 | // --------------------------------------------------------------------------- | |
214 | ||
215 | function getForeignPodsList (url, callback) { | |
216 | var path = '/api/' + constants.API_VERSION + '/pods' | |
217 | ||
218 | request.get(url + path, function (err, response, body) { | |
8425cb89 C |
219 | if (err) return callback(err) |
220 | ||
221 | callback(null, JSON.parse(body)) | |
c173e565 C |
222 | }) |
223 | } | |
224 | })() |