]>
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
33 // ensure namePath is null
35 PoolRequests
.addRequest(id
, 'add', video
)
38 function hasFriends (callback
) {
39 Pods
.count(function (err
, count
) {
40 if (err
) return callback(err
)
42 var has_friends
= (count
!== 0)
43 callback(null, has_friends
)
47 function makeFriends (callback
) {
50 logger
.info('Make friends!')
51 fs
.readFile(peertubeCrypto
.getCertDir() + 'peertube.pub', 'utf8', function (err
, cert
) {
53 logger
.error('Cannot read public cert.')
57 var urls
= config
.get('network.friends')
59 async
.each(urls
, computeForeignPodsList
, function (err
) {
60 if (err
) return callback(err
)
62 logger
.debug('Pods scores computed.', { pods_score: pods_score
})
63 var pods_list
= computeWinningPods(urls
, pods_score
)
64 logger
.debug('Pods that we keep computed.', { pods_to_keep: pods_list
})
66 makeRequestsToWinningPods(cert
, pods_list
)
70 // -----------------------------------------------------------------------
72 function computeForeignPodsList (url
, callback
) {
73 // Let's give 1 point to the pod we ask the friends list
76 getForeignPodsList(url
, function (err
, foreign_pods_list
) {
77 if (err
) return callback(err
)
78 if (foreign_pods_list
.length
=== 0) return callback()
80 async
.each(foreign_pods_list
, function (foreign_pod
, callback_each
) {
81 var foreign_url
= foreign_pod
.url
83 if (pods_score
[foreign_url
]) pods_score
[foreign_url
]++
84 else pods_score
[foreign_url
] = 1
93 function computeWinningPods (urls
, pods_score
) {
94 // Build the list of pods to add
95 // Only add a pod if it exists in more than a half base pods
97 var base_score
= urls
.length
/ 2
98 Object
.keys(pods_score
).forEach(function (pod
) {
99 if (pods_score
[pod
] > base_score
) pods_list
.push({ url: pod
})
105 function makeRequestsToWinningPods (cert
, pods_list
) {
106 // Stop pool requests
107 poolRequests
.deactivate()
108 // Flush pool requests
109 poolRequests
.forceSend()
111 // Get the list of our videos to send to our new friends
112 Videos
.listOwned(function (err
, videos_list
) {
114 logger
.error('Cannot get the list of videos we own.')
119 url: http
+ '://' + host
+ ':' + port
,
124 requests
.makeMultipleRetryRequest(
125 { method: 'POST', path: '/api/' + constants
.API_VERSION
+ '/pods/', data: data
},
129 function eachRequest (err
, response
, body
, url
, pod
, callback_each_request
) {
130 // We add the pod if it responded correctly with its public certificate
131 if (!err
&& response
.statusCode
=== 200) {
132 Pods
.add({ url: pod
.url
, publicKey: body
.cert
, score: constants
.FRIEND_BASE_SCORE
}, function (err
) {
133 if (err
) logger
.error('Error with adding %s pod.', pod
.url
, { error: err
})
135 Videos
.addRemotes(body
.videos
, function (err
) {
136 if (err
) logger
.error('Error with adding videos of pod.', pod
.url
, { error: err
})
138 logger
.debug('Adding remote videos from %s.', pod
.url
, { videos: body
.videos
})
139 return callback_each_request()
143 logger
.error('Error with adding %s pod.', pod
.url
, { error: err
|| new Error('Status not 200') })
144 return callback_each_request()
148 function endRequests (err
) {
149 // Now we made new friends, we can re activate the pool of requests
150 poolRequests
.activate()
153 logger
.error('There was some errors when we wanted to make friends.')
157 logger
.debug('makeRequestsToWinningPods finished.')
158 return callback(null)
165 function quitFriends (callback
) {
166 // Stop pool requests
167 poolRequests
.deactivate()
168 // Flush pool requests
169 poolRequests
.forceSend()
171 Pods
.list(function (err
, pods
) {
172 if (err
) return callback(err
)
176 path: '/api/' + constants
.API_VERSION
+ '/pods/remove',
180 url: 'me' // Fake data
184 // Announce we quit them
185 requests
.makeMultipleRetryRequest(request
, pods
, function () {
186 Pods
.removeAll(function (err
) {
187 poolRequests
.activate()
189 if (err
) return callback(err
)
191 logger
.info('Broke friends, so sad :(')
193 Videos
.removeAllRemotes(function (err
) {
194 if (err
) return callback(err
)
196 logger
.info('Removed all remote videos.')
204 function removeVideoToFriends (video
) {
205 // To avoid duplicates
206 var id
= video
.name
+ video
.magnetUri
207 PoolRequests
.addRequest(id
, 'remove', video
)
210 // ---------------------------------------------------------------------------
212 module
.exports
= pods
214 // ---------------------------------------------------------------------------
216 function getForeignPodsList (url
, callback
) {
217 var path
= '/api/' + constants
.API_VERSION
+ '/pods'
219 request
.get(url
+ path
, function (err
, response
, body
) {
220 if (err
) return callback(err
)
222 callback(null, JSON
.parse(body
))