]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - src/pods.js
Finalise the join in a network and add the ability to quit it
[github/Chocobozzz/PeerTube.git] / src / pods.js
CommitLineData
8c308c2b
C
1;(function () {
2 'use strict'
3
8c308c2b 4 var async = require('async')
a1860380
C
5 var config = require('config')
6 var fs = require('fs')
8c308c2b
C
7 var request = require('request')
8
656ea8f7 9 var constants = require('./constants')
8c308c2b 10 var logger = require('./logger')
8c308c2b 11 var PodsDB = require('./database').PodsDB
0b697522 12 var poolRequests = require('./poolRequests')
a1860380 13 var utils = require('./utils')
8c308c2b
C
14
15 var pods = {}
a1860380 16
8c308c2b
C
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 // ----------- Private functions -----------
22
23 function getForeignPodsList (url, callback) {
656ea8f7 24 var path = '/api/' + constants.API_VERSION + '/pods'
8c308c2b
C
25
26 request.get(url + path, function (err, response, body) {
27 if (err) throw err
28 callback(JSON.parse(body))
29 })
30 }
31
32 // ----------- Public functions -----------
a1860380 33
8c308c2b
C
34 pods.list = function (callback) {
35 PodsDB.find(function (err, pods_list) {
36 if (err) {
37 logger.error('Cannot get the list of the pods.', { error: err })
38 return callback(err)
39 }
40
41 return callback(null, pods_list)
42 })
43 }
44
45 // { url }
45239549 46 // TODO: check if the pod is not already a friend
8c308c2b 47 pods.add = function (data, callback) {
45239549 48 var videos = require('./videos')
8c308c2b
C
49 logger.info('Adding pod: %s', data.url)
50
51 var params = {
52 url: data.url,
3bcb78b3 53 publicKey: data.publicKey,
656ea8f7 54 score: constants.FRIEND_BASE_SCORE
8c308c2b
C
55 }
56
57 PodsDB.create(params, function (err, pod) {
58 if (err) {
59 logger.error('Cannot insert the pod.', { error: err })
60 return callback(err)
61 }
62
45239549
C
63 videos.addRemotes(data.videos)
64
d148f3b9 65 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
66 if (err) {
67 logger.error('Cannot read cert file.', { error: err })
68 return callback(err)
69 }
70
45239549
C
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)
8c308c2b
C
95 })
96 })
97 }
98
0b697522
C
99 pods.addVideoToFriends = function (video) {
100 // To avoid duplicates
101 var id = video.name + video.magnetUri
102 poolRequests.addToPoolRequests(id, 'add', video)
103 }
3bcb78b3 104
0b697522
C
105 pods.removeVideoToFriends = function (video) {
106 // To avoid duplicates
107 var id = video.name + video.magnetUri
108 poolRequests.addToPoolRequests(id, 'remove', video)
8c308c2b
C
109 }
110
111 pods.makeFriends = function (callback) {
45239549 112 var videos = require('./videos')
a1860380
C
113 var pods_score = {}
114
115 logger.info('Make friends!')
d148f3b9 116 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
117 if (err) {
118 logger.error('Cannot read public cert.', { error: err })
119 return callback(err)
120 }
121
122 var urls = config.get('network.friends')
8c308c2b 123
a1860380
C
124 async.each(urls, computeForeignPodsList, function () {
125 logger.debug('Pods scores computed.', { pods_score: pods_score })
126 var pods_list = computeWinningPods(urls, pods_score)
127 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
128
a1860380
C
129 makeRequestsToWinningPods(cert, pods_list)
130 })
131 })
132
133 // -----------------------------------------------------------------------
8c308c2b 134
a1860380 135 function computeForeignPodsList (url, callback) {
3bcb78b3
C
136 // Let's give 1 point to the pod we ask the friends list
137 pods_score[url] = 1
8c308c2b 138
a1860380
C
139 getForeignPodsList(url, function (foreign_pods_list) {
140 if (foreign_pods_list.length === 0) return callback()
141
142 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
143 var foreign_url = foreign_pod.url
144
145 if (pods_score[foreign_url]) pods_score[foreign_url]++
146 else pods_score[foreign_url] = 1
147
148 callback_each()
149 }, function () {
150 callback()
8c308c2b 151 })
a1860380
C
152 })
153 }
8c308c2b 154
a1860380
C
155 function computeWinningPods (urls, pods_score) {
156 // Build the list of pods to add
157 // Only add a pod if it exists in more than a half base pods
158 var pods_list = []
159 var base_score = urls.length / 2
160 Object.keys(pods_score).forEach(function (pod) {
161 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
162 })
8c308c2b 163
a1860380
C
164 return pods_list
165 }
8c308c2b 166
a1860380 167 function makeRequestsToWinningPods (cert, pods_list) {
45239549
C
168 // Stop pool requests
169 poolRequests.deactivate()
170 // Flush pool requests
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 }
8c308c2b 182
45239549
C
183 utils.makeMultipleRetryRequest(
184 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
a1860380 185
45239549 186 pods_list,
a1860380 187
45239549
C
188 function eachRequest (err, response, body, url, pod, callback_each_request) {
189 // We add the pod if it responded correctly with its public certificate
190 if (!err && response.statusCode === 200) {
191 pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
192 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
3bcb78b3 193
45239549
C
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') })
3bcb78b3 203 return callback_each_request()
45239549
C
204 }
205 },
a1860380 206
45239549
C
207 function endRequests (err) {
208 // Now we made new friends, we can re activate the pool of requests
209 poolRequests.activate()
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)
a1860380 218 }
45239549
C
219 )
220 })
221 }
222 }
a1860380 223
45239549
C
224 pods.quitFriends = function (callback) {
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
a1860380 240 }
45239549
C
241 }
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 })
8c308c2b
C
271 }
272
273 module.exports = pods
274})()