diff options
Diffstat (limited to 'server/models/request.js')
-rw-r--r-- | server/models/request.js | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/server/models/request.js b/server/models/request.js index 2a407388a..db6ad5409 100644 --- a/server/models/request.js +++ b/server/models/request.js | |||
@@ -6,9 +6,9 @@ const mongoose = require('mongoose') | |||
6 | 6 | ||
7 | const constants = require('../initializers/constants') | 7 | const constants = require('../initializers/constants') |
8 | const logger = require('../helpers/logger') | 8 | const logger = require('../helpers/logger') |
9 | const Pods = require('../models/pods') | ||
10 | const requests = require('../helpers/requests') | 9 | const requests = require('../helpers/requests') |
11 | 10 | ||
11 | const Pod = mongoose.model('Pod') | ||
12 | const Video = mongoose.model('Video') | 12 | const Video = mongoose.model('Video') |
13 | 13 | ||
14 | let timer = null | 14 | let timer = null |
@@ -31,7 +31,7 @@ RequestSchema.pre('save', function (next) { | |||
31 | const self = this | 31 | const self = this |
32 | 32 | ||
33 | if (self.to.length === 0) { | 33 | if (self.to.length === 0) { |
34 | Pods.listAllIds(function (err, podIds) { | 34 | Pod.listAllIds(function (err, podIds) { |
35 | if (err) return next(err) | 35 | if (err) return next(err) |
36 | 36 | ||
37 | // No friends | 37 | // No friends |
@@ -140,7 +140,7 @@ function makeRequests () { | |||
140 | const requestToMake = requestsToMake[toPodId] | 140 | const requestToMake = requestsToMake[toPodId] |
141 | 141 | ||
142 | // FIXME: mongodb request inside a loop :/ | 142 | // FIXME: mongodb request inside a loop :/ |
143 | Pods.findById(toPodId, function (err, toPod) { | 143 | Pod.load(toPodId, function (err, toPod) { |
144 | if (err) { | 144 | if (err) { |
145 | logger.error('Error finding pod by id.', { err: err }) | 145 | logger.error('Error finding pod by id.', { err: err }) |
146 | return callbackEach() | 146 | return callbackEach() |
@@ -185,7 +185,7 @@ function makeRequests () { | |||
185 | function removeBadPods () { | 185 | function removeBadPods () { |
186 | async.waterfall([ | 186 | async.waterfall([ |
187 | function findBadPods (callback) { | 187 | function findBadPods (callback) { |
188 | Pods.findBadPods(function (err, pods) { | 188 | Pod.listBadPods(function (err, pods) { |
189 | if (err) { | 189 | if (err) { |
190 | logger.error('Cannot find bad pods.', { error: err }) | 190 | logger.error('Cannot find bad pods.', { error: err }) |
191 | return callback(err) | 191 | return callback(err) |
@@ -199,21 +199,23 @@ function removeBadPods () { | |||
199 | if (pods.length === 0) return callback(null) | 199 | if (pods.length === 0) return callback(null) |
200 | 200 | ||
201 | const urls = map(pods, 'url') | 201 | const urls = map(pods, 'url') |
202 | const ids = map(pods, '_id') | ||
203 | 202 | ||
204 | Video.listByUrls(urls, function (err, videosList) { | 203 | Video.listByUrls(urls, function (err, videosList) { |
205 | if (err) { | 204 | if (err) { |
206 | logger.error('Cannot list videos urls.', { error: err, urls: urls }) | 205 | logger.error('Cannot list videos urls.', { error: err, urls: urls }) |
207 | return callback(null, ids, []) | 206 | return callback(null, pods, []) |
208 | } | 207 | } |
209 | 208 | ||
210 | return callback(null, ids, videosList) | 209 | return callback(null, pods, videosList) |
211 | }) | 210 | }) |
212 | }, | 211 | }, |
213 | 212 | ||
214 | function removeVideosOfTheseBadPods (podIds, videosList, callback) { | 213 | function removeVideosOfTheseBadPods (pods, videosList, callback) { |
215 | // We don't have to remove pods, skip | 214 | // We don't have to remove pods, skip |
216 | if (typeof podIds === 'function') return podIds(null) | 215 | if (typeof pods === 'function') { |
216 | callback = pods | ||
217 | return callback(null) | ||
218 | } | ||
217 | 219 | ||
218 | async.each(videosList, function (video, callbackEach) { | 220 | async.each(videosList, function (video, callbackEach) { |
219 | video.remove(callbackEach) | 221 | video.remove(callbackEach) |
@@ -224,22 +226,30 @@ function removeBadPods () { | |||
224 | return | 226 | return |
225 | } | 227 | } |
226 | 228 | ||
227 | return callback(null, podIds) | 229 | return callback(null, pods) |
228 | }) | 230 | }) |
229 | }, | 231 | }, |
230 | 232 | ||
231 | function removeBadPodsFromDB (podIds, callback) { | 233 | function removeBadPodsFromDB (pods, callback) { |
232 | // We don't have to remove pods, skip | 234 | // We don't have to remove pods, skip |
233 | if (typeof podIds === 'function') return podIds(null) | 235 | if (typeof pods === 'function') { |
236 | callback = pods | ||
237 | return callback(null) | ||
238 | } | ||
239 | |||
240 | async.each(pods, function (pod, callbackEach) { | ||
241 | pod.remove(callbackEach) | ||
242 | }, function (err) { | ||
243 | if (err) return callback(err) | ||
234 | 244 | ||
235 | Pods.removeAllByIds(podIds, callback) | 245 | return callback(null, pods.length) |
246 | }) | ||
236 | } | 247 | } |
237 | ], function (err, removeResult) { | 248 | ], function (err, numberOfPodsRemoved) { |
238 | if (err) { | 249 | if (err) { |
239 | logger.error('Cannot remove bad pods.', { error: err }) | 250 | logger.error('Cannot remove bad pods.', { error: err }) |
240 | } else if (removeResult) { | 251 | } else if (numberOfPodsRemoved) { |
241 | const podsRemoved = removeResult.result.n | 252 | logger.info('Removed %d pods.', numberOfPodsRemoved) |
242 | logger.info('Removed %d pods.', podsRemoved) | ||
243 | } else { | 253 | } else { |
244 | logger.info('No need to remove bad pods.') | 254 | logger.info('No need to remove bad pods.') |
245 | } | 255 | } |
@@ -249,11 +259,11 @@ function removeBadPods () { | |||
249 | function updatePodsScore (goodPods, badPods) { | 259 | function updatePodsScore (goodPods, badPods) { |
250 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) | 260 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) |
251 | 261 | ||
252 | Pods.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { | 262 | Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { |
253 | if (err) logger.error('Cannot increment scores of good pods.') | 263 | if (err) logger.error('Cannot increment scores of good pods.') |
254 | }) | 264 | }) |
255 | 265 | ||
256 | Pods.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { | 266 | Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { |
257 | if (err) logger.error('Cannot decrement scores of bad pods.') | 267 | if (err) logger.error('Cannot decrement scores of bad pods.') |
258 | removeBadPods() | 268 | removeBadPods() |
259 | }) | 269 | }) |