diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-21 11:20:45 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-21 11:20:45 +0200 |
commit | 80a6c9e76fda57f01e37fe4620771ae70738a211 (patch) | |
tree | 7381a94285c11d954efca2be8103c8b86df3e832 | |
parent | 792b893ed414301c060391ed8a00368d68688236 (diff) | |
download | PeerTube-80a6c9e76fda57f01e37fe4620771ae70738a211.tar.gz PeerTube-80a6c9e76fda57f01e37fe4620771ae70738a211.tar.zst PeerTube-80a6c9e76fda57f01e37fe4620771ae70738a211.zip |
Server: pod removing refractoring
-rw-r--r-- | server/controllers/api/v1/pods.js | 24 | ||||
-rw-r--r-- | server/initializers/database.js | 3 | ||||
-rw-r--r-- | server/lib/friends.js | 20 | ||||
-rw-r--r-- | server/models/pods.js | 14 | ||||
-rw-r--r-- | server/models/request.js | 49 | ||||
-rw-r--r-- | server/models/video.js | 6 |
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 | ||
3 | const each = require('async/each') | ||
4 | const express = require('express') | 3 | const express = require('express') |
5 | const mongoose = require('mongoose') | 4 | const mongoose = require('mongoose') |
6 | const waterfall = require('async/waterfall') | 5 | const waterfall = require('async/waterfall') |
@@ -17,7 +16,6 @@ const signatureValidator = middlewares.validators.remote.signature | |||
17 | 16 | ||
18 | const router = express.Router() | 17 | const router = express.Router() |
19 | const Pod = mongoose.model('Pod') | 18 | const Pod = mongoose.model('Pod') |
20 | const Video = mongoose.model('Video') | ||
21 | 19 | ||
22 | router.get('/', listPods) | 20 | router.get('/', listPods) |
23 | router.post('/', | 21 | router.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') | |||
10 | require('../models/oauth-token') | 10 | require('../models/oauth-token') |
11 | require('../models/user') | 11 | require('../models/user') |
12 | require('../models/oauth-client') | 12 | require('../models/oauth-client') |
13 | require('../models/pods') | ||
14 | require('../models/video') | 13 | require('../models/video') |
15 | // Request model needs Video model | 14 | // Request model needs Video model |
15 | require('../models/pods') | ||
16 | // Request model needs Pod model | ||
16 | require('../models/request') | 17 | require('../models/request') |
17 | 18 | ||
18 | const database = { | 19 | const 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 | ||
3 | const each = require('async/each') | ||
3 | const mongoose = require('mongoose') | 4 | const mongoose = require('mongoose') |
4 | const map = require('lodash/map') | 5 | const map = require('lodash/map') |
5 | const validator = require('express-validator').validator | 6 | const validator = require('express-validator').validator |
6 | 7 | ||
7 | const constants = require('../initializers/constants') | 8 | const constants = require('../initializers/constants') |
8 | 9 | ||
10 | const Video = mongoose.model('Video') | ||
11 | |||
9 | // --------------------------------------------------------------------------- | 12 | // --------------------------------------------------------------------------- |
10 | 13 | ||
11 | const PodSchema = mongoose.Schema({ | 14 | const PodSchema = mongoose.Schema({ |
@@ -51,6 +54,17 @@ PodSchema.pre('save', function (next) { | |||
51 | }) | 54 | }) |
52 | }) | 55 | }) |
53 | 56 | ||
57 | PodSchema.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 | |||
54 | const Pod = mongoose.model('Pod', PodSchema) | 68 | const 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 | ||
3 | const each = require('async/each') | 3 | const each = require('async/each') |
4 | const eachLimit = require('async/eachLimit') | 4 | const eachLimit = require('async/eachLimit') |
5 | const map = require('lodash/map') | ||
6 | const mongoose = require('mongoose') | 5 | const mongoose = require('mongoose') |
7 | const waterfall = require('async/waterfall') | 6 | const waterfall = require('async/waterfall') |
8 | 7 | ||
@@ -11,7 +10,6 @@ const logger = require('../helpers/logger') | |||
11 | const requests = require('../helpers/requests') | 10 | const requests = require('../helpers/requests') |
12 | 11 | ||
13 | const Pod = mongoose.model('Pod') | 12 | const Pod = mongoose.model('Pod') |
14 | const Video = mongoose.model('Video') | ||
15 | 13 | ||
16 | let timer = null | 14 | let timer = null |
17 | let lastRequestTimestamp = 0 | 15 | let 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 | ||
221 | function listByUrls (fromUrls, callback) { | 221 | function listByUrl (fromUrl, callback) { |
222 | this.find({ podUrl: { $in: fromUrls } }, callback) | 222 | this.find({ podUrl: fromUrl }, callback) |
223 | } | 223 | } |
224 | 224 | ||
225 | function listOwned (callback) { | 225 | function listOwned (callback) { |