]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b | 3 | const mongoose = require('mongoose') |
00057e85 | 4 | const map = require('lodash/map') |
a3ee6fa2 | 5 | const validator = require('express-validator').validator |
9f10b292 | 6 | |
f0f5567b | 7 | const constants = require('../initializers/constants') |
9f10b292 C |
8 | |
9 | // --------------------------------------------------------------------------- | |
10 | ||
a3ee6fa2 | 11 | const PodSchema = mongoose.Schema({ |
9f10b292 C |
12 | url: String, |
13 | publicKey: String, | |
53572423 C |
14 | score: { type: Number, max: constants.FRIEND_SCORE.MAX }, |
15 | createdDate: { | |
16 | type: Date, | |
17 | default: Date.now | |
18 | } | |
9f10b292 | 19 | }) |
9f10b292 | 20 | |
a3ee6fa2 C |
21 | // TODO: set options (TLD...) |
22 | PodSchema.path('url').validate(validator.isURL) | |
23 | PodSchema.path('publicKey').required(true) | |
24 | PodSchema.path('score').validate(function (value) { return !isNaN(value) }) | |
9f10b292 | 25 | |
53572423 | 26 | PodSchema.methods = { |
c4403b29 | 27 | toFormatedJSON |
53572423 C |
28 | } |
29 | ||
a3ee6fa2 | 30 | PodSchema.statics = { |
c4403b29 C |
31 | countAll, |
32 | incrementScores, | |
33 | list, | |
34 | listAllIds, | |
35 | listBadPods, | |
36 | load, | |
37 | loadByUrl, | |
38 | removeAll | |
9f10b292 C |
39 | } |
40 | ||
a3ee6fa2 C |
41 | PodSchema.pre('save', function (next) { |
42 | const self = this | |
8c308c2b | 43 | |
a3ee6fa2 C |
44 | Pod.loadByUrl(this.url, function (err, pod) { |
45 | if (err) return next(err) | |
45239549 | 46 | |
a3ee6fa2 | 47 | if (pod) return next(new Error('Pod already exists.')) |
8c308c2b | 48 | |
a3ee6fa2 C |
49 | self.score = constants.FRIEND_SCORE.BASE |
50 | return next() | |
51 | }) | |
52 | }) | |
45239549 | 53 | |
a3ee6fa2 | 54 | const Pod = mongoose.model('Pod', PodSchema) |
528a9efa | 55 | |
53572423 C |
56 | // ------------------------------ METHODS ------------------------------ |
57 | ||
58 | function toFormatedJSON () { | |
59 | const json = { | |
60 | id: this._id, | |
61 | url: this.url, | |
62 | score: this.score, | |
63 | createdDate: this.createdDate | |
64 | } | |
65 | ||
66 | return json | |
67 | } | |
68 | ||
a3ee6fa2 C |
69 | // ------------------------------ Statics ------------------------------ |
70 | ||
71 | function countAll (callback) { | |
72 | return this.count(callback) | |
9f10b292 | 73 | } |
45239549 | 74 | |
9f10b292 C |
75 | function incrementScores (ids, value, callback) { |
76 | if (!callback) callback = function () {} | |
a3ee6fa2 | 77 | return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) |
9f10b292 | 78 | } |
45239549 | 79 | |
9f10b292 | 80 | function list (callback) { |
a3ee6fa2 | 81 | return this.find(callback) |
9f10b292 | 82 | } |
8c308c2b | 83 | |
528a9efa | 84 | function listAllIds (callback) { |
a3ee6fa2 | 85 | return this.find({}, { _id: 1 }, function (err, pods) { |
00057e85 C |
86 | if (err) return callback(err) |
87 | ||
88 | return callback(null, map(pods, '_id')) | |
89 | }) | |
528a9efa C |
90 | } |
91 | ||
a3ee6fa2 C |
92 | function listBadPods (callback) { |
93 | return this.find({ score: 0 }, callback) | |
9f10b292 | 94 | } |
8c308c2b | 95 | |
a3ee6fa2 C |
96 | function load (id, callback) { |
97 | return this.findById(id, callback) | |
9f10b292 | 98 | } |
c45f7f84 | 99 | |
a3ee6fa2 C |
100 | function loadByUrl (url, callback) { |
101 | return this.findOne({ url: url }, callback) | |
9f10b292 | 102 | } |
c45f7f84 | 103 | |
a3ee6fa2 C |
104 | function removeAll (callback) { |
105 | return this.remove({}, callback) | |
106 | } |