]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - lib/friends.js
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 peertubeCrypto
= require('../helpers/peertubeCrypto')
12 var Pods
= require('../models/pods')
13 var poolRequests
= require('../lib/poolRequests')
14 var requests
= require('../helpers/requests')
15 var Videos
= require('../models/videos')
17 var http
= config
.get('webserver.https') ? 'https' : 'http'
18 var host
= config
.get('webserver.host')
19 var port
= config
.get('webserver.port')
22 addVideoToFriends: addVideoToFriends
,
23 hasFriends: hasFriends
,
24 makeFriends: makeFriends
,
25 quitFriends: quitFriends
,
26 removeVideoToFriends: removeVideoToFriends
29 function addVideoToFriends (video
) {
30 // To avoid duplicates
31 var id
= video
.name
+ video
.magnetUri
32 // ensure namePath is null
34 poolRequests
.addRequest(id
, 'add', video
)
37 function hasFriends (callback
) {
38 Pods
.count(function (err
, count
) {
39 if (err
) return callback(err
)
41 var has_friends
= (count
!== 0)
42 callback(null, has_friends
)
46 function makeFriends (callback
) {
49 logger
.info('Make friends!')
50 fs
.readFile(peertubeCrypto
.getCertDir() + 'peertube.pub', 'utf8', function (err
, cert
) {
52 logger
.error('Cannot read public cert.')
56 var urls
= config
.get('network.friends')
58 async
.each(urls
, computeForeignPodsList
, function (err
) {
59 if (err
) return callback(err
)
61 logger
.debug('Pods scores computed.', { pods_score: pods_score
})
62 var pods_list
= computeWinningPods(urls
, pods_score
)
63 logger
.debug('Pods that we keep computed.', { pods_to_keep: pods_list
})
65 makeRequestsToWinningPods(cert
, pods_list
)
69 // -----------------------------------------------------------------------
71 function computeForeignPodsList (url
, callback
) {
72 // Let's give 1 point to the pod we ask the friends list
75 getForeignPodsList(url
, function (err
, foreign_pods_list
) {
76 if (err
) return callback(err
)
77 if (foreign_pods_list
.length
=== 0) return callback()
79 async
.each(foreign_pods_list
, function (foreign_pod
, callback_each
) {
80 var foreign_url
= foreign_pod
.url
82 if (pods_score
[foreign_url
]) pods_score
[foreign_url
]++
83 else pods_score
[foreign_url
] = 1
92 function computeWinningPods (urls
, pods_score
) {
93 // Build the list of pods to add
94 // Only add a pod if it exists in more than a half base pods
96 var base_score
= urls
.length
/ 2
97 Object
.keys(pods_score
).forEach(function (pod
) {
98 if (pods_score
[pod
] > base_score
) pods_list
.push({ url: pod
})
104 function makeRequestsToWinningPods (cert
, pods_list
) {
105 // Stop pool requests
106 poolRequests
.deactivate()
107 // Flush pool requests
108 poolRequests
.forceSend()
110 // Get the list of our videos to send to our new friends
111 Videos
.listOwned(function (err
, videos_list
) {
113 logger
.error('Cannot get the list of videos we own.')
118 url: http
+ '://' + host
+ ':' + port
,
123 requests
.makeMultipleRetryRequest(
124 { method: 'POST', path: '/api/' + constants
.API_VERSION
+ '/pods/', data: data
},
128 function eachRequest (err
, response
, body
, url
, pod
, callback_each_request
) {
129 // We add the pod if it responded correctly with its public certificate
130 if (!err
&& response
.statusCode
=== 200) {
131 Pods
.add({ url: pod
.url
, publicKey: body
.cert
, score: constants
.FRIEND_BASE_SCORE
}, function (err
) {
132 if (err
) logger
.error('Error with adding %s pod.', pod
.url
, { error: err
})
134 Videos
.addRemotes(body
.videos
, function (err
) {
135 if (err
) logger
.error('Error with adding videos of pod.', pod
.url
, { error: err
})
137 logger
.debug('Adding remote videos from %s.', pod
.url
, { videos: body
.videos
})
138 return callback_each_request()
142 logger
.error('Error with adding %s pod.', pod
.url
, { error: err
|| new Error('Status not 200') })
143 return callback_each_request()
147 function endRequests (err
) {
148 // Now we made new friends, we can re activate the pool of requests
149 poolRequests
.activate()
152 logger
.error('There was some errors when we wanted to make friends.')
156 logger
.debug('makeRequestsToWinningPods finished.')
157 return callback(null)
164 function quitFriends (callback
) {
165 // Stop pool requests
166 poolRequests
.deactivate()
167 // Flush pool requests
168 poolRequests
.forceSend()
170 Pods
.list(function (err
, pods
) {
171 if (err
) return callback(err
)
175 path: '/api/' + constants
.API_VERSION
+ '/pods/remove',
179 url: 'me' // Fake data
183 // Announce we quit them
184 requests
.makeMultipleRetryRequest(request
, pods
, function () {
185 Pods
.removeAll(function (err
) {
186 poolRequests
.activate()
188 if (err
) return callback(err
)
190 logger
.info('Broke friends, so sad :(')
192 Videos
.removeAllRemotes(function (err
) {
193 if (err
) return callback(err
)
195 logger
.info('Removed all remote videos.')
203 function removeVideoToFriends (video
) {
204 // To avoid duplicates
205 var id
= video
.name
+ video
.magnetUri
206 poolRequests
.addRequest(id
, 'remove', video
)
209 // ---------------------------------------------------------------------------
211 module
.exports
= pods
213 // ---------------------------------------------------------------------------
215 function getForeignPodsList (url
, callback
) {
216 var path
= '/api/' + constants
.API_VERSION
+ '/pods'
218 request
.get(url
+ path
, function (err
, response
, body
) {
219 if (err
) return callback(err
)
221 callback(null, JSON
.parse(body
))