2016-02-07 11:23:23 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
var mongoose = require('mongoose')
|
|
|
|
|
|
|
|
var constants = require('../initializers/constants')
|
|
|
|
var logger = require('../helpers/logger')
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
var podsSchema = mongoose.Schema({
|
|
|
|
url: String,
|
|
|
|
publicKey: String,
|
|
|
|
score: { type: Number, max: constants.FRIEND_BASE_SCORE }
|
|
|
|
})
|
|
|
|
var PodsDB = mongoose.model('pods', podsSchema)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
var Pods = {
|
|
|
|
add: add,
|
|
|
|
count: count,
|
|
|
|
findByUrl: findByUrl,
|
|
|
|
findBadPods: findBadPods,
|
|
|
|
incrementScores: incrementScores,
|
|
|
|
list: list,
|
|
|
|
remove: remove,
|
|
|
|
removeAll: removeAll,
|
|
|
|
removeAllByIds: removeAllByIds
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check if the pod is not already a friend
|
|
|
|
function add (data, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
var params = {
|
|
|
|
url: data.url,
|
|
|
|
publicKey: data.publicKey,
|
|
|
|
score: constants.FRIEND_BASE_SCORE
|
2015-06-09 17:41:40 +02:00
|
|
|
}
|
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
PodsDB.create(params, callback)
|
|
|
|
}
|
2016-01-23 18:31:58 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function count (callback) {
|
|
|
|
return PodsDB.count(callback)
|
|
|
|
}
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function findBadPods (callback) {
|
|
|
|
PodsDB.find({ score: 0 }, callback)
|
|
|
|
}
|
2016-01-23 18:31:58 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function findByUrl (url, callback) {
|
|
|
|
PodsDB.findOne({ url: url }, callback)
|
|
|
|
}
|
2016-01-23 18:31:58 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function incrementScores (ids, value, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
|
|
|
|
}
|
2016-01-23 18:31:58 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function list (callback) {
|
|
|
|
PodsDB.find(function (err, pods_list) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get the list of the pods.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
2016-01-23 18:31:58 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
return callback(null, pods_list)
|
|
|
|
})
|
|
|
|
}
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function remove (url, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
PodsDB.remove({ url: url }, callback)
|
|
|
|
}
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function removeAll (callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
PodsDB.remove(callback)
|
|
|
|
}
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function removeAllByIds (ids, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
PodsDB.remove({ _id: { $in: ids } }, callback)
|
|
|
|
}
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
module.exports = Pods
|