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 /lib | |
parent | c45f7f84001c2731909db04dd82e1c1f290386eb (diff) | |
download | PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.gz PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.zst PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.zip |
Split models
Diffstat (limited to 'lib')
-rw-r--r-- | lib/friends.js | 218 | ||||
-rw-r--r-- | lib/poolRequests.js | 61 | ||||
-rw-r--r-- | lib/videos.js | 51 | ||||
-rw-r--r-- | lib/webtorrent.js | 2 |
4 files changed, 282 insertions, 50 deletions
diff --git a/lib/friends.js b/lib/friends.js new file mode 100644 index 000000000..e093c85c4 --- /dev/null +++ b/lib/friends.js | |||
@@ -0,0 +1,218 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var async = require('async') | ||
5 | var config = require('config') | ||
6 | var fs = require('fs') | ||
7 | var request = require('request') | ||
8 | |||
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') | ||
16 | |||
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 | var pods = { | ||
22 | addVideoToFriends: addVideoToFriends, | ||
23 | hasFriends: hasFriends, | ||
24 | makeFriends: makeFriends, | ||
25 | quitFriends: quitFriends, | ||
26 | removeVideoToFriends: removeVideoToFriends | ||
27 | } | ||
28 | |||
29 | function addVideoToFriends (video) { | ||
30 | // To avoid duplicates | ||
31 | var id = video.name + video.magnetUri | ||
32 | // namePath is null | ||
33 | // TODO | ||
34 | video.namePath = null | ||
35 | PoolRequests.addRequest(id, 'add', video) | ||
36 | } | ||
37 | |||
38 | function hasFriends (callback) { | ||
39 | Pods.count(function (err, count) { | ||
40 | if (err) return callback(err) | ||
41 | |||
42 | var has_friends = (count !== 0) | ||
43 | callback(null, has_friends) | ||
44 | }) | ||
45 | } | ||
46 | |||
47 | function makeFriends (callback) { | ||
48 | var pods_score = {} | ||
49 | |||
50 | logger.info('Make friends!') | ||
51 | fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { | ||
52 | if (err) { | ||
53 | logger.error('Cannot read public cert.', { error: err }) | ||
54 | return callback(err) | ||
55 | } | ||
56 | |||
57 | var urls = config.get('network.friends') | ||
58 | |||
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 }) | ||
63 | |||
64 | makeRequestsToWinningPods(cert, pods_list) | ||
65 | }) | ||
66 | }) | ||
67 | |||
68 | // ----------------------------------------------------------------------- | ||
69 | |||
70 | function computeForeignPodsList (url, callback) { | ||
71 | // Let's give 1 point to the pod we ask the friends list | ||
72 | pods_score[url] = 1 | ||
73 | |||
74 | getForeignPodsList(url, function (foreign_pods_list) { | ||
75 | if (foreign_pods_list.length === 0) return callback() | ||
76 | |||
77 | async.each(foreign_pods_list, function (foreign_pod, callback_each) { | ||
78 | var foreign_url = foreign_pod.url | ||
79 | |||
80 | if (pods_score[foreign_url]) pods_score[foreign_url]++ | ||
81 | else pods_score[foreign_url] = 1 | ||
82 | |||
83 | callback_each() | ||
84 | }, function () { | ||
85 | callback() | ||
86 | }) | ||
87 | }) | ||
88 | } | ||
89 | |||
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 | ||
93 | var pods_list = [] | ||
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 }) | ||
97 | }) | ||
98 | |||
99 | return pods_list | ||
100 | } | ||
101 | |||
102 | function makeRequestsToWinningPods (cert, pods_list) { | ||
103 | // Stop pool requests | ||
104 | poolRequests.deactivate() | ||
105 | // Flush pool requests | ||
106 | poolRequests.forceSend() | ||
107 | |||
108 | // Get the list of our videos to send to our new friends | ||
109 | Videos.listOwned(function (err, videos_list) { | ||
110 | if (err) throw err | ||
111 | |||
112 | var data = { | ||
113 | url: http + '://' + host + ':' + port, | ||
114 | publicKey: cert, | ||
115 | videos: videos_list | ||
116 | } | ||
117 | |||
118 | utils.makeMultipleRetryRequest( | ||
119 | { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, | ||
120 | |||
121 | pods_list, | ||
122 | |||
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 }) | ||
128 | |||
129 | Videos.addRemotes(body.videos, function (err) { | ||
130 | if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err }) | ||
131 | |||
132 | logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) | ||
133 | return callback_each_request() | ||
134 | }) | ||
135 | }) | ||
136 | } else { | ||
137 | logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) | ||
138 | return callback_each_request() | ||
139 | } | ||
140 | }, | ||
141 | |||
142 | function endRequests (err) { | ||
143 | // Now we made new friends, we can re activate the pool of requests | ||
144 | poolRequests.activate() | ||
145 | |||
146 | if (err) { | ||
147 | logger.error('There was some errors when we wanted to make friends.', { error: err }) | ||
148 | return callback(err) | ||
149 | } | ||
150 | |||
151 | logger.debug('makeRequestsToWinningPods finished.') | ||
152 | return callback(null) | ||
153 | } | ||
154 | ) | ||
155 | }) | ||
156 | } | ||
157 | } | ||
158 | |||
159 | function quitFriends (callback) { | ||
160 | // Stop pool requests | ||
161 | poolRequests.deactivate() | ||
162 | // Flush pool requests | ||
163 | poolRequests.forceSend() | ||
164 | |||
165 | Pods.list(function (err, pods) { | ||
166 | if (err) return callback(err) | ||
167 | |||
168 | var request = { | ||
169 | method: 'POST', | ||
170 | path: '/api/' + constants.API_VERSION + '/pods/remove', | ||
171 | sign: true, | ||
172 | encrypt: true, | ||
173 | data: { | ||
174 | url: 'me' // Fake data | ||
175 | } | ||
176 | } | ||
177 | |||
178 | // Announce we quit them | ||
179 | utils.makeMultipleRetryRequest(request, pods, function () { | ||
180 | Pods.removeAll(function (err) { | ||
181 | poolRequests.activate() | ||
182 | |||
183 | if (err) return callback(err) | ||
184 | |||
185 | logger.info('Broke friends, so sad :(') | ||
186 | |||
187 | Videos.removeAllRemotes(function (err) { | ||
188 | if (err) return callback(err) | ||
189 | |||
190 | logger.info('Removed all remote videos.') | ||
191 | callback(null) | ||
192 | }) | ||
193 | }) | ||
194 | }) | ||
195 | }) | ||
196 | } | ||
197 | |||
198 | function removeVideoToFriends (video) { | ||
199 | // To avoid duplicates | ||
200 | var id = video.name + video.magnetUri | ||
201 | PoolRequests.addRequest(id, 'remove', video) | ||
202 | } | ||
203 | |||
204 | // --------------------------------------------------------------------------- | ||
205 | |||
206 | module.exports = pods | ||
207 | |||
208 | // --------------------------------------------------------------------------- | ||
209 | |||
210 | function getForeignPodsList (url, callback) { | ||
211 | var path = '/api/' + constants.API_VERSION + '/pods' | ||
212 | |||
213 | request.get(url + path, function (err, response, body) { | ||
214 | if (err) throw err | ||
215 | callback(JSON.parse(body)) | ||
216 | }) | ||
217 | } | ||
218 | })() | ||
diff --git a/lib/poolRequests.js b/lib/poolRequests.js index 53f47d629..796f06149 100644 --- a/lib/poolRequests.js +++ b/lib/poolRequests.js | |||
@@ -5,18 +5,16 @@ | |||
5 | var pluck = require('lodash-node/compat/collection/pluck') | 5 | var pluck = require('lodash-node/compat/collection/pluck') |
6 | 6 | ||
7 | var constants = require('../initializers/constants') | 7 | var constants = require('../initializers/constants') |
8 | var database = require('../initializers/database') | ||
9 | var logger = require('../helpers/logger') | 8 | var logger = require('../helpers/logger') |
10 | var PodsDB = database.PodsDB | 9 | var Pods = require('../models/pods') |
11 | var PoolRequestsDB = database.PoolRequestsDB | 10 | var PoolRequests = require('../models/poolRequests') |
12 | var utils = require('../helpers/utils') | 11 | var utils = require('../helpers/utils') |
13 | var VideosDB = database.VideosDB | 12 | var Videos = require('../models/videos') |
14 | 13 | ||
15 | var timer = null | 14 | var timer = null |
16 | 15 | ||
17 | var poolRequests = { | 16 | var poolRequests = { |
18 | activate: activate, | 17 | activate: activate, |
19 | addToPoolRequests: addToPoolRequests, | ||
20 | deactivate: deactivate, | 18 | deactivate: deactivate, |
21 | forceSend: forceSend | 19 | forceSend: forceSend |
22 | } | 20 | } |
@@ -36,30 +34,6 @@ | |||
36 | timer = setInterval(makePoolRequests, constants.INTERVAL) | 34 | timer = setInterval(makePoolRequests, constants.INTERVAL) |
37 | } | 35 | } |
38 | 36 | ||
39 | function addToPoolRequests (id, type, request) { | ||
40 | logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) | ||
41 | |||
42 | PoolRequestsDB.findOne({ id: id }, function (err, entity) { | ||
43 | if (err) logger.error(err) | ||
44 | |||
45 | if (entity) { | ||
46 | if (entity.type === type) { | ||
47 | logger.error(new Error('Cannot insert two same requests.')) | ||
48 | return | ||
49 | } | ||
50 | |||
51 | // Remove the request of the other type | ||
52 | PoolRequestsDB.remove({ id: id }, function (err) { | ||
53 | if (err) logger.error(err) | ||
54 | }) | ||
55 | } else { | ||
56 | PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) { | ||
57 | if (err) logger.error(err) | ||
58 | }) | ||
59 | } | ||
60 | }) | ||
61 | } | ||
62 | |||
63 | // --------------------------------------------------------------------------- | 37 | // --------------------------------------------------------------------------- |
64 | 38 | ||
65 | module.exports = poolRequests | 39 | module.exports = poolRequests |
@@ -69,7 +43,7 @@ | |||
69 | function makePoolRequest (type, requests, callback) { | 43 | function makePoolRequest (type, requests, callback) { |
70 | if (!callback) callback = function () {} | 44 | if (!callback) callback = function () {} |
71 | 45 | ||
72 | PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) { | 46 | Pods.list(function (err, pods) { |
73 | if (err) throw err | 47 | if (err) throw err |
74 | 48 | ||
75 | var params = { | 49 | var params = { |
@@ -116,7 +90,7 @@ | |||
116 | function makePoolRequests () { | 90 | function makePoolRequests () { |
117 | logger.info('Making pool requests to friends.') | 91 | logger.info('Making pool requests to friends.') |
118 | 92 | ||
119 | PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, function (err, pool_requests) { | 93 | PoolRequests.list(function (err, pool_requests) { |
120 | if (err) throw err | 94 | if (err) throw err |
121 | 95 | ||
122 | if (pool_requests.length === 0) return | 96 | if (pool_requests.length === 0) return |
@@ -150,7 +124,7 @@ | |||
150 | makePoolRequest('add', requests.add.requests, function (err) { | 124 | makePoolRequest('add', requests.add.requests, function (err) { |
151 | if (err) logger.error('Errors when sent add pool requests.', { error: err }) | 125 | if (err) logger.error('Errors when sent add pool requests.', { error: err }) |
152 | 126 | ||
153 | removePoolRequestsFromDB(requests.add.ids) | 127 | PoolRequests.removeRequests(requests.add.ids) |
154 | }) | 128 | }) |
155 | } | 129 | } |
156 | 130 | ||
@@ -159,7 +133,7 @@ | |||
159 | makePoolRequest('remove', requests.remove.requests, function (err) { | 133 | makePoolRequest('remove', requests.remove.requests, function (err) { |
160 | if (err) logger.error('Errors when sent remove pool requests.', { error: err }) | 134 | if (err) logger.error('Errors when sent remove pool requests.', { error: err }) |
161 | 135 | ||
162 | removePoolRequestsFromDB(requests.remove.ids) | 136 | PoolRequests.removeRequests(requests.remove.ids) |
163 | }) | 137 | }) |
164 | } | 138 | } |
165 | }) | 139 | }) |
@@ -167,7 +141,7 @@ | |||
167 | } | 141 | } |
168 | 142 | ||
169 | function removeBadPods () { | 143 | function removeBadPods () { |
170 | PodsDB.find({ score: 0 }, { _id: 1, url: 1 }, function (err, pods) { | 144 | Pods.findBadPods(function (err, pods) { |
171 | if (err) throw err | 145 | if (err) throw err |
172 | 146 | ||
173 | if (pods.length === 0) return | 147 | if (pods.length === 0) return |
@@ -175,12 +149,12 @@ | |||
175 | var urls = pluck(pods, 'url') | 149 | var urls = pluck(pods, 'url') |
176 | var ids = pluck(pods, '_id') | 150 | var ids = pluck(pods, '_id') |
177 | 151 | ||
178 | VideosDB.remove({ podUrl: { $in: urls } }, function (err, r) { | 152 | Videos.removeAllRemotesOf(urls, function (err, r) { |
179 | if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err }) | 153 | if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err }) |
180 | var videos_removed = r.result.n | 154 | var videos_removed = r.result.n |
181 | logger.info('Removed %d videos.', videos_removed) | 155 | logger.info('Removed %d videos.', videos_removed) |
182 | 156 | ||
183 | PodsDB.remove({ _id: { $in: ids } }, function (err, r) { | 157 | Pods.removeAllByIds(ids, function (err, r) { |
184 | if (err) logger.error('Cannot remove bad pods.', { error: err }) | 158 | if (err) logger.error('Cannot remove bad pods.', { error: err }) |
185 | 159 | ||
186 | var pods_removed = r.result.n | 160 | var pods_removed = r.result.n |
@@ -190,22 +164,11 @@ | |||
190 | }) | 164 | }) |
191 | } | 165 | } |
192 | 166 | ||
193 | function removePoolRequestsFromDB (ids) { | ||
194 | PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { | ||
195 | if (err) { | ||
196 | logger.error('Cannot remove requests from the pool requests database.', { error: err }) | ||
197 | return | ||
198 | } | ||
199 | |||
200 | logger.info('Pool requests flushed.') | ||
201 | }) | ||
202 | } | ||
203 | |||
204 | function updatePodsScore (good_pods, bad_pods) { | 167 | function updatePodsScore (good_pods, bad_pods) { |
205 | logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) | 168 | logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) |
206 | 169 | ||
207 | PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec() | 170 | Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS) |
208 | PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) { | 171 | Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { |
209 | if (err) throw err | 172 | if (err) throw err |
210 | removeBadPods() | 173 | removeBadPods() |
211 | }) | 174 | }) |
diff --git a/lib/videos.js b/lib/videos.js new file mode 100644 index 000000000..5d23070a7 --- /dev/null +++ b/lib/videos.js | |||
@@ -0,0 +1,51 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var async = require('async') | ||
5 | var config = require('config') | ||
6 | var webtorrent = require('../lib/webTorrentNode') | ||
7 | |||
8 | var logger = require('../helpers/logger') | ||
9 | var Videos = require('../models/videos') | ||
10 | |||
11 | var uploadDir = __dirname + '/../' + config.get('storage.uploads') | ||
12 | |||
13 | var videos = { | ||
14 | seed: seed, | ||
15 | seedAllExisting: seedAllExisting | ||
16 | } | ||
17 | |||
18 | function seed (path, callback) { | ||
19 | logger.info('Seeding %s...', path) | ||
20 | |||
21 | webtorrent.seed(path, function (torrent) { | ||
22 | logger.info('%s seeded (%s).', path, torrent.magnetURI) | ||
23 | |||
24 | return callback(null, torrent) | ||
25 | }) | ||
26 | } | ||
27 | |||
28 | function seedAllExisting (callback) { | ||
29 | Videos.listOwned(function (err, videos_list) { | ||
30 | if (err) { | ||
31 | logger.error('Cannot get list of the videos to seed.', { error: err }) | ||
32 | return callback(err) | ||
33 | } | ||
34 | |||
35 | async.each(videos_list, function (video, each_callback) { | ||
36 | seed(uploadDir + video.namePath, function (err) { | ||
37 | if (err) { | ||
38 | logger.error('Cannot seed this video.', { error: err }) | ||
39 | return callback(err) | ||
40 | } | ||
41 | |||
42 | each_callback(null) | ||
43 | }) | ||
44 | }, callback) | ||
45 | }) | ||
46 | } | ||
47 | |||
48 | // --------------------------------------------------------------------------- | ||
49 | |||
50 | module.exports = videos | ||
51 | })() | ||
diff --git a/lib/webtorrent.js b/lib/webtorrent.js index 41e60499f..d1ca3c9f2 100644 --- a/lib/webtorrent.js +++ b/lib/webtorrent.js | |||
@@ -62,7 +62,7 @@ | |||
62 | try { | 62 | try { |
63 | wt.remove(magnetUri, callback) | 63 | wt.remove(magnetUri, callback) |
64 | } catch (err) { | 64 | } catch (err) { |
65 | console.log('Cannot remove the torrent from WebTorrent', { err: err }) | 65 | console.log('Cannot remove the torrent from WebTorrent') |
66 | return callback(null) | 66 | return callback(null) |
67 | } | 67 | } |
68 | 68 | ||