]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/pods.js
3 const mongoose
= require('mongoose')
4 const map
= require('lodash/map')
5 const validator
= require('express-validator').validator
7 const constants
= require('../initializers/constants')
9 // ---------------------------------------------------------------------------
11 const PodSchema
= mongoose
.Schema({
14 score: { type: Number
, max: constants
.FRIEND_SCORE
.MAX
},
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
) })
27 toFormatedJSON: toFormatedJSON
32 incrementScores: incrementScores
,
34 listAllIds: listAllIds
,
35 listBadPods: listBadPods
,
41 PodSchema
.pre('save', function (next
) {
44 Pod
.loadByUrl(this.url
, function (err
, pod
) {
45 if (err
) return next(err
)
47 if (pod
) return next(new Error('Pod already exists.'))
49 self
.score
= constants
.FRIEND_SCORE
.BASE
54 const Pod
= mongoose
.model('Pod', PodSchema
)
56 // ------------------------------ METHODS ------------------------------
58 function toFormatedJSON () {
63 createdDate: this.createdDate
69 // ------------------------------ Statics ------------------------------
71 function countAll (callback
) {
72 return this.count(callback
)
75 function incrementScores (ids
, value
, callback
) {
76 if (!callback
) callback = function () {}
77 return this.update({ _id: { $in: ids
} }, { $inc: { score: value
} }, { multi: true }, callback
)
80 function list (callback
) {
81 return this.find(callback
)
84 function listAllIds (callback
) {
85 return this.find({}, { _id: 1 }, function (err
, pods
) {
86 if (err
) return callback(err
)
88 return callback(null, map(pods
, '_id'))
92 function listBadPods (callback
) {
93 return this.find({ score: 0 }, callback
)
96 function load (id
, callback
) {
97 return this.findById(id
, callback
)
100 function loadByUrl (url
, callback
) {
101 return this.findOne({ url: url
}, callback
)
104 function removeAll (callback
) {
105 return this.remove({}, callback
)