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