aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pods.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-06-30 22:39:08 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-06-30 22:39:08 +0200
commita3ee6fa22dee4b68fcde9cd23708b471db446e11 (patch)
treeb05543f6d858c52dd4757123bc605277ede96983 /server/models/pods.js
parentd14b3e37a2c0d647b59259bf2ca059b5817f144c (diff)
downloadPeerTube-a3ee6fa22dee4b68fcde9cd23708b471db446e11.tar.gz
PeerTube-a3ee6fa22dee4b68fcde9cd23708b471db446e11.tar.zst
PeerTube-a3ee6fa22dee4b68fcde9cd23708b471db446e11.zip
Pod model refractoring -> use mongoose api
Diffstat (limited to 'server/models/pods.js')
-rw-r--r--server/models/pods.js102
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
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}