aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/v1/pods.js24
-rw-r--r--server/initializers/database.js3
-rw-r--r--server/lib/friends.js20
-rw-r--r--server/models/pods.js14
-rw-r--r--server/models/request.js49
-rw-r--r--server/models/video.js6
6 files changed, 27 insertions, 89 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js
index 8ffade578..2f4621327 100644
--- a/server/controllers/api/v1/pods.js
+++ b/server/controllers/api/v1/pods.js
@@ -1,6 +1,5 @@
1'use strict' 1'use strict'
2 2
3const each = require('async/each')
4const express = require('express') 3const express = require('express')
5const mongoose = require('mongoose') 4const mongoose = require('mongoose')
6const waterfall = require('async/waterfall') 5const waterfall = require('async/waterfall')
@@ -17,7 +16,6 @@ const signatureValidator = middlewares.validators.remote.signature
17 16
18const router = express.Router() 17const router = express.Router()
19const Pod = mongoose.model('Pod') 18const Pod = mongoose.model('Pod')
20const Video = mongoose.model('Video')
21 19
22router.get('/', listPods) 20router.get('/', listPods)
23router.post('/', 21router.post('/',
@@ -117,27 +115,7 @@ function removePods (req, res, next) {
117 }, 115 },
118 116
119 function removePod (pod, callback) { 117 function removePod (pod, callback) {
120 pod.remove(function (err) { 118 pod.remove(callback)
121 // Be sure we only return one argument in the callback
122 return callback(err)
123 })
124 },
125
126 function (callback) {
127 Video.listByUrls([ url ], function (err, videosList) {
128 if (err) {
129 logger.error('Cannot list videos from url.', { error: err })
130 return callback(err)
131 }
132
133 return callback(null, videosList)
134 })
135 },
136
137 function removeTheRemoteVideos (videosList, callback) {
138 each(videosList, function (video, callbackEach) {
139 video.remove(callbackEach)
140 }, callback)
141 } 119 }
142 ], function (err) { 120 ], function (err) {
143 if (err) return next(err) 121 if (err) return next(err)
diff --git a/server/initializers/database.js b/server/initializers/database.js
index 45c8a240d..632040b81 100644
--- a/server/initializers/database.js
+++ b/server/initializers/database.js
@@ -10,9 +10,10 @@ require('../models/application')
10require('../models/oauth-token') 10require('../models/oauth-token')
11require('../models/user') 11require('../models/user')
12require('../models/oauth-client') 12require('../models/oauth-client')
13require('../models/pods')
14require('../models/video') 13require('../models/video')
15// Request model needs Video model 14// Request model needs Video model
15require('../models/pods')
16// Request model needs Pod model
16require('../models/request') 17require('../models/request')
17 18
18const database = { 19const database = {
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 556d2e773..55cecc53e 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -97,25 +97,13 @@ function quitFriends (callback) {
97 // Don't stop the process 97 // Don't stop the process
98 } 98 }
99 99
100 return callbackAsync() 100 return callbackAsync(null, pods)
101 }) 101 })
102 }, 102 },
103 103
104 function removePodsFromDB (callbackAsync) { 104 function removePodsFromDB (pods, callbackAsync) {
105 Pod.removeAll(function (err) { 105 each(pods, function (pod, callbackEach) {
106 return callbackAsync(err) 106 pod.remove(callbackEach)
107 })
108 },
109
110 function listRemoteVideos (callbackAsync) {
111 logger.info('Broke friends, so sad :(')
112
113 Video.listRemotes(callbackAsync)
114 },
115
116 function removeTheRemoteVideos (videosList, callbackAsync) {
117 each(videosList, function (video, callbackEach) {
118 video.remove(callbackEach)
119 }, callbackAsync) 107 }, callbackAsync)
120 } 108 }
121 ], function (err) { 109 ], function (err) {
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) {