]>
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
}
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
) })
24 incrementScores: incrementScores
,
26 listAllIds: listAllIds
,
27 listOnlyUrls: listOnlyUrls
,
28 listBadPods: listBadPods
,
34 PodSchema
.pre('save', function (next
) {
37 Pod
.loadByUrl(this.url
, function (err
, pod
) {
38 if (err
) return next(err
)
40 if (pod
) return next(new Error('Pod already exists.'))
42 self
.score
= constants
.FRIEND_SCORE
.BASE
47 const Pod
= mongoose
.model('Pod', PodSchema
)
49 // ------------------------------ Statics ------------------------------
51 function countAll (callback
) {
52 return this.count(callback
)
55 function incrementScores (ids
, value
, callback
) {
56 if (!callback
) callback = function () {}
57 return this.update({ _id: { $in: ids
} }, { $inc: { score: value
} }, { multi: true }, callback
)
60 function list (callback
) {
61 return this.find(callback
)
64 function listAllIds (callback
) {
65 return this.find({}, { _id: 1 }, function (err
, pods
) {
66 if (err
) return callback(err
)
68 return callback(null, map(pods
, '_id'))
72 function listOnlyUrls (callback
) {
73 return this.find({}, { _id: 0, url: 1 }, callback
)
76 function listBadPods (callback
) {
77 return this.find({ score: 0 }, callback
)
80 function load (id
, callback
) {
81 return this.findById(id
, callback
)
84 function loadByUrl (url
, callback
) {
85 return this.findOne({ url: url
}, callback
)
88 function removeAll (callback
) {
89 return this.remove({}, callback
)