aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
Diffstat (limited to 'server/models')
-rw-r--r--server/models/pods.js102
-rw-r--r--server/models/request.js48
2 files changed, 71 insertions, 79 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
3const mongoose = require('mongoose') 3const mongoose = require('mongoose')
4const map = require('lodash/map') 4const map = require('lodash/map')
5const validator = require('express-validator').validator
5 6
6const constants = require('../initializers/constants') 7const constants = require('../initializers/constants')
7const logger = require('../helpers/logger')
8 8
9// --------------------------------------------------------------------------- 9// ---------------------------------------------------------------------------
10 10
11const podsSchema = mongoose.Schema({ 11const 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})
16const PodsDB = mongoose.model('pods', podsSchema)
17 16
18// --------------------------------------------------------------------------- 17// TODO: set options (TLD...)
18PodSchema.path('url').validate(validator.isURL)
19PodSchema.path('publicKey').required(true)
20PodSchema.path('score').validate(function (value) { return !isNaN(value) })
19 21
20const Pods = { 22PodSchema.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 34PodSchema.pre('save', function (next) {
36function 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
47function count (callback) { 40 if (pod) return next(new Error('Pod already exists.'))
48 return PodsDB.count(callback)
49}
50 41
51function findBadPods (callback) { 42 self.score = constants.FRIEND_SCORE.BASE
52 PodsDB.find({ score: 0 }, callback) 43 return next()
53} 44 })
45})
54 46
55function findById (id, callback) { 47const Pod = mongoose.model('Pod', PodSchema)
56 PodsDB.findById(id, callback)
57}
58 48
59function findByUrl (url, callback) { 49// ------------------------------ Statics ------------------------------
60 PodsDB.findOne({ url: url }, callback) 50
51function countAll (callback) {
52 return this.count(callback)
61} 53}
62 54
63function incrementScores (ids, value, callback) { 55function 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
68function list (callback) { 60function 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
79function listAllIds (callback) { 64function 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
87function listAllUrls (callback) { 72function listOnlyUrls (callback) {
88 return PodsDB.find({}, { _id: 0, url: 1 }, callback) 73 return this.find({}, { _id: 0, url: 1 }, callback)
89} 74}
90 75
91function remove (url, callback) { 76function listBadPods (callback) {
92 if (!callback) callback = function () {} 77 return this.find({ score: 0 }, callback)
93 PodsDB.remove({ url: url }, callback)
94} 78}
95 79
96function removeAll (callback) { 80function load (id, callback) {
97 if (!callback) callback = function () {} 81 return this.findById(id, callback)
98 PodsDB.remove(callback)
99} 82}
100 83
101function removeAllByIds (ids, callback) { 84function 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// --------------------------------------------------------------------------- 88function removeAll (callback) {
107 89 return this.remove({}, callback)
108module.exports = Pods 90}
diff --git a/server/models/request.js b/server/models/request.js
index 2a407388a..db6ad5409 100644
--- a/server/models/request.js
+++ b/server/models/request.js
@@ -6,9 +6,9 @@ const mongoose = require('mongoose')
6 6
7const constants = require('../initializers/constants') 7const constants = require('../initializers/constants')
8const logger = require('../helpers/logger') 8const logger = require('../helpers/logger')
9const Pods = require('../models/pods')
10const requests = require('../helpers/requests') 9const requests = require('../helpers/requests')
11 10
11const Pod = mongoose.model('Pod')
12const Video = mongoose.model('Video') 12const Video = mongoose.model('Video')
13 13
14let timer = null 14let timer = null
@@ -31,7 +31,7 @@ RequestSchema.pre('save', function (next) {
31 const self = this 31 const self = this
32 32
33 if (self.to.length === 0) { 33 if (self.to.length === 0) {
34 Pods.listAllIds(function (err, podIds) { 34 Pod.listAllIds(function (err, podIds) {
35 if (err) return next(err) 35 if (err) return next(err)
36 36
37 // No friends 37 // No friends
@@ -140,7 +140,7 @@ function makeRequests () {
140 const requestToMake = requestsToMake[toPodId] 140 const requestToMake = requestsToMake[toPodId]
141 141
142 // FIXME: mongodb request inside a loop :/ 142 // FIXME: mongodb request inside a loop :/
143 Pods.findById(toPodId, function (err, toPod) { 143 Pod.load(toPodId, function (err, toPod) {
144 if (err) { 144 if (err) {
145 logger.error('Error finding pod by id.', { err: err }) 145 logger.error('Error finding pod by id.', { err: err })
146 return callbackEach() 146 return callbackEach()
@@ -185,7 +185,7 @@ function makeRequests () {
185function removeBadPods () { 185function removeBadPods () {
186 async.waterfall([ 186 async.waterfall([
187 function findBadPods (callback) { 187 function findBadPods (callback) {
188 Pods.findBadPods(function (err, pods) { 188 Pod.listBadPods(function (err, pods) {
189 if (err) { 189 if (err) {
190 logger.error('Cannot find bad pods.', { error: err }) 190 logger.error('Cannot find bad pods.', { error: err })
191 return callback(err) 191 return callback(err)
@@ -199,21 +199,23 @@ function removeBadPods () {
199 if (pods.length === 0) return callback(null) 199 if (pods.length === 0) return callback(null)
200 200
201 const urls = map(pods, 'url') 201 const urls = map(pods, 'url')
202 const ids = map(pods, '_id')
203 202
204 Video.listByUrls(urls, function (err, videosList) { 203 Video.listByUrls(urls, function (err, videosList) {
205 if (err) { 204 if (err) {
206 logger.error('Cannot list videos urls.', { error: err, urls: urls }) 205 logger.error('Cannot list videos urls.', { error: err, urls: urls })
207 return callback(null, ids, []) 206 return callback(null, pods, [])
208 } 207 }
209 208
210 return callback(null, ids, videosList) 209 return callback(null, pods, videosList)
211 }) 210 })
212 }, 211 },
213 212
214 function removeVideosOfTheseBadPods (podIds, videosList, callback) { 213 function removeVideosOfTheseBadPods (pods, videosList, callback) {
215 // We don't have to remove pods, skip 214 // We don't have to remove pods, skip
216 if (typeof podIds === 'function') return podIds(null) 215 if (typeof pods === 'function') {
216 callback = pods
217 return callback(null)
218 }
217 219
218 async.each(videosList, function (video, callbackEach) { 220 async.each(videosList, function (video, callbackEach) {
219 video.remove(callbackEach) 221 video.remove(callbackEach)
@@ -224,22 +226,30 @@ function removeBadPods () {
224 return 226 return
225 } 227 }
226 228
227 return callback(null, podIds) 229 return callback(null, pods)
228 }) 230 })
229 }, 231 },
230 232
231 function removeBadPodsFromDB (podIds, callback) { 233 function removeBadPodsFromDB (pods, callback) {
232 // We don't have to remove pods, skip 234 // We don't have to remove pods, skip
233 if (typeof podIds === 'function') return podIds(null) 235 if (typeof pods === 'function') {
236 callback = pods
237 return callback(null)
238 }
239
240 async.each(pods, function (pod, callbackEach) {
241 pod.remove(callbackEach)
242 }, function (err) {
243 if (err) return callback(err)
234 244
235 Pods.removeAllByIds(podIds, callback) 245 return callback(null, pods.length)
246 })
236 } 247 }
237 ], function (err, removeResult) { 248 ], function (err, numberOfPodsRemoved) {
238 if (err) { 249 if (err) {
239 logger.error('Cannot remove bad pods.', { error: err }) 250 logger.error('Cannot remove bad pods.', { error: err })
240 } else if (removeResult) { 251 } else if (numberOfPodsRemoved) {
241 const podsRemoved = removeResult.result.n 252 logger.info('Removed %d pods.', numberOfPodsRemoved)
242 logger.info('Removed %d pods.', podsRemoved)
243 } else { 253 } else {
244 logger.info('No need to remove bad pods.') 254 logger.info('No need to remove bad pods.')
245 } 255 }
@@ -249,11 +259,11 @@ function removeBadPods () {
249function updatePodsScore (goodPods, badPods) { 259function updatePodsScore (goodPods, badPods) {
250 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) 260 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
251 261
252 Pods.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { 262 Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
253 if (err) logger.error('Cannot increment scores of good pods.') 263 if (err) logger.error('Cannot increment scores of good pods.')
254 }) 264 })
255 265
256 Pods.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { 266 Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
257 if (err) logger.error('Cannot decrement scores of bad pods.') 267 if (err) logger.error('Cannot decrement scores of bad pods.')
258 removeBadPods() 268 removeBadPods()
259 }) 269 })