From 80a6c9e76fda57f01e37fe4620771ae70738a211 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 21 Oct 2016 11:20:45 +0200 Subject: Server: pod removing refractoring --- server/models/pods.js | 14 ++++++++++++++ server/models/request.js | 49 +++--------------------------------------------- server/models/video.js | 6 +++--- 3 files changed, 20 insertions(+), 49 deletions(-) (limited to 'server/models') diff --git a/server/models/pods.js b/server/models/pods.js index 4020a9603..6ab018c1c 100644 --- a/server/models/pods.js +++ b/server/models/pods.js @@ -1,11 +1,14 @@ 'use strict' +const each = require('async/each') const mongoose = require('mongoose') const map = require('lodash/map') const validator = require('express-validator').validator const constants = require('../initializers/constants') +const Video = mongoose.model('Video') + // --------------------------------------------------------------------------- const PodSchema = mongoose.Schema({ @@ -51,6 +54,17 @@ PodSchema.pre('save', function (next) { }) }) +PodSchema.pre('remove', function (next) { + // Remove the videos owned by this pod too + Video.listByUrl(this.url, function (err, videos) { + if (err) return next(err) + + each(videos, function (video, callbackEach) { + video.remove(callbackEach) + }, next) + }) +}) + const Pod = mongoose.model('Pod', PodSchema) // ------------------------------ METHODS ------------------------------ diff --git a/server/models/request.js b/server/models/request.js index 1a99dd2ea..f11c20b52 100644 --- a/server/models/request.js +++ b/server/models/request.js @@ -2,7 +2,6 @@ const each = require('async/each') const eachLimit = require('async/eachLimit') -const map = require('lodash/map') const mongoose = require('mongoose') const waterfall = require('async/waterfall') @@ -11,7 +10,6 @@ const logger = require('../helpers/logger') const requests = require('../helpers/requests') const Pod = mongoose.model('Pod') -const Video = mongoose.model('Video') let timer = null let lastRequestTimestamp = 0 @@ -218,54 +216,13 @@ function removeBadPods () { }) }, - function listVideosOfTheseBadPods (pods, callback) { - if (pods.length === 0) return callback(null) - - const urls = map(pods, 'url') - - Video.listByUrls(urls, function (err, videosList) { - if (err) { - logger.error('Cannot list videos urls.', { error: err, urls: urls }) - return callback(null, pods, []) - } - - return callback(null, pods, videosList) - }) - }, - - function removeVideosOfTheseBadPods (pods, videosList, callback) { - // We don't have to remove pods, skip - if (typeof pods === 'function') { - callback = pods - return callback(null) - } - - each(videosList, function (video, callbackEach) { - video.remove(callbackEach) - }, function (err) { - if (err) { - // Don't stop the process - logger.error('Error while removing videos of bad pods.', { error: err }) - return - } - - return callback(null, pods) - }) - }, - - function removeBadPodsFromDB (pods, callback) { - // We don't have to remove pods, skip - if (typeof pods === 'function') { - callback = pods - return callback(null) - } + function removeTheseBadPods (pods, callback) { + if (pods.length === 0) return callback(null, 0) each(pods, function (pod, callbackEach) { pod.remove(callbackEach) }, function (err) { - if (err) return callback(err) - - return callback(null, pods.length) + return callback(err, pods.length) }) } ], function (err, numberOfPodsRemoved) { diff --git a/server/models/video.js b/server/models/video.js index 4b941a40c..1feefe24f 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -57,7 +57,7 @@ VideoSchema.statics = { getDurationFromFile, listForApi, listByUrlAndMagnet, - listByUrls, + listByUrl, listOwned, listOwnedByAuthor, listRemotes, @@ -218,8 +218,8 @@ function listByUrlAndMagnet (fromUrl, magnetUri, callback) { this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback) } -function listByUrls (fromUrls, callback) { - this.find({ podUrl: { $in: fromUrls } }, callback) +function listByUrl (fromUrl, callback) { + this.find({ podUrl: fromUrl }, callback) } function listOwned (callback) { -- cgit v1.2.3