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