Pod model refractoring -> use mongoose api

pull/10/head
Chocobozzz 2016-06-30 22:39:08 +02:00
parent d14b3e37a2
commit a3ee6fa22d
8 changed files with 111 additions and 99 deletions

View File

@ -7,12 +7,12 @@ const mongoose = require('mongoose')
const logger = require('../../../helpers/logger') const logger = require('../../../helpers/logger')
const friends = require('../../../lib/friends') const friends = require('../../../lib/friends')
const middlewares = require('../../../middlewares') const middlewares = require('../../../middlewares')
const Pods = require('../../../models/pods')
const oAuth2 = middlewares.oauth2 const oAuth2 = middlewares.oauth2
const reqValidator = middlewares.reqValidators.pods const reqValidator = middlewares.reqValidators.pods
const signatureValidator = middlewares.reqValidators.remote.signature const signatureValidator = middlewares.reqValidators.remote.signature
const router = express.Router() const router = express.Router()
const Pod = mongoose.model('Pod')
const Video = mongoose.model('Video') const Video = mongoose.model('Video')
router.get('/', listPodsUrl) router.get('/', listPodsUrl)
@ -33,7 +33,11 @@ function addPods (req, res, next) {
async.waterfall([ async.waterfall([
function addPod (callback) { function addPod (callback) {
Pods.add(informations, callback) const pod = new Pod(informations)
pod.save(function (err, podCreated) {
// Be sure about the number of parameters for the callback
return callback(err, podCreated)
})
}, },
function sendMyVideos (podCreated, callback) { function sendMyVideos (podCreated, callback) {
@ -60,7 +64,7 @@ function addPods (req, res, next) {
} }
function listPodsUrl (req, res, next) { function listPodsUrl (req, res, next) {
Pods.listAllUrls(function (err, podsUrlList) { Pod.listOnlyUrls(function (err, podsUrlList) {
if (err) return next(err) if (err) return next(err)
res.json(podsUrlList) res.json(podsUrlList)
@ -79,8 +83,13 @@ function removePods (req, res, next) {
const url = req.body.signature.url const url = req.body.signature.url
async.waterfall([ async.waterfall([
function (callback) { function loadPod (callback) {
Pods.remove(url, function (err) { Pod.loadByUrl(url, callback)
},
function removePod (pod, callback) {
pod.remove(function (err) {
// Be sure we only return one argument in the callback
return callback(err) return callback(err)
}) })
}, },

View File

@ -85,6 +85,7 @@ function isVideoNameValid (value) {
} }
function isVideoPodUrlValid (value) { function isVideoPodUrlValid (value) {
// TODO: set options (TLD...)
return validator.isURL(value) return validator.isURL(value)
} }

View File

@ -4,7 +4,10 @@
const API_VERSION = 'v1' const API_VERSION = 'v1'
// Score a pod has when we create it as a friend // Score a pod has when we create it as a friend
let FRIEND_BASE_SCORE = 100 const FRIEND_SCORE = {
BASE: 100,
MAX: 1000
}
// Time to wait between requests to the friends (10 min) // Time to wait between requests to the friends (10 min)
let INTERVAL = 600000 let INTERVAL = 600000
@ -54,7 +57,7 @@ const VIDEOS_CONSTRAINTS_FIELDS = {
// Special constants for a test instance // Special constants for a test instance
if (isTestInstance() === true) { if (isTestInstance() === true) {
FRIEND_BASE_SCORE = 20 FRIEND_SCORE.BASE = 20
INTERVAL = 10000 INTERVAL = 10000
VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14 VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14
} }
@ -63,7 +66,7 @@ if (isTestInstance() === true) {
module.exports = { module.exports = {
API_VERSION: API_VERSION, API_VERSION: API_VERSION,
FRIEND_BASE_SCORE: FRIEND_BASE_SCORE, FRIEND_SCORE: FRIEND_SCORE,
INTERVAL: INTERVAL, INTERVAL: INTERVAL,
PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT,
PODS_SCORE: PODS_SCORE, PODS_SCORE: PODS_SCORE,

View File

@ -6,6 +6,7 @@ const mongoose = require('mongoose')
const logger = require('../helpers/logger') const logger = require('../helpers/logger')
// Bootstrap models // Bootstrap models
require('../models/pods')
require('../models/video') require('../models/video')
// Request model needs Video model // Request model needs Video model
require('../models/request') require('../models/request')

View File

@ -9,16 +9,16 @@ const request = require('request')
const constants = require('../initializers/constants') const constants = require('../initializers/constants')
const logger = require('../helpers/logger') const logger = require('../helpers/logger')
const peertubeCrypto = require('../helpers/peertubeCrypto') const peertubeCrypto = require('../helpers/peertubeCrypto')
const Pods = require('../models/pods')
const requests = require('../helpers/requests') const requests = require('../helpers/requests')
const http = config.get('webserver.https') ? 'https' : 'http' const http = config.get('webserver.https') ? 'https' : 'http'
const host = config.get('webserver.host') const host = config.get('webserver.host')
const port = config.get('webserver.port') const port = config.get('webserver.port')
const Pod = mongoose.model('Pod')
const Request = mongoose.model('Request') const Request = mongoose.model('Request')
const Video = mongoose.model('Video') const Video = mongoose.model('Video')
const pods = { const friends = {
addVideoToFriends: addVideoToFriends, addVideoToFriends: addVideoToFriends,
hasFriends: hasFriends, hasFriends: hasFriends,
getMyCertificate: getMyCertificate, getMyCertificate: getMyCertificate,
@ -33,7 +33,7 @@ function addVideoToFriends (video) {
} }
function hasFriends (callback) { function hasFriends (callback) {
Pods.count(function (err, count) { Pod.countAll(function (err, count) {
if (err) return callback(err) if (err) return callback(err)
const hasFriends = (count !== 0) const hasFriends = (count !== 0)
@ -79,7 +79,7 @@ function quitFriends (callback) {
async.waterfall([ async.waterfall([
function getPodsList (callbackAsync) { function getPodsList (callbackAsync) {
return Pods.list(callbackAsync) return Pod.list(callbackAsync)
}, },
function announceIQuitMyFriends (pods, callbackAsync) { function announceIQuitMyFriends (pods, callbackAsync) {
@ -106,7 +106,7 @@ function quitFriends (callback) {
}, },
function removePodsFromDB (callbackAsync) { function removePodsFromDB (callbackAsync) {
Pods.removeAll(function (err) { Pod.removeAll(function (err) {
return callbackAsync(err) return callbackAsync(err)
}) })
}, },
@ -160,7 +160,7 @@ function sendOwnedVideosToPod (podId) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = pods module.exports = friends
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -230,8 +230,12 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
} }
if (res.statusCode === 200) { if (res.statusCode === 200) {
Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err, podCreated) { const podObj = new Pod({ url: pod.url, publicKey: body.cert })
if (err) logger.error('Cannot add friend %s pod.', pod.url) podObj.save(function (err, podCreated) {
if (err) {
logger.error('Cannot add friend %s pod.', pod.url, { error: err })
return callbackEach()
}
// Add our videos to the request scheduler // Add our videos to the request scheduler
sendOwnedVideosToPod(podCreated._id) sendOwnedVideosToPod(podCreated._id)

View File

@ -1,8 +1,10 @@
'use strict' 'use strict'
const logger = require('../helpers/logger') const logger = require('../helpers/logger')
const mongoose = require('mongoose')
const peertubeCrypto = require('../helpers/peertubeCrypto') const peertubeCrypto = require('../helpers/peertubeCrypto')
const Pods = require('../models/pods')
const Pod = mongoose.model('Pod')
const secureMiddleware = { const secureMiddleware = {
decryptBody: decryptBody decryptBody: decryptBody
@ -10,7 +12,7 @@ const secureMiddleware = {
function decryptBody (req, res, next) { function decryptBody (req, res, next) {
const url = req.body.signature.url const url = req.body.signature.url
Pods.findByUrl(url, function (err, pod) { Pod.loadByUrl(url, function (err, pod) {
if (err) { if (err) {
logger.error('Cannot get signed url in decryptBody.', { error: err }) logger.error('Cannot get signed url in decryptBody.', { error: err })
return res.sendStatus(500) return res.sendStatus(500)

View File

@ -2,107 +2,89 @@
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 constants = require('../initializers/constants') const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const podsSchema = mongoose.Schema({ const PodSchema = mongoose.Schema({
url: String, url: String,
publicKey: 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 = { PodSchema.statics = {
add: add, countAll: countAll,
count: count,
findById: findById,
findByUrl: findByUrl,
findBadPods: findBadPods,
incrementScores: incrementScores, incrementScores: incrementScores,
list: list, list: list,
listAllIds: listAllIds, listAllIds: listAllIds,
listAllUrls: listAllUrls, listOnlyUrls: listOnlyUrls,
remove: remove, listBadPods: listBadPods,
removeAll: removeAll, load: load,
removeAllByIds: removeAllByIds loadByUrl: loadByUrl,
removeAll: removeAll
} }
// TODO: check if the pod is not already a friend PodSchema.pre('save', function (next) {
function add (data, callback) { const self = this
if (!callback) callback = function () {}
const params = {
url: data.url,
publicKey: data.publicKey,
score: constants.FRIEND_BASE_SCORE
}
PodsDB.create(params, callback) Pod.loadByUrl(this.url, function (err, pod) {
} if (err) return next(err)
function count (callback) { if (pod) return next(new Error('Pod already exists.'))
return PodsDB.count(callback)
}
function findBadPods (callback) { self.score = constants.FRIEND_SCORE.BASE
PodsDB.find({ score: 0 }, callback) return next()
} })
})
function findById (id, callback) { const Pod = mongoose.model('Pod', PodSchema)
PodsDB.findById(id, callback)
}
function findByUrl (url, callback) { // ------------------------------ Statics ------------------------------
PodsDB.findOne({ url: url }, callback)
function countAll (callback) {
return this.count(callback)
} }
function incrementScores (ids, value, callback) { function incrementScores (ids, value, callback) {
if (!callback) callback = function () {} 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) { function list (callback) {
PodsDB.find(function (err, podsList) { return this.find(callback)
if (err) {
logger.error('Cannot get the list of the pods.')
return callback(err)
}
return callback(null, podsList)
})
} }
function listAllIds (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) if (err) return callback(err)
return callback(null, map(pods, '_id')) return callback(null, map(pods, '_id'))
}) })
} }
function listAllUrls (callback) { function listOnlyUrls (callback) {
return PodsDB.find({}, { _id: 0, url: 1 }, callback) return this.find({}, { _id: 0, url: 1 }, callback)
} }
function remove (url, callback) { function listBadPods (callback) {
if (!callback) callback = function () {} return this.find({ score: 0 }, callback)
PodsDB.remove({ url: url }, callback) }
function load (id, callback) {
return this.findById(id, callback)
}
function loadByUrl (url, callback) {
return this.findOne({ url: url }, callback)
} }
function removeAll (callback) { function removeAll (callback) {
if (!callback) callback = function () {} return this.remove({}, callback)
PodsDB.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 constants = require('../initializers/constants')
const logger = require('../helpers/logger') const logger = require('../helpers/logger')
const Pods = require('../models/pods')
const requests = require('../helpers/requests') const requests = require('../helpers/requests')
const Pod = mongoose.model('Pod')
const Video = mongoose.model('Video') const Video = mongoose.model('Video')
let timer = null let timer = null
@ -31,7 +31,7 @@ RequestSchema.pre('save', function (next) {
const self = this const self = this
if (self.to.length === 0) { if (self.to.length === 0) {
Pods.listAllIds(function (err, podIds) { Pod.listAllIds(function (err, podIds) {
if (err) return next(err) if (err) return next(err)
// No friends // No friends
@ -140,7 +140,7 @@ function makeRequests () {
const requestToMake = requestsToMake[toPodId] const requestToMake = requestsToMake[toPodId]
// FIXME: mongodb request inside a loop :/ // FIXME: mongodb request inside a loop :/
Pods.findById(toPodId, function (err, toPod) { Pod.load(toPodId, function (err, toPod) {
if (err) { if (err) {
logger.error('Error finding pod by id.', { err: err }) logger.error('Error finding pod by id.', { err: err })
return callbackEach() return callbackEach()
@ -185,7 +185,7 @@ function makeRequests () {
function removeBadPods () { function removeBadPods () {
async.waterfall([ async.waterfall([
function findBadPods (callback) { function findBadPods (callback) {
Pods.findBadPods(function (err, pods) { Pod.listBadPods(function (err, pods) {
if (err) { if (err) {
logger.error('Cannot find bad pods.', { error: err }) logger.error('Cannot find bad pods.', { error: err })
return callback(err) return callback(err)
@ -199,21 +199,23 @@ function removeBadPods () {
if (pods.length === 0) return callback(null) if (pods.length === 0) return callback(null)
const urls = map(pods, 'url') const urls = map(pods, 'url')
const ids = map(pods, '_id')
Video.listByUrls(urls, function (err, videosList) { Video.listByUrls(urls, function (err, videosList) {
if (err) { if (err) {
logger.error('Cannot list videos urls.', { error: err, urls: urls }) 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 // 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) { async.each(videosList, function (video, callbackEach) {
video.remove(callbackEach) video.remove(callbackEach)
@ -224,22 +226,30 @@ function removeBadPods () {
return 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 // 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) { if (err) {
logger.error('Cannot remove bad pods.', { error: err }) logger.error('Cannot remove bad pods.', { error: err })
} else if (removeResult) { } else if (numberOfPodsRemoved) {
const podsRemoved = removeResult.result.n logger.info('Removed %d pods.', numberOfPodsRemoved)
logger.info('Removed %d pods.', podsRemoved)
} else { } else {
logger.info('No need to remove bad pods.') logger.info('No need to remove bad pods.')
} }
@ -249,11 +259,11 @@ function removeBadPods () {
function updatePodsScore (goodPods, badPods) { function updatePodsScore (goodPods, badPods) {
logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) 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.') 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.') if (err) logger.error('Cannot decrement scores of bad pods.')
removeBadPods() removeBadPods()
}) })