]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Move the creation of requests in lib instead of model for poolrequests
authorChocobozzz <florian.bigard@gmail.com>
Fri, 5 Feb 2016 18:41:22 +0000 (19:41 +0100)
committerChocobozzz <florian.bigard@gmail.com>
Fri, 5 Feb 2016 18:41:22 +0000 (19:41 +0100)
lib/friends.js
lib/poolRequests.js
models/poolRequests.js

index badf09c7dd7e6d184978d2479c117315ec4c524a..c05ccedb12bbd4294ad56fd8af9338dcabc92ab0 100644 (file)
@@ -10,7 +10,6 @@
   var logger = require('../helpers/logger')
   var peertubeCrypto = require('../helpers/peertubeCrypto')
   var Pods = require('../models/pods')
-  var PoolRequests = require('../models/poolRequests')
   var poolRequests = require('../lib/poolRequests')
   var requests = require('../helpers/requests')
   var Videos = require('../models/videos')
@@ -32,7 +31,7 @@
     var id = video.name + video.magnetUri
     // ensure namePath is null
     video.namePath = null
-    PoolRequests.addRequest(id, 'add', video)
+    poolRequests.addRequest(id, 'add', video)
   }
 
   function hasFriends (callback) {
   function removeVideoToFriends (video) {
     // To avoid duplicates
     var id = video.name + video.magnetUri
-    PoolRequests.addRequest(id, 'remove', video)
+    poolRequests.addRequest(id, 'remove', video)
   }
 
   // ---------------------------------------------------------------------------
index ccc3489ab195d137e281edffac51a5784b6256fa..5b7d5489d071766f6b91e3870045704c61d8f6a3 100644 (file)
 
   var poolRequests = {
     activate: activate,
+    addRequest: addRequest,
     deactivate: deactivate,
     forceSend: forceSend
   }
 
+  function activate () {
+    logger.info('Pool requests activated.')
+    timer = setInterval(makePoolRequests, constants.INTERVAL)
+  }
+
+  function addRequest (id, type, request) {
+    logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
+
+    PoolRequests.findById(id, function (err, entity) {
+      if (err) {
+        logger.error('Cannot find one pool request.', { error: err })
+        return // Abort
+      }
+
+      if (entity) {
+        if (entity.type === type) {
+          logger.error('Cannot insert two same requests.')
+          return // Abort
+        }
+
+        // Remove the request of the other type
+        PoolRequests.removeById(id, function (err) {
+          if (err) {
+            logger.error('Cannot remove a pool request.', { error: err })
+            return // Abort
+          }
+        })
+      } else {
+        PoolRequests.create(id, type, request, function (err) {
+          logger.error('Cannot create a pool request.', { error: err })
+          return // Abort
+        })
+      }
+    })
+  }
+
   function deactivate () {
     logger.info('Pool requests deactivated.')
     clearInterval(timer)
     makePoolRequests()
   }
 
-  function activate () {
-    logger.info('Pool requests activated.')
-    timer = setInterval(makePoolRequests, constants.INTERVAL)
-  }
-
   // ---------------------------------------------------------------------------
 
   module.exports = poolRequests
index 962e75e6a5bab36fac95e95c7073e5afc07a9df4..5070a1331f2b74ee3f7e5d57e24ccde85a90e963 100644 (file)
   // ---------------------------------------------------------------------------
 
   var PoolRequests = {
-    addRequest: addRequest,
+    create: create,
+    findById: findById,
     list: list,
+    removeRequestById: removeRequestById,
     removeRequests: removeRequests
   }
 
-  function addRequest (id, type, request) {
-    logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
-
-    PoolRequestsDB.findOne({ id: id }, function (err, entity) {
-      if (err) {
-        logger.error('Cannot find one pool request.', { error: err })
-        return // Abort
-      }
-
-      if (entity) {
-        if (entity.type === type) {
-          logger.error('Cannot insert two same requests.')
-          return // Abort
-        }
+  function create (id, type, request, callback) {
+    PoolRequestsDB.create({ id: id, type: type, request: request }, callback)
+  }
 
-        // Remove the request of the other type
-        PoolRequestsDB.remove({ id: id }, function (err) {
-          if (err) {
-            logger.error('Cannot remove a pool request.', { error: err })
-            return // Abort
-          }
-        })
-      } else {
-        PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) {
-          logger.error('Cannot create a pool request.', { error: err })
-          return // Abort
-        })
-      }
-    })
+  function findById (id, callback) {
+    PoolRequestsDB.findOne({ id: id }, callback)
   }
 
   function list (callback) {
     PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
   }
 
+  function removeRequestById (id, callback) {
+    PoolRequestsDB.remove({ id: id }, callback)
+  }
+
   function removeRequests (ids) {
     PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
       if (err) {