]>
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 Pods
= require('../models/pods')
12 var PoolRequests
= require('../models/poolRequests')
13 var poolRequests
= require('../lib/poolRequests')
14 var utils
= require('../helpers/utils')
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
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(utils
.getCertDir() + 'peertube.pub', 'utf8', function (err
, cert
) {
53 logger
.error('Cannot read public cert.', { error: err
})
57 var urls
= config
.get('network.friends')
59 async
.each(urls
, computeForeignPodsList
, function () {
60 logger
.debug('Pods scores computed.', { pods_score: pods_score
})
61 var pods_list
= computeWinningPods(urls
, pods_score
)
62 logger
.debug('Pods that we keep computed.', { pods_to_keep: pods_list
})
64 makeRequestsToWinningPods(cert
, pods_list
)
68 // -----------------------------------------------------------------------
70 function computeForeignPodsList (url
, callback
) {
71 // Let's give 1 point to the pod we ask the friends list
74 getForeignPodsList(url
, function (foreign_pods_list
) {
75 if (foreign_pods_list
.length
=== 0) return callback()
77 async
.each(foreign_pods_list
, function (foreign_pod
, callback_each
) {
78 var foreign_url
= foreign_pod
.url
80 if (pods_score
[foreign_url
]) pods_score
[foreign_url
]++
81 else pods_score
[foreign_url
] = 1
90 function computeWinningPods (urls
, pods_score
) {
91 // Build the list of pods to add
92 // Only add a pod if it exists in more than a half base pods
94 var base_score
= urls
.length
/ 2
95 Object
.keys(pods_score
).forEach(function (pod
) {
96 if (pods_score
[pod
] > base_score
) pods_list
.push({ url: pod
})
102 function makeRequestsToWinningPods (cert
, pods_list
) {
103 // Stop pool requests
104 poolRequests
.deactivate()
105 // Flush pool requests
106 poolRequests
.forceSend()
108 // Get the list of our videos to send to our new friends
109 Videos
.listOwned(function (err
, videos_list
) {
113 url: http
+ '://' + host
+ ':' + port
,
118 utils
.makeMultipleRetryRequest(
119 { method: 'POST', path: '/api/' + constants
.API_VERSION
+ '/pods/', data: data
},
123 function eachRequest (err
, response
, body
, url
, pod
, callback_each_request
) {
124 // We add the pod if it responded correctly with its public certificate
125 if (!err
&& response
.statusCode
=== 200) {
126 Pods
.add({ url: pod
.url
, publicKey: body
.cert
, score: constants
.FRIEND_BASE_SCORE
}, function (err
) {
127 if (err
) logger
.error('Error with adding %s pod.', pod
.url
, { error: err
})
129 Videos
.addRemotes(body
.videos
, function (err
) {
130 if (err
) logger
.error('Error with adding videos of pod.', pod
.url
, { error: err
})
132 logger
.debug('Adding remote videos from %s.', pod
.url
, { videos: body
.videos
})
133 return callback_each_request()
137 logger
.error('Error with adding %s pod.', pod
.url
, { error: err
|| new Error('Status not 200') })
138 return callback_each_request()
142 function endRequests (err
) {
143 // Now we made new friends, we can re activate the pool of requests
144 poolRequests
.activate()
147 logger
.error('There was some errors when we wanted to make friends.', { error: err
})
151 logger
.debug('makeRequestsToWinningPods finished.')
152 return callback(null)
159 function quitFriends (callback
) {
160 // Stop pool requests
161 poolRequests
.deactivate()
162 // Flush pool requests
163 poolRequests
.forceSend()
165 Pods
.list(function (err
, pods
) {
166 if (err
) return callback(err
)
170 path: '/api/' + constants
.API_VERSION
+ '/pods/remove',
174 url: 'me' // Fake data
178 // Announce we quit them
179 utils
.makeMultipleRetryRequest(request
, pods
, function () {
180 Pods
.removeAll(function (err
) {
181 poolRequests
.activate()
183 if (err
) return callback(err
)
185 logger
.info('Broke friends, so sad :(')
187 Videos
.removeAllRemotes(function (err
) {
188 if (err
) return callback(err
)
190 logger
.info('Removed all remote videos.')
198 function removeVideoToFriends (video
) {
199 // To avoid duplicates
200 var id
= video
.name
+ video
.magnetUri
201 PoolRequests
.addRequest(id
, 'remove', video
)
204 // ---------------------------------------------------------------------------
206 module
.exports
= pods
208 // ---------------------------------------------------------------------------
210 function getForeignPodsList (url
, callback
) {
211 var path
= '/api/' + constants
.API_VERSION
+ '/pods'
213 request
.get(url
+ path
, function (err
, response
, body
) {
215 callback(JSON
.parse(body
))