]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - models/pods.js
ed2f0d8ee9f20a7204d4b61f06ed6447739a81e0
4 var async
= require('async')
5 var config
= require('config')
7 var request
= require('request')
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')
15 var http
= config
.get('webserver.https') ? 'https' : 'http'
16 var host
= config
.get('webserver.host')
17 var port
= config
.get('webserver.port')
21 addVideoToFriends: addVideoToFriends
,
23 hasFriends: hasFriends
,
24 makeFriends: makeFriends
,
25 quitFriends: quitFriends
,
30 // TODO: check if the pod is not already a friend
31 function add (data
, callback
) {
32 var videos
= require('./videos')
33 logger
.info('Adding pod: %s', data
.url
)
37 publicKey: data
.publicKey
,
38 score: constants
.FRIEND_BASE_SCORE
41 PodsDB
.create(params
, function (err
, pod
) {
43 logger
.error('Cannot insert the pod.', { error: err
})
47 videos
.addRemotes(data
.videos
)
49 fs
.readFile(utils
.getCertDir() + 'peertube.pub', 'utf8', function (err
, cert
) {
51 logger
.error('Cannot read cert file.', { error: err
})
55 videos
.listOwned(function (err
, videos_list
) {
57 logger
.error('Cannot get the list of owned videos.', { error: err
})
61 return callback(null, { cert: cert
, videos: videos_list
})
67 function addVideoToFriends (video
) {
68 // To avoid duplicates
69 var id
= video
.name
+ video
.magnetUri
70 poolRequests
.addToPoolRequests(id
, 'add', video
)
73 function list (callback
) {
74 PodsDB
.find(function (err
, pods_list
) {
76 logger
.error('Cannot get the list of the pods.', { error: err
})
80 return callback(null, pods_list
)
84 function hasFriends (callback
) {
85 PodsDB
.count(function (err
, count
) {
86 if (err
) return callback(err
)
88 var has_friends
= (count
!== 0)
89 callback(null, has_friends
)
93 function makeFriends (callback
) {
94 var videos
= require('./videos')
97 logger
.info('Make friends!')
98 fs
.readFile(utils
.getCertDir() + 'peertube.pub', 'utf8', function (err
, cert
) {
100 logger
.error('Cannot read public cert.', { error: err
})
104 var urls
= config
.get('network.friends')
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
})
111 makeRequestsToWinningPods(cert
, pods_list
)
115 // -----------------------------------------------------------------------
117 function computeForeignPodsList (url
, callback
) {
118 // Let's give 1 point to the pod we ask the friends list
121 getForeignPodsList(url
, function (foreign_pods_list
) {
122 if (foreign_pods_list
.length
=== 0) return callback()
124 async
.each(foreign_pods_list
, function (foreign_pod
, callback_each
) {
125 var foreign_url
= foreign_pod
.url
127 if (pods_score
[foreign_url
]) pods_score
[foreign_url
]++
128 else pods_score
[foreign_url
] = 1
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
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
})
149 function makeRequestsToWinningPods (cert
, pods_list
) {
150 // Stop pool requests
151 poolRequests
.deactivate()
152 // Flush pool requests
153 poolRequests
.forceSend()
155 // Get the list of our videos to send to our new friends
156 videos
.listOwned(function (err
, videos_list
) {
160 url: http
+ '://' + host
+ ':' + port
,
165 utils
.makeMultipleRetryRequest(
166 { method: 'POST', path: '/api/' + constants
.API_VERSION
+ '/pods/', data: data
},
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) {
173 add({ url: pod
.url
, publicKey: body
.cert
, score: constants
.FRIEND_BASE_SCORE
}, function (err
) {
174 if (err
) logger
.error('Error with adding %s pod.', pod
.url
, { error: err
})
176 videos
.addRemotes(body
.videos
, function (err
) {
177 if (err
) logger
.error('Error with adding videos of pod.', pod
.url
, { error: err
})
179 logger
.debug('Adding remote videos from %s.', pod
.url
, { videos: body
.videos
})
180 return callback_each_request()
184 logger
.error('Error with adding %s pod.', pod
.url
, { error: err
|| new Error('Status not 200') })
185 return callback_each_request()
189 function endRequests (err
) {
190 // Now we made new friends, we can re activate the pool of requests
191 poolRequests
.activate()
194 logger
.error('There was some errors when we wanted to make friends.', { error: err
})
198 logger
.debug('makeRequestsToWinningPods finished.')
199 return callback(null)
206 function quitFriends (callback
) {
207 // Stop pool requests
208 poolRequests
.deactivate()
209 // Flush pool requests
210 poolRequests
.forceSend()
212 PodsDB
.find(function (err
, pods
) {
213 if (err
) return callback(err
)
217 path: '/api/' + constants
.API_VERSION
+ '/pods/remove',
221 url: 'me' // Fake data
225 // Announce we quit them
226 utils
.makeMultipleRetryRequest(request
, pods
, function () {
227 PodsDB
.remove(function (err
) {
228 poolRequests
.activate()
230 if (err
) return callback(err
)
232 logger
.info('Broke friends, so sad :(')
234 var videos
= require('./videos')
235 videos
.removeAllRemotes(function (err
) {
236 if (err
) return callback(err
)
238 logger
.info('Removed all remote videos.')
246 function remove (url
, callback
) {
247 var videos
= require('./videos')
248 logger
.info('Removing %s pod.', url
)
250 videos
.removeAllRemotesOf(url
, function (err
) {
251 if (err
) logger
.error('Cannot remove all remote videos of %s.', url
)
253 PodsDB
.remove({ url: url
}, function (err
) {
254 if (err
) return callback(err
)
256 logger
.info('%s pod removed.', url
)
262 function removeVideoToFriends (video
) {
263 // To avoid duplicates
264 var id
= video
.name
+ video
.magnetUri
265 poolRequests
.addToPoolRequests(id
, 'remove', video
)
268 // ---------------------------------------------------------------------------
270 module
.exports
= pods
272 // ---------------------------------------------------------------------------
274 function getForeignPodsList (url
, callback
) {
275 var path
= '/api/' + constants
.API_VERSION
+ '/pods'
277 request
.get(url
+ path
, function (err
, response
, body
) {
279 callback(JSON
.parse(body
))