1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 19:42:24 +02:00

Pod model refractoring -> use mongoose api

This commit is contained in:
Chocobozzz 2016-06-30 22:39:08 +02:00
parent d14b3e37a2
commit a3ee6fa22d
8 changed files with 111 additions and 99 deletions

View file

@ -2,107 +2,89 @@
const mongoose = require('mongoose')
const map = require('lodash/map')
const validator = require('express-validator').validator
const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
// ---------------------------------------------------------------------------
const podsSchema = mongoose.Schema({
const PodSchema = mongoose.Schema({
url: String,
publicKey: String,
score: { type: Number, max: constants.FRIEND_BASE_SCORE }
score: { type: Number, max: constants.FRIEND_SCORE.MAX }
})
const PodsDB = mongoose.model('pods', podsSchema)
// ---------------------------------------------------------------------------
// TODO: set options (TLD...)
PodSchema.path('url').validate(validator.isURL)
PodSchema.path('publicKey').required(true)
PodSchema.path('score').validate(function (value) { return !isNaN(value) })
const Pods = {
add: add,
count: count,
findById: findById,
findByUrl: findByUrl,
findBadPods: findBadPods,
PodSchema.statics = {
countAll: countAll,
incrementScores: incrementScores,
list: list,
listAllIds: listAllIds,
listAllUrls: listAllUrls,
remove: remove,
removeAll: removeAll,
removeAllByIds: removeAllByIds
listOnlyUrls: listOnlyUrls,
listBadPods: listBadPods,
load: load,
loadByUrl: loadByUrl,
removeAll: removeAll
}
// TODO: check if the pod is not already a friend
function add (data, callback) {
if (!callback) callback = function () {}
const params = {
url: data.url,
publicKey: data.publicKey,
score: constants.FRIEND_BASE_SCORE
}
PodSchema.pre('save', function (next) {
const self = this
PodsDB.create(params, callback)
}
Pod.loadByUrl(this.url, function (err, pod) {
if (err) return next(err)
function count (callback) {
return PodsDB.count(callback)
}
if (pod) return next(new Error('Pod already exists.'))
function findBadPods (callback) {
PodsDB.find({ score: 0 }, callback)
}
self.score = constants.FRIEND_SCORE.BASE
return next()
})
})
function findById (id, callback) {
PodsDB.findById(id, callback)
}
const Pod = mongoose.model('Pod', PodSchema)
function findByUrl (url, callback) {
PodsDB.findOne({ url: url }, callback)
// ------------------------------ Statics ------------------------------
function countAll (callback) {
return this.count(callback)
}
function incrementScores (ids, value, callback) {
if (!callback) callback = function () {}
PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
}
function list (callback) {
PodsDB.find(function (err, podsList) {
if (err) {
logger.error('Cannot get the list of the pods.')
return callback(err)
}
return callback(null, podsList)
})
return this.find(callback)
}
function listAllIds (callback) {
return PodsDB.find({}, { _id: 1 }, function (err, pods) {
return this.find({}, { _id: 1 }, function (err, pods) {
if (err) return callback(err)
return callback(null, map(pods, '_id'))
})
}
function listAllUrls (callback) {
return PodsDB.find({}, { _id: 0, url: 1 }, callback)
function listOnlyUrls (callback) {
return this.find({}, { _id: 0, url: 1 }, callback)
}
function remove (url, callback) {
if (!callback) callback = function () {}
PodsDB.remove({ url: url }, callback)
function listBadPods (callback) {
return this.find({ score: 0 }, callback)
}
function load (id, callback) {
return this.findById(id, callback)
}
function loadByUrl (url, callback) {
return this.findOne({ url: url }, callback)
}
function removeAll (callback) {
if (!callback) callback = function () {}
PodsDB.remove(callback)
return this.remove({}, callback)
}
function removeAllByIds (ids, callback) {
if (!callback) callback = function () {}
PodsDB.remove({ _id: { $in: ids } }, callback)
}
// ---------------------------------------------------------------------------
module.exports = Pods

View file

@ -6,9 +6,9 @@ const mongoose = require('mongoose')
const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
const Pods = require('../models/pods')
const requests = require('../helpers/requests')
const Pod = mongoose.model('Pod')
const Video = mongoose.model('Video')
let timer = null
@ -31,7 +31,7 @@ RequestSchema.pre('save', function (next) {
const self = this
if (self.to.length === 0) {
Pods.listAllIds(function (err, podIds) {
Pod.listAllIds(function (err, podIds) {
if (err) return next(err)
// No friends
@ -140,7 +140,7 @@ function makeRequests () {
const requestToMake = requestsToMake[toPodId]
// FIXME: mongodb request inside a loop :/
Pods.findById(toPodId, function (err, toPod) {
Pod.load(toPodId, function (err, toPod) {
if (err) {
logger.error('Error finding pod by id.', { err: err })
return callbackEach()
@ -185,7 +185,7 @@ function makeRequests () {
function removeBadPods () {
async.waterfall([
function findBadPods (callback) {
Pods.findBadPods(function (err, pods) {
Pod.listBadPods(function (err, pods) {
if (err) {
logger.error('Cannot find bad pods.', { error: err })
return callback(err)
@ -199,21 +199,23 @@ function removeBadPods () {
if (pods.length === 0) return callback(null)
const urls = map(pods, 'url')
const ids = map(pods, '_id')
Video.listByUrls(urls, function (err, videosList) {
if (err) {
logger.error('Cannot list videos urls.', { error: err, urls: urls })
return callback(null, ids, [])
return callback(null, pods, [])
}
return callback(null, ids, videosList)
return callback(null, pods, videosList)
})
},
function removeVideosOfTheseBadPods (podIds, videosList, callback) {
function removeVideosOfTheseBadPods (pods, videosList, callback) {
// We don't have to remove pods, skip
if (typeof podIds === 'function') return podIds(null)
if (typeof pods === 'function') {
callback = pods
return callback(null)
}
async.each(videosList, function (video, callbackEach) {
video.remove(callbackEach)
@ -224,22 +226,30 @@ function removeBadPods () {
return
}
return callback(null, podIds)
return callback(null, pods)
})
},
function removeBadPodsFromDB (podIds, callback) {
function removeBadPodsFromDB (pods, callback) {
// We don't have to remove pods, skip
if (typeof podIds === 'function') return podIds(null)
if (typeof pods === 'function') {
callback = pods
return callback(null)
}
Pods.removeAllByIds(podIds, callback)
async.each(pods, function (pod, callbackEach) {
pod.remove(callbackEach)
}, function (err) {
if (err) return callback(err)
return callback(null, pods.length)
})
}
], function (err, removeResult) {
], function (err, numberOfPodsRemoved) {
if (err) {
logger.error('Cannot remove bad pods.', { error: err })
} else if (removeResult) {
const podsRemoved = removeResult.result.n
logger.info('Removed %d pods.', podsRemoved)
} else if (numberOfPodsRemoved) {
logger.info('Removed %d pods.', numberOfPodsRemoved)
} else {
logger.info('No need to remove bad pods.')
}
@ -249,11 +259,11 @@ function removeBadPods () {
function updatePodsScore (goodPods, badPods) {
logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
Pods.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
if (err) logger.error('Cannot increment scores of good pods.')
})
Pods.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
if (err) logger.error('Cannot decrement scores of bad pods.')
removeBadPods()
})