aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/friends.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r--server/lib/friends.js39
1 files changed, 26 insertions, 13 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 9a2c5c06e..3b8a52060 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -11,6 +11,7 @@ const peertubeCrypto = require('../helpers/peertubeCrypto')
11const Pods = require('../models/pods') 11const Pods = require('../models/pods')
12const requestsScheduler = require('../lib/requestsScheduler') 12const requestsScheduler = require('../lib/requestsScheduler')
13const requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
14const videos = require('../lib/videos')
14const Videos = require('../models/videos') 15const Videos = require('../models/videos')
15 16
16const http = config.get('webserver.https') ? 'https' : 'http' 17const http = config.get('webserver.https') ? 'https' : 'http'
@@ -20,6 +21,7 @@ const port = config.get('webserver.port')
20const pods = { 21const pods = {
21 addVideoToFriends: addVideoToFriends, 22 addVideoToFriends: addVideoToFriends,
22 hasFriends: hasFriends, 23 hasFriends: hasFriends,
24 getMyCertificate: getMyCertificate,
23 makeFriends: makeFriends, 25 makeFriends: makeFriends,
24 quitFriends: quitFriends, 26 quitFriends: quitFriends,
25 removeVideoToFriends: removeVideoToFriends 27 removeVideoToFriends: removeVideoToFriends
@@ -42,11 +44,15 @@ function hasFriends (callback) {
42 }) 44 })
43} 45}
44 46
47function getMyCertificate (callback) {
48 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback)
49}
50
45function makeFriends (callback) { 51function makeFriends (callback) {
46 const pods_score = {} 52 const pods_score = {}
47 53
48 logger.info('Make friends!') 54 logger.info('Make friends!')
49 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { 55 getMyCertificate(function (err, cert) {
50 if (err) { 56 if (err) {
51 logger.error('Cannot read public cert.') 57 logger.error('Cannot read public cert.')
52 return callback(err) 58 return callback(err)
@@ -54,8 +60,8 @@ function makeFriends (callback) {
54 60
55 const urls = config.get('network.friends') 61 const urls = config.get('network.friends')
56 62
57 async.each(urls, function (url, callback) { 63 async.each(urls, function (url, callback_each) {
58 computeForeignPodsList(url, pods_score, callback) 64 computeForeignPodsList(url, pods_score, callback_each)
59 }, function (err) { 65 }, function (err) {
60 if (err) return callback(err) 66 if (err) return callback(err)
61 67
@@ -96,11 +102,18 @@ function quitFriends (callback) {
96 102
97 logger.info('Broke friends, so sad :(') 103 logger.info('Broke friends, so sad :(')
98 104
99 Videos.removeAllRemotes(function (err) { 105 Videos.listFromRemotes(function (err, videos_list) {
100 if (err) return callback(err) 106 if (err) return callback(err)
101 107
102 logger.info('Removed all remote videos.') 108 videos.removeRemoteVideos(videos_list, function (err) {
103 callback(null) 109 if (err) {
110 logger.error('Cannot remove remote videos.', { error: err })
111 return callback(err)
112 }
113
114 logger.info('Removed all remote videos.')
115 callback(null)
116 })
104 }) 117 })
105 }) 118 })
106 }) 119 })
@@ -127,16 +140,14 @@ function computeForeignPodsList (url, pods_score, callback) {
127 if (err) return callback(err) 140 if (err) return callback(err)
128 if (foreign_pods_list.length === 0) return callback() 141 if (foreign_pods_list.length === 0) return callback()
129 142
130 async.each(foreign_pods_list, function (foreign_pod, callback_each) { 143 foreign_pods_list.forEach(function (foreign_pod) {
131 const foreign_url = foreign_pod.url 144 const foreign_url = foreign_pod.url
132 145
133 if (pods_score[foreign_url]) pods_score[foreign_url]++ 146 if (pods_score[foreign_url]) pods_score[foreign_url]++
134 else pods_score[foreign_url] = 1 147 else pods_score[foreign_url] = 1
135
136 callback_each()
137 }, function () {
138 callback()
139 }) 148 })
149
150 callback()
140 }) 151 })
141} 152}
142 153
@@ -194,13 +205,15 @@ function makeRequestsToWinningPods (cert, pods_list, callback) {
194 logger.error('Error with adding %s pod.', pod.url, { error: err }) 205 logger.error('Error with adding %s pod.', pod.url, { error: err })
195 return callback_each_request() 206 return callback_each_request()
196 } 207 }
197 208 console.log('hihi')
198 Videos.addRemotes(body.videos, function (err) { 209 videos.createRemoteVideos(body.videos, function (err) {
199 if (err) { 210 if (err) {
200 logger.error('Error with adding videos of pod.', pod.url, { error: err }) 211 logger.error('Error with adding videos of pod.', pod.url, { error: err })
201 return callback_each_request() 212 return callback_each_request()
202 } 213 }
203 214
215 console.log('kik')
216
204 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) 217 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
205 return callback_each_request() 218 return callback_each_request()
206 }) 219 })