2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const mongoose = require('mongoose')
|
2016-06-28 13:10:32 -05:00
|
|
|
const map = require('lodash/map')
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const constants = require('../initializers/constants')
|
|
|
|
const logger = require('../helpers/logger')
|
2016-02-07 04:23:23 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const podsSchema = mongoose.Schema({
|
2016-02-07 04:23:23 -06:00
|
|
|
url: String,
|
|
|
|
publicKey: String,
|
|
|
|
score: { type: Number, max: constants.FRIEND_BASE_SCORE }
|
|
|
|
})
|
2016-03-16 16:29:27 -05:00
|
|
|
const PodsDB = mongoose.model('pods', podsSchema)
|
2016-02-07 04:23:23 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const Pods = {
|
2016-02-07 04:23:23 -06:00
|
|
|
add: add,
|
|
|
|
count: count,
|
2016-06-18 09:13:54 -05:00
|
|
|
findById: findById,
|
2016-02-07 04:23:23 -06:00
|
|
|
findByUrl: findByUrl,
|
|
|
|
findBadPods: findBadPods,
|
|
|
|
incrementScores: incrementScores,
|
|
|
|
list: list,
|
2016-06-18 09:13:54 -05:00
|
|
|
listAllIds: listAllIds,
|
|
|
|
listAllUrls: listAllUrls,
|
2016-02-07 04:23:23 -06:00
|
|
|
remove: remove,
|
|
|
|
removeAll: removeAll,
|
|
|
|
removeAllByIds: removeAllByIds
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check if the pod is not already a friend
|
|
|
|
function add (data, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
2016-03-16 16:29:27 -05:00
|
|
|
const params = {
|
2016-02-07 04:23:23 -06:00
|
|
|
url: data.url,
|
|
|
|
publicKey: data.publicKey,
|
|
|
|
score: constants.FRIEND_BASE_SCORE
|
2015-06-09 10:41:40 -05:00
|
|
|
}
|
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
PodsDB.create(params, callback)
|
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function count (callback) {
|
|
|
|
return PodsDB.count(callback)
|
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function findBadPods (callback) {
|
|
|
|
PodsDB.find({ score: 0 }, callback)
|
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-06-18 09:13:54 -05:00
|
|
|
function findById (id, callback) {
|
|
|
|
PodsDB.findById(id, callback)
|
|
|
|
}
|
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function findByUrl (url, callback) {
|
|
|
|
PodsDB.findOne({ url: url }, callback)
|
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function incrementScores (ids, value, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
|
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function list (callback) {
|
2016-05-11 14:19:34 -05:00
|
|
|
PodsDB.find(function (err, podsList) {
|
2016-02-07 04:23:23 -06:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get the list of the pods.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
return callback(null, podsList)
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-06-18 09:13:54 -05:00
|
|
|
function listAllIds (callback) {
|
2016-06-28 13:10:32 -05:00
|
|
|
return PodsDB.find({}, { _id: 1 }, function (err, pods) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, map(pods, '_id'))
|
|
|
|
})
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function listAllUrls (callback) {
|
|
|
|
return PodsDB.find({}, { _id: 0, url: 1 }, callback)
|
|
|
|
}
|
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function remove (url, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
PodsDB.remove({ url: url }, callback)
|
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function removeAll (callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
PodsDB.remove(callback)
|
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function removeAllByIds (ids, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
|
|
|
PodsDB.remove({ _id: { $in: ids } }, callback)
|
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
module.exports = Pods
|