]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b | 3 | const mongoose = require('mongoose') |
9f10b292 | 4 | |
f0f5567b C |
5 | const constants = require('../initializers/constants') |
6 | const logger = require('../helpers/logger') | |
9f10b292 C |
7 | |
8 | // --------------------------------------------------------------------------- | |
9 | ||
f0f5567b | 10 | const podsSchema = mongoose.Schema({ |
9f10b292 C |
11 | url: String, |
12 | publicKey: String, | |
13 | score: { type: Number, max: constants.FRIEND_BASE_SCORE } | |
14 | }) | |
f0f5567b | 15 | const PodsDB = mongoose.model('pods', podsSchema) |
9f10b292 C |
16 | |
17 | // --------------------------------------------------------------------------- | |
18 | ||
f0f5567b | 19 | const Pods = { |
9f10b292 C |
20 | add: add, |
21 | count: count, | |
22 | findByUrl: findByUrl, | |
23 | findBadPods: findBadPods, | |
24 | incrementScores: incrementScores, | |
25 | list: list, | |
26 | remove: remove, | |
27 | removeAll: removeAll, | |
28 | removeAllByIds: removeAllByIds | |
29 | } | |
30 | ||
31 | // TODO: check if the pod is not already a friend | |
32 | function add (data, callback) { | |
33 | if (!callback) callback = function () {} | |
f0f5567b | 34 | const params = { |
9f10b292 C |
35 | url: data.url, |
36 | publicKey: data.publicKey, | |
37 | score: constants.FRIEND_BASE_SCORE | |
8c308c2b C |
38 | } |
39 | ||
9f10b292 C |
40 | PodsDB.create(params, callback) |
41 | } | |
45239549 | 42 | |
9f10b292 C |
43 | function count (callback) { |
44 | return PodsDB.count(callback) | |
45 | } | |
8c308c2b | 46 | |
9f10b292 C |
47 | function findBadPods (callback) { |
48 | PodsDB.find({ score: 0 }, callback) | |
49 | } | |
45239549 | 50 | |
9f10b292 C |
51 | function findByUrl (url, callback) { |
52 | PodsDB.findOne({ url: url }, callback) | |
53 | } | |
45239549 | 54 | |
9f10b292 C |
55 | function incrementScores (ids, value, callback) { |
56 | if (!callback) callback = function () {} | |
57 | PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) | |
58 | } | |
45239549 | 59 | |
9f10b292 | 60 | function list (callback) { |
bc503c2a | 61 | PodsDB.find(function (err, podsList) { |
9f10b292 C |
62 | if (err) { |
63 | logger.error('Cannot get the list of the pods.') | |
64 | return callback(err) | |
65 | } | |
45239549 | 66 | |
bc503c2a | 67 | return callback(null, podsList) |
9f10b292 C |
68 | }) |
69 | } | |
8c308c2b | 70 | |
9f10b292 C |
71 | function remove (url, callback) { |
72 | if (!callback) callback = function () {} | |
73 | PodsDB.remove({ url: url }, callback) | |
74 | } | |
8c308c2b | 75 | |
9f10b292 C |
76 | function removeAll (callback) { |
77 | if (!callback) callback = function () {} | |
78 | PodsDB.remove(callback) | |
79 | } | |
c45f7f84 | 80 | |
9f10b292 C |
81 | function removeAllByIds (ids, callback) { |
82 | if (!callback) callback = function () {} | |
83 | PodsDB.remove({ _id: { $in: ids } }, callback) | |
84 | } | |
c45f7f84 | 85 | |
9f10b292 | 86 | // --------------------------------------------------------------------------- |
c45f7f84 | 87 | |
9f10b292 | 88 | module.exports = Pods |