aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-02-05 19:41:22 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-02-05 19:41:22 +0100
commit1fe5076f63e0d3b291c4b9a2295220713f982f91 (patch)
treeed776cc0d43a4371c41f2d6c9f36a8155f68c042
parent8425cb894d4867d26fd5f7fae7862b0669f3c717 (diff)
downloadPeerTube-1fe5076f63e0d3b291c4b9a2295220713f982f91.tar.gz
PeerTube-1fe5076f63e0d3b291c4b9a2295220713f982f91.tar.zst
PeerTube-1fe5076f63e0d3b291c4b9a2295220713f982f91.zip
Move the creation of requests in lib instead of model for poolrequests
-rw-r--r--lib/friends.js5
-rw-r--r--lib/poolRequests.js42
-rw-r--r--models/poolRequests.js41
3 files changed, 51 insertions, 37 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
diff --git a/models/poolRequests.js b/models/poolRequests.js
index 962e75e6a..5070a1331 100644
--- a/models/poolRequests.js
+++ b/models/poolRequests.js
@@ -17,46 +17,29 @@
17 // --------------------------------------------------------------------------- 17 // ---------------------------------------------------------------------------
18 18
19 var PoolRequests = { 19 var PoolRequests = {
20 addRequest: addRequest, 20 create: create,
21 findById: findById,
21 list: list, 22 list: list,
23 removeRequestById: removeRequestById,
22 removeRequests: removeRequests 24 removeRequests: removeRequests
23 } 25 }
24 26
25 function addRequest (id, type, request) { 27 function create (id, type, request, callback) {
26 logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) 28 PoolRequestsDB.create({ id: id, type: type, request: request }, callback)
27 29 }
28 PoolRequestsDB.findOne({ id: id }, function (err, entity) {
29 if (err) {
30 logger.error('Cannot find one pool request.', { error: err })
31 return // Abort
32 }
33
34 if (entity) {
35 if (entity.type === type) {
36 logger.error('Cannot insert two same requests.')
37 return // Abort
38 }
39 30
40 // Remove the request of the other type 31 function findById (id, callback) {
41 PoolRequestsDB.remove({ id: id }, function (err) { 32 PoolRequestsDB.findOne({ id: id }, callback)
42 if (err) {
43 logger.error('Cannot remove a pool request.', { error: err })
44 return // Abort
45 }
46 })
47 } else {
48 PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) {
49 logger.error('Cannot create a pool request.', { error: err })
50 return // Abort
51 })
52 }
53 })
54 } 33 }
55 34
56 function list (callback) { 35 function list (callback) {
57 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) 36 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
58 } 37 }
59 38
39 function removeRequestById (id, callback) {
40 PoolRequestsDB.remove({ id: id }, callback)
41 }
42
60 function removeRequests (ids) { 43 function removeRequests (ids) {
61 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { 44 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
62 if (err) { 45 if (err) {