]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - models/pods.js
Infile code reorganization
[github/Chocobozzz/PeerTube.git] / models / 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
cda02107
C
9 var constants = require('../initializers/constants')
10 var logger = require('../helpers/logger')
11 var PodsDB = require('../initializers/database').PodsDB
12 var poolRequests = require('../lib/poolRequests')
13 var utils = require('../helpers/utils')
8c308c2b 14
8c308c2b
C
15 var http = config.get('webserver.https') ? 'https' : 'http'
16 var host = config.get('webserver.host')
17 var port = config.get('webserver.port')
18
c45f7f84
C
19 var pods = {
20 add: add,
21 addVideoToFriends: addVideoToFriends,
22 list: list,
23 hasFriends: hasFriends,
24 makeFriends: makeFriends,
25 quitFriends: quitFriends,
26 remove: remove,
27 removeVideoToFriends
8c308c2b
C
28 }
29
45239549 30 // TODO: check if the pod is not already a friend
c45f7f84 31 function add (data, callback) {
45239549 32 var videos = require('./videos')
8c308c2b
C
33 logger.info('Adding pod: %s', data.url)
34
35 var params = {
36 url: data.url,
3bcb78b3 37 publicKey: data.publicKey,
656ea8f7 38 score: constants.FRIEND_BASE_SCORE
8c308c2b
C
39 }
40
41 PodsDB.create(params, function (err, pod) {
42 if (err) {
43 logger.error('Cannot insert the pod.', { error: err })
44 return callback(err)
45 }
46
45239549
C
47 videos.addRemotes(data.videos)
48
c45f7f84 49 fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
50 if (err) {
51 logger.error('Cannot read cert file.', { error: err })
52 return callback(err)
53 }
54
45239549
C
55 videos.listOwned(function (err, videos_list) {
56 if (err) {
57 logger.error('Cannot get the list of owned videos.', { error: err })
58 return callback(err)
59 }
60
61 return callback(null, { cert: cert, videos: videos_list })
62 })
63 })
64 })
65 }
66
c45f7f84
C
67 function addVideoToFriends (video) {
68 // To avoid duplicates
69 var id = video.name + video.magnetUri
70 poolRequests.addToPoolRequests(id, 'add', video)
71 }
45239549 72
c45f7f84
C
73 function list (callback) {
74 PodsDB.find(function (err, pods_list) {
75 if (err) {
76 logger.error('Cannot get the list of the pods.', { error: err })
77 return callback(err)
78 }
45239549 79
c45f7f84 80 return callback(null, pods_list)
8c308c2b
C
81 })
82 }
83
c45f7f84
C
84 function hasFriends (callback) {
85 PodsDB.count(function (err, count) {
86 if (err) return callback(err)
3bcb78b3 87
c45f7f84
C
88 var has_friends = (count !== 0)
89 callback(null, has_friends)
90 })
8c308c2b
C
91 }
92
c45f7f84 93 function makeFriends (callback) {
45239549 94 var videos = require('./videos')
a1860380
C
95 var pods_score = {}
96
97 logger.info('Make friends!')
c45f7f84 98 fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
99 if (err) {
100 logger.error('Cannot read public cert.', { error: err })
101 return callback(err)
102 }
103
104 var urls = config.get('network.friends')
8c308c2b 105
a1860380
C
106 async.each(urls, computeForeignPodsList, function () {
107 logger.debug('Pods scores computed.', { pods_score: pods_score })
108 var pods_list = computeWinningPods(urls, pods_score)
109 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
110
a1860380
C
111 makeRequestsToWinningPods(cert, pods_list)
112 })
113 })
114
115 // -----------------------------------------------------------------------
8c308c2b 116
a1860380 117 function computeForeignPodsList (url, callback) {
3bcb78b3
C
118 // Let's give 1 point to the pod we ask the friends list
119 pods_score[url] = 1
8c308c2b 120
a1860380
C
121 getForeignPodsList(url, function (foreign_pods_list) {
122 if (foreign_pods_list.length === 0) return callback()
123
124 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
125 var foreign_url = foreign_pod.url
126
127 if (pods_score[foreign_url]) pods_score[foreign_url]++
128 else pods_score[foreign_url] = 1
129
130 callback_each()
131 }, function () {
132 callback()
8c308c2b 133 })
a1860380
C
134 })
135 }
8c308c2b 136
a1860380
C
137 function computeWinningPods (urls, pods_score) {
138 // Build the list of pods to add
139 // Only add a pod if it exists in more than a half base pods
140 var pods_list = []
141 var base_score = urls.length / 2
142 Object.keys(pods_score).forEach(function (pod) {
143 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
144 })
8c308c2b 145
a1860380
C
146 return pods_list
147 }
8c308c2b 148
a1860380 149 function makeRequestsToWinningPods (cert, pods_list) {
45239549
C
150 // Stop pool requests
151 poolRequests.deactivate()
152 // Flush pool requests
153 poolRequests.forceSend()
154
155 // Get the list of our videos to send to our new friends
156 videos.listOwned(function (err, videos_list) {
157 if (err) throw err
158
159 var data = {
160 url: http + '://' + host + ':' + port,
161 publicKey: cert,
162 videos: videos_list
163 }
8c308c2b 164
45239549
C
165 utils.makeMultipleRetryRequest(
166 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
a1860380 167
45239549 168 pods_list,
a1860380 169
45239549
C
170 function eachRequest (err, response, body, url, pod, callback_each_request) {
171 // We add the pod if it responded correctly with its public certificate
172 if (!err && response.statusCode === 200) {
c45f7f84 173 add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
45239549 174 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
3bcb78b3 175
45239549
C
176 videos.addRemotes(body.videos, function (err) {
177 if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err })
178
179 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
180 return callback_each_request()
181 })
182 })
183 } else {
184 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
3bcb78b3 185 return callback_each_request()
45239549
C
186 }
187 },
a1860380 188
45239549
C
189 function endRequests (err) {
190 // Now we made new friends, we can re activate the pool of requests
191 poolRequests.activate()
192
193 if (err) {
194 logger.error('There was some errors when we wanted to make friends.', { error: err })
195 return callback(err)
196 }
197
198 logger.debug('makeRequestsToWinningPods finished.')
199 return callback(null)
a1860380 200 }
45239549
C
201 )
202 })
203 }
204 }
a1860380 205
c45f7f84 206 function quitFriends (callback) {
45239549
C
207 // Stop pool requests
208 poolRequests.deactivate()
209 // Flush pool requests
210 poolRequests.forceSend()
211
212 PodsDB.find(function (err, pods) {
213 if (err) return callback(err)
214
215 var request = {
216 method: 'POST',
217 path: '/api/' + constants.API_VERSION + '/pods/remove',
218 sign: true,
219 encrypt: true,
220 data: {
221 url: 'me' // Fake data
a1860380 222 }
45239549
C
223 }
224
225 // Announce we quit them
226 utils.makeMultipleRetryRequest(request, pods, function () {
227 PodsDB.remove(function (err) {
228 poolRequests.activate()
229
230 if (err) return callback(err)
231
232 logger.info('Broke friends, so sad :(')
233
234 var videos = require('./videos')
235 videos.removeAllRemotes(function (err) {
236 if (err) return callback(err)
237
238 logger.info('Removed all remote videos.')
239 callback(null)
240 })
241 })
242 })
243 })
244 }
245
c45f7f84
C
246 function remove (url, callback) {
247 var videos = require('./videos')
248 logger.info('Removing %s pod.', url)
45239549 249
c45f7f84
C
250 videos.removeAllRemotesOf(url, function (err) {
251 if (err) logger.error('Cannot remove all remote videos of %s.', url)
252
253 PodsDB.remove({ url: url }, function (err) {
254 if (err) return callback(err)
255
256 logger.info('%s pod removed.', url)
257 callback(null)
258 })
45239549 259 })
8c308c2b
C
260 }
261
c45f7f84
C
262 function removeVideoToFriends (video) {
263 // To avoid duplicates
264 var id = video.name + video.magnetUri
265 poolRequests.addToPoolRequests(id, 'remove', video)
266 }
267
268 // ---------------------------------------------------------------------------
269
8c308c2b 270 module.exports = pods
c45f7f84
C
271
272 // ---------------------------------------------------------------------------
273
274 function getForeignPodsList (url, callback) {
275 var path = '/api/' + constants.API_VERSION + '/pods'
276
277 request.get(url + path, function (err, response, body) {
278 if (err) throw err
279 callback(JSON.parse(body))
280 })
281 }
8c308c2b 282})()