aboutsummaryrefslogtreecommitdiffhomepage
path: root/models/pods.js
diff options
context:
space:
mode:
Diffstat (limited to 'models/pods.js')
-rw-r--r--models/pods.js120
1 files changed, 64 insertions, 56 deletions
diff --git a/models/pods.js b/models/pods.js
index c8d08b26f..ed2f0d8ee 100644
--- a/models/pods.js
+++ b/models/pods.js
@@ -12,39 +12,23 @@
12 var poolRequests = require('../lib/poolRequests') 12 var poolRequests = require('../lib/poolRequests')
13 var utils = require('../helpers/utils') 13 var utils = require('../helpers/utils')
14 14
15 var pods = {}
16
17 var http = config.get('webserver.https') ? 'https' : 'http' 15 var http = config.get('webserver.https') ? 'https' : 'http'
18 var host = config.get('webserver.host') 16 var host = config.get('webserver.host')
19 var port = config.get('webserver.port') 17 var port = config.get('webserver.port')
20 18
21 // ----------- Private functions ----------- 19 var pods = {
22 20 add: add,
23 function getForeignPodsList (url, callback) { 21 addVideoToFriends: addVideoToFriends,
24 var path = '/api/' + constants.API_VERSION + '/pods' 22 list: list,
25 23 hasFriends: hasFriends,
26 request.get(url + path, function (err, response, body) { 24 makeFriends: makeFriends,
27 if (err) throw err 25 quitFriends: quitFriends,
28 callback(JSON.parse(body)) 26 remove: remove,
29 }) 27 removeVideoToFriends
30 } 28 }
31 29
32 // ----------- Public functions -----------
33
34 pods.list = function (callback) {
35 PodsDB.find(function (err, pods_list) {
36 if (err) {
37 logger.error('Cannot get the list of the pods.', { error: err })
38 return callback(err)
39 }
40
41 return callback(null, pods_list)
42 })
43 }
44
45 // { url }
46 // TODO: check if the pod is not already a friend 30 // TODO: check if the pod is not already a friend
47 pods.add = function (data, callback) { 31 function add (data, callback) {
48 var videos = require('./videos') 32 var videos = require('./videos')
49 logger.info('Adding pod: %s', data.url) 33 logger.info('Adding pod: %s', data.url)
50 34
@@ -62,7 +46,7 @@
62 46
63 videos.addRemotes(data.videos) 47 videos.addRemotes(data.videos)
64 48
65 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) { 49 fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
66 if (err) { 50 if (err) {
67 logger.error('Cannot read cert file.', { error: err }) 51 logger.error('Cannot read cert file.', { error: err })
68 return callback(err) 52 return callback(err)
@@ -80,40 +64,38 @@
80 }) 64 })
81 } 65 }
82 66
83 pods.remove = function (url, callback) { 67 function addVideoToFriends (video) {
84 var videos = require('./videos') 68 // To avoid duplicates
85 logger.info('Removing %s pod.', url) 69 var id = video.name + video.magnetUri
86 70 poolRequests.addToPoolRequests(id, 'add', video)
87 videos.removeAllRemotesOf(url, function (err) { 71 }
88 if (err) logger.error('Cannot remove all remote videos of %s.', url)
89 72
90 PodsDB.remove({ url: url }, function (err) { 73 function list (callback) {
91 if (err) return callback(err) 74 PodsDB.find(function (err, pods_list) {
75 if (err) {
76 logger.error('Cannot get the list of the pods.', { error: err })
77 return callback(err)
78 }
92 79
93 logger.info('%s pod removed.', url) 80 return callback(null, pods_list)
94 callback(null)
95 })
96 }) 81 })
97 } 82 }
98 83
99 pods.addVideoToFriends = function (video) { 84 function hasFriends (callback) {
100 // To avoid duplicates 85 PodsDB.count(function (err, count) {
101 var id = video.name + video.magnetUri 86 if (err) return callback(err)
102 poolRequests.addToPoolRequests(id, 'add', video)
103 }
104 87
105 pods.removeVideoToFriends = function (video) { 88 var has_friends = (count !== 0)
106 // To avoid duplicates 89 callback(null, has_friends)
107 var id = video.name + video.magnetUri 90 })
108 poolRequests.addToPoolRequests(id, 'remove', video)
109 } 91 }
110 92
111 pods.makeFriends = function (callback) { 93 function makeFriends (callback) {
112 var videos = require('./videos') 94 var videos = require('./videos')
113 var pods_score = {} 95 var pods_score = {}
114 96
115 logger.info('Make friends!') 97 logger.info('Make friends!')
116 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) { 98 fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
117 if (err) { 99 if (err) {
118 logger.error('Cannot read public cert.', { error: err }) 100 logger.error('Cannot read public cert.', { error: err })
119 return callback(err) 101 return callback(err)
@@ -188,7 +170,7 @@
188 function eachRequest (err, response, body, url, pod, callback_each_request) { 170 function eachRequest (err, response, body, url, pod, callback_each_request) {
189 // We add the pod if it responded correctly with its public certificate 171 // We add the pod if it responded correctly with its public certificate
190 if (!err && response.statusCode === 200) { 172 if (!err && response.statusCode === 200) {
191 pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { 173 add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
192 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err }) 174 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
193 175
194 videos.addRemotes(body.videos, function (err) { 176 videos.addRemotes(body.videos, function (err) {
@@ -221,7 +203,7 @@
221 } 203 }
222 } 204 }
223 205
224 pods.quitFriends = function (callback) { 206 function quitFriends (callback) {
225 // Stop pool requests 207 // Stop pool requests
226 poolRequests.deactivate() 208 poolRequests.deactivate()
227 // Flush pool requests 209 // Flush pool requests
@@ -261,14 +243,40 @@
261 }) 243 })
262 } 244 }
263 245
264 pods.hasFriends = function (callback) { 246 function remove (url, callback) {
265 PodsDB.count(function (err, count) { 247 var videos = require('./videos')
266 if (err) return callback(err) 248 logger.info('Removing %s pod.', url)
267 249
268 var has_friends = (count !== 0) 250 videos.removeAllRemotesOf(url, function (err) {
269 callback(null, has_friends) 251 if (err) logger.error('Cannot remove all remote videos of %s.', url)
252
253 PodsDB.remove({ url: url }, function (err) {
254 if (err) return callback(err)
255
256 logger.info('%s pod removed.', url)
257 callback(null)
258 })
270 }) 259 })
271 } 260 }
272 261
262 function removeVideoToFriends (video) {
263 // To avoid duplicates
264 var id = video.name + video.magnetUri
265 poolRequests.addToPoolRequests(id, 'remove', video)
266 }
267
268 // ---------------------------------------------------------------------------
269
273 module.exports = pods 270 module.exports = pods
271
272 // ---------------------------------------------------------------------------
273
274 function getForeignPodsList (url, callback) {
275 var path = '/api/' + constants.API_VERSION + '/pods'
276
277 request.get(url + path, function (err, response, body) {
278 if (err) throw err
279 callback(JSON.parse(body))
280 })
281 }
274})() 282})()