From c173e56520b0fe4206b9ea8049b6add40bfeabcd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 Feb 2016 21:10:33 +0100 Subject: Split models --- lib/poolRequests.js | 61 +++++++++++------------------------------------------ 1 file changed, 12 insertions(+), 49 deletions(-) (limited to 'lib/poolRequests.js') 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 @@ var pluck = require('lodash-node/compat/collection/pluck') var constants = require('../initializers/constants') - var database = require('../initializers/database') var logger = require('../helpers/logger') - var PodsDB = database.PodsDB - var PoolRequestsDB = database.PoolRequestsDB + var Pods = require('../models/pods') + var PoolRequests = require('../models/poolRequests') var utils = require('../helpers/utils') - var VideosDB = database.VideosDB + var Videos = require('../models/videos') var timer = null var poolRequests = { activate: activate, - addToPoolRequests: addToPoolRequests, deactivate: deactivate, forceSend: forceSend } @@ -36,30 +34,6 @@ timer = setInterval(makePoolRequests, constants.INTERVAL) } - function addToPoolRequests (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 - } - - // 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) - }) - } - }) - } - // --------------------------------------------------------------------------- module.exports = poolRequests @@ -69,7 +43,7 @@ function makePoolRequest (type, requests, callback) { if (!callback) callback = function () {} - PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) { + Pods.list(function (err, pods) { if (err) throw err var params = { @@ -116,7 +90,7 @@ function makePoolRequests () { logger.info('Making pool requests to friends.') - PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, function (err, pool_requests) { + PoolRequests.list(function (err, pool_requests) { if (err) throw err if (pool_requests.length === 0) return @@ -150,7 +124,7 @@ makePoolRequest('add', requests.add.requests, function (err) { if (err) logger.error('Errors when sent add pool requests.', { error: err }) - removePoolRequestsFromDB(requests.add.ids) + PoolRequests.removeRequests(requests.add.ids) }) } @@ -159,7 +133,7 @@ makePoolRequest('remove', requests.remove.requests, function (err) { if (err) logger.error('Errors when sent remove pool requests.', { error: err }) - removePoolRequestsFromDB(requests.remove.ids) + PoolRequests.removeRequests(requests.remove.ids) }) } }) @@ -167,7 +141,7 @@ } function removeBadPods () { - PodsDB.find({ score: 0 }, { _id: 1, url: 1 }, function (err, pods) { + Pods.findBadPods(function (err, pods) { if (err) throw err if (pods.length === 0) return @@ -175,12 +149,12 @@ var urls = pluck(pods, 'url') var ids = pluck(pods, '_id') - VideosDB.remove({ podUrl: { $in: urls } }, function (err, r) { + Videos.removeAllRemotesOf(urls, function (err, r) { if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err }) var videos_removed = r.result.n logger.info('Removed %d videos.', videos_removed) - PodsDB.remove({ _id: { $in: ids } }, function (err, r) { + Pods.removeAllByIds(ids, function (err, r) { if (err) logger.error('Cannot remove bad pods.', { error: err }) var pods_removed = r.result.n @@ -190,22 +164,11 @@ }) } - function removePoolRequestsFromDB (ids) { - PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { - if (err) { - logger.error('Cannot remove requests from the pool requests database.', { error: err }) - return - } - - logger.info('Pool requests flushed.') - }) - } - function updatePodsScore (good_pods, bad_pods) { logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) - PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec() - PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) { + Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS) + Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { if (err) throw err removeBadPods() }) -- cgit v1.2.3