]>
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('../models/poolRequests')
14 var poolRequests
= require('../lib/poolRequests')
15 var requests
= require('../helpers/requests')
16 var Videos
= require('../models/videos')
18 var http
= config
.get('webserver.https') ? 'https' : 'http'
19 var host
= config
.get('webserver.host')
20 var port
= config
.get('webserver.port')
23 addVideoToFriends: addVideoToFriends
,
24 hasFriends: hasFriends
,
25 makeFriends: makeFriends
,
26 quitFriends: quitFriends
,
27 removeVideoToFriends: removeVideoToFriends
30 function addVideoToFriends (video
) {
31 // To avoid duplicates
32 var id
= video
.name
+ video
.magnetUri
36 PoolRequests
.addRequest(id
, 'add', video
)
39 function hasFriends (callback
) {
40 Pods
.count(function (err
, count
) {
41 if (err
) return callback(err
)
43 var has_friends
= (count
!== 0)
44 callback(null, has_friends
)
48 function makeFriends (callback
) {
51 logger
.info('Make friends!')
52 fs
.readFile(peertubeCrypto
.getCertDir() + 'peertube.pub', 'utf8', function (err
, cert
) {
54 logger
.error('Cannot read public cert.', { error: err
})
58 var urls
= config
.get('network.friends')
60 async
.each(urls
, computeForeignPodsList
, function () {
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 (foreign_pods_list
) {
76 if (foreign_pods_list
.length
=== 0) return callback()
78 async
.each(foreign_pods_list
, function (foreign_pod
, callback_each
) {
79 var foreign_url
= foreign_pod
.url
81 if (pods_score
[foreign_url
]) pods_score
[foreign_url
]++
82 else pods_score
[foreign_url
] = 1
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
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
})
103 function makeRequestsToWinningPods (cert
, pods_list
) {
104 // Stop pool requests
105 poolRequests
.deactivate()
106 // Flush pool requests
107 poolRequests
.forceSend()
109 // Get the list of our videos to send to our new friends
110 Videos
.listOwned(function (err
, videos_list
) {
114 url: http
+ '://' + host
+ ':' + port
,
119 requests
.makeMultipleRetryRequest(
120 { method: 'POST', path: '/api/' + constants
.API_VERSION
+ '/pods/', data: data
},
124 function eachRequest (err
, response
, body
, url
, pod
, callback_each_request
) {
125 // We add the pod if it responded correctly with its public certificate
126 if (!err
&& response
.statusCode
=== 200) {
127 Pods
.add({ url: pod
.url
, publicKey: body
.cert
, score: constants
.FRIEND_BASE_SCORE
}, function (err
) {
128 if (err
) logger
.error('Error with adding %s pod.', pod
.url
, { error: err
})
130 Videos
.addRemotes(body
.videos
, function (err
) {
131 if (err
) logger
.error('Error with adding videos of pod.', pod
.url
, { error: err
})
133 logger
.debug('Adding remote videos from %s.', pod
.url
, { videos: body
.videos
})
134 return callback_each_request()
138 logger
.error('Error with adding %s pod.', pod
.url
, { error: err
|| new Error('Status not 200') })
139 return callback_each_request()
143 function endRequests (err
) {
144 // Now we made new friends, we can re activate the pool of requests
145 poolRequests
.activate()
148 logger
.error('There was some errors when we wanted to make friends.', { error: err
})
152 logger
.debug('makeRequestsToWinningPods finished.')
153 return callback(null)
160 function quitFriends (callback
) {
161 // Stop pool requests
162 poolRequests
.deactivate()
163 // Flush pool requests
164 poolRequests
.forceSend()
166 Pods
.list(function (err
, pods
) {
167 if (err
) return callback(err
)
171 path: '/api/' + constants
.API_VERSION
+ '/pods/remove',
175 url: 'me' // Fake data
179 // Announce we quit them
180 requests
.makeMultipleRetryRequest(request
, pods
, function () {
181 Pods
.removeAll(function (err
) {
182 poolRequests
.activate()
184 if (err
) return callback(err
)
186 logger
.info('Broke friends, so sad :(')
188 Videos
.removeAllRemotes(function (err
) {
189 if (err
) return callback(err
)
191 logger
.info('Removed all remote videos.')
199 function removeVideoToFriends (video
) {
200 // To avoid duplicates
201 var id
= video
.name
+ video
.magnetUri
202 PoolRequests
.addRequest(id
, 'remove', video
)
205 // ---------------------------------------------------------------------------
207 module
.exports
= pods
209 // ---------------------------------------------------------------------------
211 function getForeignPodsList (url
, callback
) {
212 var path
= '/api/' + constants
.API_VERSION
+ '/pods'
214 request
.get(url
+ path
, function (err
, response
, body
) {
216 callback(JSON
.parse(body
))