3 const mongoose = require('mongoose')
5 const constants = require('../initializers/constants')
6 const logger = require('../helpers/logger')
8 // ---------------------------------------------------------------------------
10 const podsSchema = mongoose.Schema({
13 score: { type: Number, max: constants.FRIEND_BASE_SCORE }
15 const PodsDB = mongoose.model('pods', podsSchema)
17 // ---------------------------------------------------------------------------
24 findBadPods: findBadPods,
25 incrementScores: incrementScores,
27 listAllIds: listAllIds,
28 listAllUrls: listAllUrls,
31 removeAllByIds: removeAllByIds
34 // TODO: check if the pod is not already a friend
35 function add (data, callback) {
36 if (!callback) callback = function () {}
39 publicKey: data.publicKey,
40 score: constants.FRIEND_BASE_SCORE
43 PodsDB.create(params, callback)
46 function count (callback) {
47 return PodsDB.count(callback)
50 function findBadPods (callback) {
51 PodsDB.find({ score: 0 }, callback)
54 function findById (id, callback) {
55 PodsDB.findById(id, callback)
58 function findByUrl (url, callback) {
59 PodsDB.findOne({ url: url }, callback)
62 function incrementScores (ids, value, callback) {
63 if (!callback) callback = function () {}
64 PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
67 function list (callback) {
68 PodsDB.find(function (err, podsList) {
70 logger.error('Cannot get the list of the pods.')
74 return callback(null, podsList)
78 function listAllIds (callback) {
79 return PodsDB.find({}, { _id: 1 }, callback)
82 function listAllUrls (callback) {
83 return PodsDB.find({}, { _id: 0, url: 1 }, callback)
86 function remove (url, callback) {
87 if (!callback) callback = function () {}
88 PodsDB.remove({ url: url }, callback)
91 function removeAll (callback) {
92 if (!callback) callback = function () {}
93 PodsDB.remove(callback)
96 function removeAllByIds (ids, callback) {
97 if (!callback) callback = function () {}
98 PodsDB.remove({ _id: { $in: ids } }, callback)
101 // ---------------------------------------------------------------------------
103 module.exports = Pods