aboutsummaryrefslogtreecommitdiffhomepage
path: root/models/pods.js
diff options
context:
space:
mode:
Diffstat (limited to 'models/pods.js')
-rw-r--r--models/pods.js152
1 files changed, 75 insertions, 77 deletions
diff --git a/models/pods.js b/models/pods.js
index 04fd042c3..57ed20292 100644
--- a/models/pods.js
+++ b/models/pods.js
@@ -1,90 +1,88 @@
1;(function () { 1'use strict'
2 'use strict' 2
3 3var mongoose = require('mongoose')
4 var mongoose = require('mongoose') 4
5 5var constants = require('../initializers/constants')
6 var constants = require('../initializers/constants') 6var logger = require('../helpers/logger')
7 var logger = require('../helpers/logger') 7
8 8// ---------------------------------------------------------------------------
9 // --------------------------------------------------------------------------- 9
10 10var podsSchema = mongoose.Schema({
11 var podsSchema = mongoose.Schema({ 11 url: String,
12 url: String, 12 publicKey: String,
13 publicKey: String, 13 score: { type: Number, max: constants.FRIEND_BASE_SCORE }
14 score: { type: Number, max: constants.FRIEND_BASE_SCORE } 14})
15 }) 15var PodsDB = mongoose.model('pods', podsSchema)
16 var PodsDB = mongoose.model('pods', podsSchema) 16
17 17// ---------------------------------------------------------------------------
18 // --------------------------------------------------------------------------- 18
19 19var Pods = {
20 var Pods = { 20 add: add,
21 add: add, 21 count: count,
22 count: count, 22 findByUrl: findByUrl,
23 findByUrl: findByUrl, 23 findBadPods: findBadPods,
24 findBadPods: findBadPods, 24 incrementScores: incrementScores,
25 incrementScores: incrementScores, 25 list: list,
26 list: list, 26 remove: remove,
27 remove: remove, 27 removeAll: removeAll,
28 removeAll: removeAll, 28 removeAllByIds: removeAllByIds
29 removeAllByIds: removeAllByIds 29}
30
31// TODO: check if the pod is not already a friend
32function add (data, callback) {
33 if (!callback) callback = function () {}
34 var params = {
35 url: data.url,
36 publicKey: data.publicKey,
37 score: constants.FRIEND_BASE_SCORE
30 } 38 }
31 39
32 // TODO: check if the pod is not already a friend 40 PodsDB.create(params, callback)
33 function add (data, callback) { 41}
34 if (!callback) callback = function () {}
35 var params = {
36 url: data.url,
37 publicKey: data.publicKey,
38 score: constants.FRIEND_BASE_SCORE
39 }
40
41 PodsDB.create(params, callback)
42 }
43 42
44 function count (callback) { 43function count (callback) {
45 return PodsDB.count(callback) 44 return PodsDB.count(callback)
46 } 45}
47 46
48 function findBadPods (callback) { 47function findBadPods (callback) {
49 PodsDB.find({ score: 0 }, callback) 48 PodsDB.find({ score: 0 }, callback)
50 } 49}
51 50
52 function findByUrl (url, callback) { 51function findByUrl (url, callback) {
53 PodsDB.findOne({ url: url }, callback) 52 PodsDB.findOne({ url: url }, callback)
54 } 53}
55 54
56 function incrementScores (ids, value, callback) { 55function incrementScores (ids, value, callback) {
57 if (!callback) callback = function () {} 56 if (!callback) callback = function () {}
58 PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) 57 PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
59 } 58}
60 59
61 function list (callback) { 60function list (callback) {
62 PodsDB.find(function (err, pods_list) { 61 PodsDB.find(function (err, pods_list) {
63 if (err) { 62 if (err) {
64 logger.error('Cannot get the list of the pods.') 63 logger.error('Cannot get the list of the pods.')
65 return callback(err) 64 return callback(err)
66 } 65 }
67 66
68 return callback(null, pods_list) 67 return callback(null, pods_list)
69 }) 68 })
70 } 69}
71 70
72 function remove (url, callback) { 71function remove (url, callback) {
73 if (!callback) callback = function () {} 72 if (!callback) callback = function () {}
74 PodsDB.remove({ url: url }, callback) 73 PodsDB.remove({ url: url }, callback)
75 } 74}
76 75
77 function removeAll (callback) { 76function removeAll (callback) {
78 if (!callback) callback = function () {} 77 if (!callback) callback = function () {}
79 PodsDB.remove(callback) 78 PodsDB.remove(callback)
80 } 79}
81 80
82 function removeAllByIds (ids, callback) { 81function removeAllByIds (ids, callback) {
83 if (!callback) callback = function () {} 82 if (!callback) callback = function () {}
84 PodsDB.remove({ _id: { $in: ids } }, callback) 83 PodsDB.remove({ _id: { $in: ids } }, callback)
85 } 84}
86 85
87 // --------------------------------------------------------------------------- 86// ---------------------------------------------------------------------------
88 87
89 module.exports = Pods 88module.exports = Pods
90})()