diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/friends.js | 5 | ||||
-rw-r--r-- | lib/poolRequests.js | 42 |
2 files changed, 39 insertions, 8 deletions
diff --git a/lib/friends.js b/lib/friends.js index badf09c7d..c05ccedb1 100644 --- a/lib/friends.js +++ b/lib/friends.js | |||
@@ -10,7 +10,6 @@ | |||
10 | var logger = require('../helpers/logger') | 10 | var logger = require('../helpers/logger') |
11 | var peertubeCrypto = require('../helpers/peertubeCrypto') | 11 | var peertubeCrypto = require('../helpers/peertubeCrypto') |
12 | var Pods = require('../models/pods') | 12 | var Pods = require('../models/pods') |
13 | var PoolRequests = require('../models/poolRequests') | ||
14 | var poolRequests = require('../lib/poolRequests') | 13 | var poolRequests = require('../lib/poolRequests') |
15 | var requests = require('../helpers/requests') | 14 | var requests = require('../helpers/requests') |
16 | var Videos = require('../models/videos') | 15 | var Videos = require('../models/videos') |
@@ -32,7 +31,7 @@ | |||
32 | var id = video.name + video.magnetUri | 31 | var id = video.name + video.magnetUri |
33 | // ensure namePath is null | 32 | // ensure namePath is null |
34 | video.namePath = null | 33 | video.namePath = null |
35 | PoolRequests.addRequest(id, 'add', video) | 34 | poolRequests.addRequest(id, 'add', video) |
36 | } | 35 | } |
37 | 36 | ||
38 | function hasFriends (callback) { | 37 | function hasFriends (callback) { |
@@ -204,7 +203,7 @@ | |||
204 | function removeVideoToFriends (video) { | 203 | function removeVideoToFriends (video) { |
205 | // To avoid duplicates | 204 | // To avoid duplicates |
206 | var id = video.name + video.magnetUri | 205 | var id = video.name + video.magnetUri |
207 | PoolRequests.addRequest(id, 'remove', video) | 206 | poolRequests.addRequest(id, 'remove', video) |
208 | } | 207 | } |
209 | 208 | ||
210 | // --------------------------------------------------------------------------- | 209 | // --------------------------------------------------------------------------- |
diff --git a/lib/poolRequests.js b/lib/poolRequests.js index ccc3489ab..5b7d5489d 100644 --- a/lib/poolRequests.js +++ b/lib/poolRequests.js | |||
@@ -15,10 +15,47 @@ | |||
15 | 15 | ||
16 | var poolRequests = { | 16 | var poolRequests = { |
17 | activate: activate, | 17 | activate: activate, |
18 | addRequest: addRequest, | ||
18 | deactivate: deactivate, | 19 | deactivate: deactivate, |
19 | forceSend: forceSend | 20 | forceSend: forceSend |
20 | } | 21 | } |
21 | 22 | ||
23 | function activate () { | ||
24 | logger.info('Pool requests activated.') | ||
25 | timer = setInterval(makePoolRequests, constants.INTERVAL) | ||
26 | } | ||
27 | |||
28 | function addRequest (id, type, request) { | ||
29 | logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) | ||
30 | |||
31 | PoolRequests.findById(id, function (err, entity) { | ||
32 | if (err) { | ||
33 | logger.error('Cannot find one pool request.', { error: err }) | ||
34 | return // Abort | ||
35 | } | ||
36 | |||
37 | if (entity) { | ||
38 | if (entity.type === type) { | ||
39 | logger.error('Cannot insert two same requests.') | ||
40 | return // Abort | ||
41 | } | ||
42 | |||
43 | // Remove the request of the other type | ||
44 | PoolRequests.removeById(id, function (err) { | ||
45 | if (err) { | ||
46 | logger.error('Cannot remove a pool request.', { error: err }) | ||
47 | return // Abort | ||
48 | } | ||
49 | }) | ||
50 | } else { | ||
51 | PoolRequests.create(id, type, request, function (err) { | ||
52 | logger.error('Cannot create a pool request.', { error: err }) | ||
53 | return // Abort | ||
54 | }) | ||
55 | } | ||
56 | }) | ||
57 | } | ||
58 | |||
22 | function deactivate () { | 59 | function deactivate () { |
23 | logger.info('Pool requests deactivated.') | 60 | logger.info('Pool requests deactivated.') |
24 | clearInterval(timer) | 61 | clearInterval(timer) |
@@ -29,11 +66,6 @@ | |||
29 | makePoolRequests() | 66 | makePoolRequests() |
30 | } | 67 | } |
31 | 68 | ||
32 | function activate () { | ||
33 | logger.info('Pool requests activated.') | ||
34 | timer = setInterval(makePoolRequests, constants.INTERVAL) | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | 69 | // --------------------------------------------------------------------------- |
38 | 70 | ||
39 | module.exports = poolRequests | 71 | module.exports = poolRequests |