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