aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
Diffstat (limited to 'server/models')
-rw-r--r--server/models/pods.js14
-rw-r--r--server/models/request.js49
-rw-r--r--server/models/video.js6
3 files changed, 20 insertions, 49 deletions
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 @@
1'use strict' 1'use strict'
2 2
3const each = require('async/each')
3const mongoose = require('mongoose') 4const mongoose = require('mongoose')
4const map = require('lodash/map') 5const map = require('lodash/map')
5const validator = require('express-validator').validator 6const validator = require('express-validator').validator
6 7
7const constants = require('../initializers/constants') 8const constants = require('../initializers/constants')
8 9
10const Video = mongoose.model('Video')
11
9// --------------------------------------------------------------------------- 12// ---------------------------------------------------------------------------
10 13
11const PodSchema = mongoose.Schema({ 14const PodSchema = mongoose.Schema({
@@ -51,6 +54,17 @@ PodSchema.pre('save', function (next) {
51 }) 54 })
52}) 55})
53 56
57PodSchema.pre('remove', function (next) {
58 // Remove the videos owned by this pod too
59 Video.listByUrl(this.url, function (err, videos) {
60 if (err) return next(err)
61
62 each(videos, function (video, callbackEach) {
63 video.remove(callbackEach)
64 }, next)
65 })
66})
67
54const Pod = mongoose.model('Pod', PodSchema) 68const Pod = mongoose.model('Pod', PodSchema)
55 69
56// ------------------------------ METHODS ------------------------------ 70// ------------------------------ 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 @@
2 2
3const each = require('async/each') 3const each = require('async/each')
4const eachLimit = require('async/eachLimit') 4const eachLimit = require('async/eachLimit')
5const map = require('lodash/map')
6const mongoose = require('mongoose') 5const mongoose = require('mongoose')
7const waterfall = require('async/waterfall') 6const waterfall = require('async/waterfall')
8 7
@@ -11,7 +10,6 @@ const logger = require('../helpers/logger')
11const requests = require('../helpers/requests') 10const requests = require('../helpers/requests')
12 11
13const Pod = mongoose.model('Pod') 12const Pod = mongoose.model('Pod')
14const Video = mongoose.model('Video')
15 13
16let timer = null 14let timer = null
17let lastRequestTimestamp = 0 15let lastRequestTimestamp = 0
@@ -218,54 +216,13 @@ function removeBadPods () {
218 }) 216 })
219 }, 217 },
220 218
221 function listVideosOfTheseBadPods (pods, callback) { 219 function removeTheseBadPods (pods, callback) {
222 if (pods.length === 0) return callback(null) 220 if (pods.length === 0) return callback(null, 0)
223
224 const urls = map(pods, 'url')
225
226 Video.listByUrls(urls, function (err, videosList) {
227 if (err) {
228 logger.error('Cannot list videos urls.', { error: err, urls: urls })
229 return callback(null, pods, [])
230 }
231
232 return callback(null, pods, videosList)
233 })
234 },
235
236 function removeVideosOfTheseBadPods (pods, videosList, callback) {
237 // We don't have to remove pods, skip
238 if (typeof pods === 'function') {
239 callback = pods
240 return callback(null)
241 }
242
243 each(videosList, function (video, callbackEach) {
244 video.remove(callbackEach)
245 }, function (err) {
246 if (err) {
247 // Don't stop the process
248 logger.error('Error while removing videos of bad pods.', { error: err })
249 return
250 }
251
252 return callback(null, pods)
253 })
254 },
255
256 function removeBadPodsFromDB (pods, callback) {
257 // We don't have to remove pods, skip
258 if (typeof pods === 'function') {
259 callback = pods
260 return callback(null)
261 }
262 221
263 each(pods, function (pod, callbackEach) { 222 each(pods, function (pod, callbackEach) {
264 pod.remove(callbackEach) 223 pod.remove(callbackEach)
265 }, function (err) { 224 }, function (err) {
266 if (err) return callback(err) 225 return callback(err, pods.length)
267
268 return callback(null, pods.length)
269 }) 226 })
270 } 227 }
271 ], function (err, numberOfPodsRemoved) { 228 ], 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 = {
57 getDurationFromFile, 57 getDurationFromFile,
58 listForApi, 58 listForApi,
59 listByUrlAndMagnet, 59 listByUrlAndMagnet,
60 listByUrls, 60 listByUrl,
61 listOwned, 61 listOwned,
62 listOwnedByAuthor, 62 listOwnedByAuthor,
63 listRemotes, 63 listRemotes,
@@ -218,8 +218,8 @@ function listByUrlAndMagnet (fromUrl, magnetUri, callback) {
218 this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback) 218 this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback)
219} 219}
220 220
221function listByUrls (fromUrls, callback) { 221function listByUrl (fromUrl, callback) {
222 this.find({ podUrl: { $in: fromUrls } }, callback) 222 this.find({ podUrl: fromUrl }, callback)
223} 223}
224 224
225function listOwned (callback) { 225function listOwned (callback) {