]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - models/poolRequests.js
Move the creation of requests in lib instead of model for poolrequests
[github/Chocobozzz/PeerTube.git] / models / poolRequests.js
index 0f488ef04202e2af649e4f91f6274262b2c163fe..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(err)
-
-      if (entity) {
-        if (entity.type === type) {
-          logger.error(new Error('Cannot insert two same requests.'))
-          return
-        }
+  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(err)
-        })
-      } else {
-        PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) {
-          if (err) logger.error(err)
-        })
-      }
-    })
+  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) {
         logger.error('Cannot remove requests from the pool requests database.', { error: err })
-        return
+        return // Abort
       }
 
       logger.info('Pool requests flushed.')