]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - src/pods.js
Create a constants module to easily modify some constants in a test
[github/Chocobozzz/PeerTube.git] / src / pods.js
CommitLineData
8c308c2b
C
1;(function () {
2 'use strict'
3
8c308c2b 4 var async = require('async')
a1860380
C
5 var config = require('config')
6 var fs = require('fs')
8c308c2b
C
7 var request = require('request')
8
656ea8f7 9 var constants = require('./constants')
8c308c2b 10 var logger = require('./logger')
8c308c2b 11 var PodsDB = require('./database').PodsDB
0b697522 12 var poolRequests = require('./poolRequests')
a1860380 13 var utils = require('./utils')
8c308c2b
C
14
15 var pods = {}
a1860380 16
8c308c2b
C
17 var http = config.get('webserver.https') ? 'https' : 'http'
18 var host = config.get('webserver.host')
19 var port = config.get('webserver.port')
20
21 // ----------- Private functions -----------
22
23 function getForeignPodsList (url, callback) {
656ea8f7 24 var path = '/api/' + constants.API_VERSION + '/pods'
8c308c2b
C
25
26 request.get(url + path, function (err, response, body) {
27 if (err) throw err
28 callback(JSON.parse(body))
29 })
30 }
31
32 // ----------- Public functions -----------
a1860380 33
8c308c2b
C
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 pods.add = function (data, callback) {
47 logger.info('Adding pod: %s', data.url)
48
49 var params = {
50 url: data.url,
3bcb78b3 51 publicKey: data.publicKey,
656ea8f7 52 score: constants.FRIEND_BASE_SCORE
8c308c2b
C
53 }
54
55 PodsDB.create(params, function (err, pod) {
56 if (err) {
57 logger.error('Cannot insert the pod.', { error: err })
58 return callback(err)
59 }
60
d148f3b9 61 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
62 if (err) {
63 logger.error('Cannot read cert file.', { error: err })
64 return callback(err)
65 }
66
67 return callback(null, { cert: cert })
68 })
69 })
70 }
71
0b697522
C
72 pods.addVideoToFriends = function (video) {
73 // To avoid duplicates
74 var id = video.name + video.magnetUri
75 poolRequests.addToPoolRequests(id, 'add', video)
76 }
3bcb78b3 77
0b697522
C
78 pods.removeVideoToFriends = function (video) {
79 // To avoid duplicates
80 var id = video.name + video.magnetUri
81 poolRequests.addToPoolRequests(id, 'remove', video)
8c308c2b
C
82 }
83
84 pods.makeFriends = function (callback) {
a1860380
C
85 var pods_score = {}
86
87 logger.info('Make friends!')
d148f3b9 88 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
89 if (err) {
90 logger.error('Cannot read public cert.', { error: err })
91 return callback(err)
92 }
93
94 var urls = config.get('network.friends')
8c308c2b 95
a1860380
C
96 async.each(urls, computeForeignPodsList, function () {
97 logger.debug('Pods scores computed.', { pods_score: pods_score })
98 var pods_list = computeWinningPods(urls, pods_score)
99 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
100
a1860380
C
101 makeRequestsToWinningPods(cert, pods_list)
102 })
103 })
104
105 // -----------------------------------------------------------------------
8c308c2b 106
a1860380 107 function computeForeignPodsList (url, callback) {
3bcb78b3
C
108 // Let's give 1 point to the pod we ask the friends list
109 pods_score[url] = 1
8c308c2b 110
a1860380
C
111 getForeignPodsList(url, function (foreign_pods_list) {
112 if (foreign_pods_list.length === 0) return callback()
113
114 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
115 var foreign_url = foreign_pod.url
116
117 if (pods_score[foreign_url]) pods_score[foreign_url]++
118 else pods_score[foreign_url] = 1
119
120 callback_each()
121 }, function () {
122 callback()
8c308c2b 123 })
a1860380
C
124 })
125 }
8c308c2b 126
a1860380
C
127 function computeWinningPods (urls, pods_score) {
128 // Build the list of pods to add
129 // Only add a pod if it exists in more than a half base pods
130 var pods_list = []
131 var base_score = urls.length / 2
132 Object.keys(pods_score).forEach(function (pod) {
133 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
134 })
8c308c2b 135
a1860380
C
136 return pods_list
137 }
8c308c2b 138
a1860380
C
139 function makeRequestsToWinningPods (cert, pods_list) {
140 var data = {
141 url: http + '://' + host + ':' + port,
142 publicKey: cert
143 }
8c308c2b 144
a1860380 145 utils.makeMultipleRetryRequest(
656ea8f7 146 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
a1860380
C
147
148 pods_list,
149
0b697522 150 function eachRequest (err, response, body, url, pod, callback_each_request) {
a1860380
C
151 // We add the pod if it responded correctly with its public certificate
152 if (!err && response.statusCode === 200) {
656ea8f7 153 pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
a1860380 154 if (err) {
3bcb78b3 155 logger.error('Error with adding %s pod.', pod.url, { error: err })
a1860380 156 }
3bcb78b3
C
157
158 return callback_each_request()
a1860380
C
159 })
160 } else {
3bcb78b3
C
161 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
162 return callback_each_request()
8c308c2b 163 }
a1860380
C
164 },
165
166 function endRequests (err) {
167 if (err) {
168 logger.error('There was some errors when we wanted to make friends.', { error: err })
169 return callback(err)
170 }
171
172 logger.debug('makeRequestsToWinningPods finished.')
173 return callback(null)
174 }
175 )
176 }
8c308c2b
C
177 }
178
179 module.exports = pods
180})()