Server: pod removing refractoring

pull/15/head
Chocobozzz 2016-10-21 11:20:45 +02:00
parent 792b893ed4
commit 80a6c9e76f
6 changed files with 27 additions and 89 deletions

View File

@ -1,6 +1,5 @@
'use strict' 'use strict'
const each = require('async/each')
const express = require('express') const express = require('express')
const mongoose = require('mongoose') const mongoose = require('mongoose')
const waterfall = require('async/waterfall') const waterfall = require('async/waterfall')
@ -17,7 +16,6 @@ const signatureValidator = middlewares.validators.remote.signature
const router = express.Router() const router = express.Router()
const Pod = mongoose.model('Pod') const Pod = mongoose.model('Pod')
const Video = mongoose.model('Video')
router.get('/', listPods) router.get('/', listPods)
router.post('/', router.post('/',
@ -117,27 +115,7 @@ function removePods (req, res, next) {
}, },
function removePod (pod, callback) { function removePod (pod, callback) {
pod.remove(function (err) { pod.remove(callback)
// Be sure we only return one argument in the callback
return callback(err)
})
},
function (callback) {
Video.listByUrls([ url ], function (err, videosList) {
if (err) {
logger.error('Cannot list videos from url.', { error: err })
return callback(err)
}
return callback(null, videosList)
})
},
function removeTheRemoteVideos (videosList, callback) {
each(videosList, function (video, callbackEach) {
video.remove(callbackEach)
}, callback)
} }
], function (err) { ], function (err) {
if (err) return next(err) if (err) return next(err)

View File

@ -10,9 +10,10 @@ require('../models/application')
require('../models/oauth-token') require('../models/oauth-token')
require('../models/user') require('../models/user')
require('../models/oauth-client') require('../models/oauth-client')
require('../models/pods')
require('../models/video') require('../models/video')
// Request model needs Video model // Request model needs Video model
require('../models/pods')
// Request model needs Pod model
require('../models/request') require('../models/request')
const database = { const database = {

View File

@ -97,25 +97,13 @@ function quitFriends (callback) {
// Don't stop the process // Don't stop the process
} }
return callbackAsync() return callbackAsync(null, pods)
}) })
}, },
function removePodsFromDB (callbackAsync) { function removePodsFromDB (pods, callbackAsync) {
Pod.removeAll(function (err) { each(pods, function (pod, callbackEach) {
return callbackAsync(err) pod.remove(callbackEach)
})
},
function listRemoteVideos (callbackAsync) {
logger.info('Broke friends, so sad :(')
Video.listRemotes(callbackAsync)
},
function removeTheRemoteVideos (videosList, callbackAsync) {
each(videosList, function (video, callbackEach) {
video.remove(callbackEach)
}, callbackAsync) }, callbackAsync)
} }
], function (err) { ], function (err) {

View File

@ -1,11 +1,14 @@
'use strict' 'use strict'
const each = require('async/each')
const mongoose = require('mongoose') const mongoose = require('mongoose')
const map = require('lodash/map') const map = require('lodash/map')
const validator = require('express-validator').validator const validator = require('express-validator').validator
const constants = require('../initializers/constants') const constants = require('../initializers/constants')
const Video = mongoose.model('Video')
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const PodSchema = mongoose.Schema({ 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) const Pod = mongoose.model('Pod', PodSchema)
// ------------------------------ METHODS ------------------------------ // ------------------------------ METHODS ------------------------------

View File

@ -2,7 +2,6 @@
const each = require('async/each') const each = require('async/each')
const eachLimit = require('async/eachLimit') const eachLimit = require('async/eachLimit')
const map = require('lodash/map')
const mongoose = require('mongoose') const mongoose = require('mongoose')
const waterfall = require('async/waterfall') const waterfall = require('async/waterfall')
@ -11,7 +10,6 @@ const logger = require('../helpers/logger')
const requests = require('../helpers/requests') const requests = require('../helpers/requests')
const Pod = mongoose.model('Pod') const Pod = mongoose.model('Pod')
const Video = mongoose.model('Video')
let timer = null let timer = null
let lastRequestTimestamp = 0 let lastRequestTimestamp = 0
@ -218,54 +216,13 @@ function removeBadPods () {
}) })
}, },
function listVideosOfTheseBadPods (pods, callback) { function removeTheseBadPods (pods, callback) {
if (pods.length === 0) return callback(null) if (pods.length === 0) return callback(null, 0)
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)
}
each(pods, function (pod, callbackEach) { each(pods, function (pod, callbackEach) {
pod.remove(callbackEach) pod.remove(callbackEach)
}, function (err) { }, function (err) {
if (err) return callback(err) return callback(err, pods.length)
return callback(null, pods.length)
}) })
} }
], function (err, numberOfPodsRemoved) { ], function (err, numberOfPodsRemoved) {

View File

@ -57,7 +57,7 @@ VideoSchema.statics = {
getDurationFromFile, getDurationFromFile,
listForApi, listForApi,
listByUrlAndMagnet, listByUrlAndMagnet,
listByUrls, listByUrl,
listOwned, listOwned,
listOwnedByAuthor, listOwnedByAuthor,
listRemotes, listRemotes,
@ -218,8 +218,8 @@ function listByUrlAndMagnet (fromUrl, magnetUri, callback) {
this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback) this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback)
} }
function listByUrls (fromUrls, callback) { function listByUrl (fromUrl, callback) {
this.find({ podUrl: { $in: fromUrls } }, callback) this.find({ podUrl: fromUrl }, callback)
} }
function listOwned (callback) { function listOwned (callback) {