diff options
Diffstat (limited to 'server/models/pods.js')
-rw-r--r-- | server/models/pods.js | 102 |
1 files changed, 42 insertions, 60 deletions
diff --git a/server/models/pods.js b/server/models/pods.js index 9502d92e4..bf43d7b25 100644 --- a/server/models/pods.js +++ b/server/models/pods.js | |||
@@ -2,107 +2,89 @@ | |||
2 | 2 | ||
3 | const mongoose = require('mongoose') | 3 | const mongoose = require('mongoose') |
4 | const map = require('lodash/map') | 4 | const map = require('lodash/map') |
5 | const validator = require('express-validator').validator | ||
5 | 6 | ||
6 | const constants = require('../initializers/constants') | 7 | const constants = require('../initializers/constants') |
7 | const logger = require('../helpers/logger') | ||
8 | 8 | ||
9 | // --------------------------------------------------------------------------- | 9 | // --------------------------------------------------------------------------- |
10 | 10 | ||
11 | const podsSchema = mongoose.Schema({ | 11 | const PodSchema = mongoose.Schema({ |
12 | url: String, | 12 | url: String, |
13 | publicKey: String, | 13 | publicKey: String, |
14 | score: { type: Number, max: constants.FRIEND_BASE_SCORE } | 14 | score: { type: Number, max: constants.FRIEND_SCORE.MAX } |
15 | }) | 15 | }) |
16 | const PodsDB = mongoose.model('pods', podsSchema) | ||
17 | 16 | ||
18 | // --------------------------------------------------------------------------- | 17 | // TODO: set options (TLD...) |
18 | PodSchema.path('url').validate(validator.isURL) | ||
19 | PodSchema.path('publicKey').required(true) | ||
20 | PodSchema.path('score').validate(function (value) { return !isNaN(value) }) | ||
19 | 21 | ||
20 | const Pods = { | 22 | PodSchema.statics = { |
21 | add: add, | 23 | countAll: countAll, |
22 | count: count, | ||
23 | findById: findById, | ||
24 | findByUrl: findByUrl, | ||
25 | findBadPods: findBadPods, | ||
26 | incrementScores: incrementScores, | 24 | incrementScores: incrementScores, |
27 | list: list, | 25 | list: list, |
28 | listAllIds: listAllIds, | 26 | listAllIds: listAllIds, |
29 | listAllUrls: listAllUrls, | 27 | listOnlyUrls: listOnlyUrls, |
30 | remove: remove, | 28 | listBadPods: listBadPods, |
31 | removeAll: removeAll, | 29 | load: load, |
32 | removeAllByIds: removeAllByIds | 30 | loadByUrl: loadByUrl, |
31 | removeAll: removeAll | ||
33 | } | 32 | } |
34 | 33 | ||
35 | // TODO: check if the pod is not already a friend | 34 | PodSchema.pre('save', function (next) { |
36 | function add (data, callback) { | 35 | const self = this |
37 | if (!callback) callback = function () {} | ||
38 | const params = { | ||
39 | url: data.url, | ||
40 | publicKey: data.publicKey, | ||
41 | score: constants.FRIEND_BASE_SCORE | ||
42 | } | ||
43 | 36 | ||
44 | PodsDB.create(params, callback) | 37 | Pod.loadByUrl(this.url, function (err, pod) { |
45 | } | 38 | if (err) return next(err) |
46 | 39 | ||
47 | function count (callback) { | 40 | if (pod) return next(new Error('Pod already exists.')) |
48 | return PodsDB.count(callback) | ||
49 | } | ||
50 | 41 | ||
51 | function findBadPods (callback) { | 42 | self.score = constants.FRIEND_SCORE.BASE |
52 | PodsDB.find({ score: 0 }, callback) | 43 | return next() |
53 | } | 44 | }) |
45 | }) | ||
54 | 46 | ||
55 | function findById (id, callback) { | 47 | const Pod = mongoose.model('Pod', PodSchema) |
56 | PodsDB.findById(id, callback) | ||
57 | } | ||
58 | 48 | ||
59 | function findByUrl (url, callback) { | 49 | // ------------------------------ Statics ------------------------------ |
60 | PodsDB.findOne({ url: url }, callback) | 50 | |
51 | function countAll (callback) { | ||
52 | return this.count(callback) | ||
61 | } | 53 | } |
62 | 54 | ||
63 | function incrementScores (ids, value, callback) { | 55 | function incrementScores (ids, value, callback) { |
64 | if (!callback) callback = function () {} | 56 | if (!callback) callback = function () {} |
65 | PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) | 57 | return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) |
66 | } | 58 | } |
67 | 59 | ||
68 | function list (callback) { | 60 | function list (callback) { |
69 | PodsDB.find(function (err, podsList) { | 61 | return this.find(callback) |
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 | } | 62 | } |
78 | 63 | ||
79 | function listAllIds (callback) { | 64 | function listAllIds (callback) { |
80 | return PodsDB.find({}, { _id: 1 }, function (err, pods) { | 65 | return this.find({}, { _id: 1 }, function (err, pods) { |
81 | if (err) return callback(err) | 66 | if (err) return callback(err) |
82 | 67 | ||
83 | return callback(null, map(pods, '_id')) | 68 | return callback(null, map(pods, '_id')) |
84 | }) | 69 | }) |
85 | } | 70 | } |
86 | 71 | ||
87 | function listAllUrls (callback) { | 72 | function listOnlyUrls (callback) { |
88 | return PodsDB.find({}, { _id: 0, url: 1 }, callback) | 73 | return this.find({}, { _id: 0, url: 1 }, callback) |
89 | } | 74 | } |
90 | 75 | ||
91 | function remove (url, callback) { | 76 | function listBadPods (callback) { |
92 | if (!callback) callback = function () {} | 77 | return this.find({ score: 0 }, callback) |
93 | PodsDB.remove({ url: url }, callback) | ||
94 | } | 78 | } |
95 | 79 | ||
96 | function removeAll (callback) { | 80 | function load (id, callback) { |
97 | if (!callback) callback = function () {} | 81 | return this.findById(id, callback) |
98 | PodsDB.remove(callback) | ||
99 | } | 82 | } |
100 | 83 | ||
101 | function removeAllByIds (ids, callback) { | 84 | function loadByUrl (url, callback) { |
102 | if (!callback) callback = function () {} | 85 | return this.findOne({ url: url }, callback) |
103 | PodsDB.remove({ _id: { $in: ids } }, callback) | ||
104 | } | 86 | } |
105 | 87 | ||
106 | // --------------------------------------------------------------------------- | 88 | function removeAll (callback) { |
107 | 89 | return this.remove({}, callback) | |
108 | module.exports = Pods | 90 | } |