diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-02-04 21:10:33 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-02-04 21:10:33 +0100 |
commit | c173e56520b0fe4206b9ea8049b6add40bfeabcd (patch) | |
tree | 264c6cbf1bf81a6522685b4be5771bbeef4cd5dc /models/pods.js | |
parent | c45f7f84001c2731909db04dd82e1c1f290386eb (diff) | |
download | PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.gz PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.zst PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.zip |
Split models
Diffstat (limited to 'models/pods.js')
-rw-r--r-- | models/pods.js | 276 |
1 files changed, 42 insertions, 234 deletions
diff --git a/models/pods.js b/models/pods.js index ed2f0d8ee..395b1e0b1 100644 --- a/models/pods.js +++ b/models/pods.js | |||
@@ -1,73 +1,61 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | var async = require('async') | 4 | var mongoose = require('mongoose') |
5 | var config = require('config') | ||
6 | var fs = require('fs') | ||
7 | var request = require('request') | ||
8 | 5 | ||
9 | var constants = require('../initializers/constants') | 6 | var constants = require('../initializers/constants') |
10 | var logger = require('../helpers/logger') | 7 | var logger = require('../helpers/logger') |
11 | var PodsDB = require('../initializers/database').PodsDB | ||
12 | var poolRequests = require('../lib/poolRequests') | ||
13 | var utils = require('../helpers/utils') | ||
14 | 8 | ||
15 | var http = config.get('webserver.https') ? 'https' : 'http' | 9 | // --------------------------------------------------------------------------- |
16 | var host = config.get('webserver.host') | 10 | |
17 | var port = config.get('webserver.port') | 11 | var podsSchema = mongoose.Schema({ |
12 | url: String, | ||
13 | publicKey: String, | ||
14 | score: { type: Number, max: constants.FRIEND_BASE_SCORE } | ||
15 | }) | ||
16 | var PodsDB = mongoose.model('pods', podsSchema) | ||
18 | 17 | ||
19 | var pods = { | 18 | // --------------------------------------------------------------------------- |
19 | |||
20 | var Pods = { | ||
20 | add: add, | 21 | add: add, |
21 | addVideoToFriends: addVideoToFriends, | 22 | count: count, |
23 | findByUrl: findByUrl, | ||
24 | findBadPods: findBadPods, | ||
25 | incrementScores: incrementScores, | ||
22 | list: list, | 26 | list: list, |
23 | hasFriends: hasFriends, | ||
24 | makeFriends: makeFriends, | ||
25 | quitFriends: quitFriends, | ||
26 | remove: remove, | 27 | remove: remove, |
27 | removeVideoToFriends | 28 | removeAll: removeAll, |
29 | removeAllByIds: removeAllByIds | ||
28 | } | 30 | } |
29 | 31 | ||
30 | // TODO: check if the pod is not already a friend | 32 | // TODO: check if the pod is not already a friend |
31 | function add (data, callback) { | 33 | function add (data, callback) { |
32 | var videos = require('./videos') | 34 | if (!callback) callback = function () {} |
33 | logger.info('Adding pod: %s', data.url) | ||
34 | |||
35 | var params = { | 35 | var params = { |
36 | url: data.url, | 36 | url: data.url, |
37 | publicKey: data.publicKey, | 37 | publicKey: data.publicKey, |
38 | score: constants.FRIEND_BASE_SCORE | 38 | score: constants.FRIEND_BASE_SCORE |
39 | } | 39 | } |
40 | 40 | ||
41 | PodsDB.create(params, function (err, pod) { | 41 | PodsDB.create(params, callback) |
42 | if (err) { | 42 | } |
43 | logger.error('Cannot insert the pod.', { error: err }) | ||
44 | return callback(err) | ||
45 | } | ||
46 | |||
47 | videos.addRemotes(data.videos) | ||
48 | 43 | ||
49 | fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { | 44 | function count (callback) { |
50 | if (err) { | 45 | return PodsDB.count(callback) |
51 | logger.error('Cannot read cert file.', { error: err }) | 46 | } |
52 | return callback(err) | ||
53 | } | ||
54 | 47 | ||
55 | videos.listOwned(function (err, videos_list) { | 48 | function findBadPods (callback) { |
56 | if (err) { | 49 | PodsDB.find({ score: 0 }, callback) |
57 | logger.error('Cannot get the list of owned videos.', { error: err }) | 50 | } |
58 | return callback(err) | ||
59 | } | ||
60 | 51 | ||
61 | return callback(null, { cert: cert, videos: videos_list }) | 52 | function findByUrl (url, callback) { |
62 | }) | 53 | PodsDB.findOne({ url: url }, callback) |
63 | }) | ||
64 | }) | ||
65 | } | 54 | } |
66 | 55 | ||
67 | function addVideoToFriends (video) { | 56 | function incrementScores (ids, value, callback) { |
68 | // To avoid duplicates | 57 | if (!callback) callback = function () {} |
69 | var id = video.name + video.magnetUri | 58 | PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) |
70 | poolRequests.addToPoolRequests(id, 'add', video) | ||
71 | } | 59 | } |
72 | 60 | ||
73 | function list (callback) { | 61 | function list (callback) { |
@@ -81,202 +69,22 @@ | |||
81 | }) | 69 | }) |
82 | } | 70 | } |
83 | 71 | ||
84 | function hasFriends (callback) { | ||
85 | PodsDB.count(function (err, count) { | ||
86 | if (err) return callback(err) | ||
87 | |||
88 | var has_friends = (count !== 0) | ||
89 | callback(null, has_friends) | ||
90 | }) | ||
91 | } | ||
92 | |||
93 | function makeFriends (callback) { | ||
94 | var videos = require('./videos') | ||
95 | var pods_score = {} | ||
96 | |||
97 | logger.info('Make friends!') | ||
98 | fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { | ||
99 | if (err) { | ||
100 | logger.error('Cannot read public cert.', { error: err }) | ||
101 | return callback(err) | ||
102 | } | ||
103 | |||
104 | var urls = config.get('network.friends') | ||
105 | |||
106 | async.each(urls, computeForeignPodsList, function () { | ||
107 | logger.debug('Pods scores computed.', { pods_score: pods_score }) | ||
108 | var pods_list = computeWinningPods(urls, pods_score) | ||
109 | logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) | ||
110 | |||
111 | makeRequestsToWinningPods(cert, pods_list) | ||
112 | }) | ||
113 | }) | ||
114 | |||
115 | // ----------------------------------------------------------------------- | ||
116 | |||
117 | function computeForeignPodsList (url, callback) { | ||
118 | // Let's give 1 point to the pod we ask the friends list | ||
119 | pods_score[url] = 1 | ||
120 | |||
121 | getForeignPodsList(url, function (foreign_pods_list) { | ||
122 | if (foreign_pods_list.length === 0) return callback() | ||
123 | |||
124 | async.each(foreign_pods_list, function (foreign_pod, callback_each) { | ||
125 | var foreign_url = foreign_pod.url | ||
126 | |||
127 | if (pods_score[foreign_url]) pods_score[foreign_url]++ | ||
128 | else pods_score[foreign_url] = 1 | ||
129 | |||
130 | callback_each() | ||
131 | }, function () { | ||
132 | callback() | ||
133 | }) | ||
134 | }) | ||
135 | } | ||
136 | |||
137 | function computeWinningPods (urls, pods_score) { | ||
138 | // Build the list of pods to add | ||
139 | // Only add a pod if it exists in more than a half base pods | ||
140 | var pods_list = [] | ||
141 | var base_score = urls.length / 2 | ||
142 | Object.keys(pods_score).forEach(function (pod) { | ||
143 | if (pods_score[pod] > base_score) pods_list.push({ url: pod }) | ||
144 | }) | ||
145 | |||
146 | return pods_list | ||
147 | } | ||
148 | |||
149 | function makeRequestsToWinningPods (cert, pods_list) { | ||
150 | // Stop pool requests | ||
151 | poolRequests.deactivate() | ||
152 | // Flush pool requests | ||
153 | poolRequests.forceSend() | ||
154 | |||
155 | // Get the list of our videos to send to our new friends | ||
156 | videos.listOwned(function (err, videos_list) { | ||
157 | if (err) throw err | ||
158 | |||
159 | var data = { | ||
160 | url: http + '://' + host + ':' + port, | ||
161 | publicKey: cert, | ||
162 | videos: videos_list | ||
163 | } | ||
164 | |||
165 | utils.makeMultipleRetryRequest( | ||
166 | { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, | ||
167 | |||
168 | pods_list, | ||
169 | |||
170 | function eachRequest (err, response, body, url, pod, callback_each_request) { | ||
171 | // We add the pod if it responded correctly with its public certificate | ||
172 | if (!err && response.statusCode === 200) { | ||
173 | add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { | ||
174 | if (err) logger.error('Error with adding %s pod.', pod.url, { error: err }) | ||
175 | |||
176 | videos.addRemotes(body.videos, function (err) { | ||
177 | if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err }) | ||
178 | |||
179 | logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) | ||
180 | return callback_each_request() | ||
181 | }) | ||
182 | }) | ||
183 | } else { | ||
184 | logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) | ||
185 | return callback_each_request() | ||
186 | } | ||
187 | }, | ||
188 | |||
189 | function endRequests (err) { | ||
190 | // Now we made new friends, we can re activate the pool of requests | ||
191 | poolRequests.activate() | ||
192 | |||
193 | if (err) { | ||
194 | logger.error('There was some errors when we wanted to make friends.', { error: err }) | ||
195 | return callback(err) | ||
196 | } | ||
197 | |||
198 | logger.debug('makeRequestsToWinningPods finished.') | ||
199 | return callback(null) | ||
200 | } | ||
201 | ) | ||
202 | }) | ||
203 | } | ||
204 | } | ||
205 | |||
206 | function quitFriends (callback) { | ||
207 | // Stop pool requests | ||
208 | poolRequests.deactivate() | ||
209 | // Flush pool requests | ||
210 | poolRequests.forceSend() | ||
211 | |||
212 | PodsDB.find(function (err, pods) { | ||
213 | if (err) return callback(err) | ||
214 | |||
215 | var request = { | ||
216 | method: 'POST', | ||
217 | path: '/api/' + constants.API_VERSION + '/pods/remove', | ||
218 | sign: true, | ||
219 | encrypt: true, | ||
220 | data: { | ||
221 | url: 'me' // Fake data | ||
222 | } | ||
223 | } | ||
224 | |||
225 | // Announce we quit them | ||
226 | utils.makeMultipleRetryRequest(request, pods, function () { | ||
227 | PodsDB.remove(function (err) { | ||
228 | poolRequests.activate() | ||
229 | |||
230 | if (err) return callback(err) | ||
231 | |||
232 | logger.info('Broke friends, so sad :(') | ||
233 | |||
234 | var videos = require('./videos') | ||
235 | videos.removeAllRemotes(function (err) { | ||
236 | if (err) return callback(err) | ||
237 | |||
238 | logger.info('Removed all remote videos.') | ||
239 | callback(null) | ||
240 | }) | ||
241 | }) | ||
242 | }) | ||
243 | }) | ||
244 | } | ||
245 | |||
246 | function remove (url, callback) { | 72 | function remove (url, callback) { |
247 | var videos = require('./videos') | 73 | if (!callback) callback = function () {} |
248 | logger.info('Removing %s pod.', url) | 74 | PodsDB.remove({ url: url }, callback) |
249 | |||
250 | videos.removeAllRemotesOf(url, function (err) { | ||
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 | }) | ||
259 | }) | ||
260 | } | 75 | } |
261 | 76 | ||
262 | function removeVideoToFriends (video) { | 77 | function removeAll (callback) { |
263 | // To avoid duplicates | 78 | if (!callback) callback = function () {} |
264 | var id = video.name + video.magnetUri | 79 | PodsDB.remove(callback) |
265 | poolRequests.addToPoolRequests(id, 'remove', video) | ||
266 | } | 80 | } |
267 | 81 | ||
268 | // --------------------------------------------------------------------------- | 82 | function removeAllByIds (ids, callback) { |
269 | 83 | if (!callback) callback = function () {} | |
270 | module.exports = pods | 84 | PodsDB.remove({ _id: { $in: ids } }, callback) |
85 | } | ||
271 | 86 | ||
272 | // --------------------------------------------------------------------------- | 87 | // --------------------------------------------------------------------------- |
273 | 88 | ||
274 | function getForeignPodsList (url, callback) { | 89 | module.exports = Pods |
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 | } | ||
282 | })() | 90 | })() |