]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/pods.js
3 const each
= require('async/each')
4 const mongoose
= require('mongoose')
5 const map
= require('lodash/map')
6 const validator
= require('express-validator').validator
8 const constants
= require('../initializers/constants')
10 const Video
= mongoose
.model('Video')
12 // ---------------------------------------------------------------------------
14 const PodSchema
= mongoose
.Schema({
17 score: { type: Number
, max: constants
.FRIEND_SCORE
.MAX
},
24 // TODO: set options (TLD...)
25 PodSchema
.path('url').validate(validator
.isURL
)
26 PodSchema
.path('publicKey').required(true)
27 PodSchema
.path('score').validate(function (value
) { return !isNaN(value
) })
44 PodSchema
.pre('save', function (next
) {
47 Pod
.loadByUrl(this.url
, function (err
, pod
) {
48 if (err
) return next(err
)
50 if (pod
) return next(new Error('Pod already exists.'))
52 self
.score
= constants
.FRIEND_SCORE
.BASE
57 PodSchema
.pre('remove', function (next
) {
58 // Remove the videos owned by this pod too
59 Video
.listByUrl(this.url
, function (err
, videos
) {
60 if (err
) return next(err
)
62 each(videos
, function (video
, callbackEach
) {
63 video
.remove(callbackEach
)
68 const Pod
= mongoose
.model('Pod', PodSchema
)
70 // ------------------------------ METHODS ------------------------------
72 function toFormatedJSON () {
77 createdDate: this.createdDate
83 // ------------------------------ Statics ------------------------------
85 function countAll (callback
) {
86 return this.count(callback
)
89 function incrementScores (ids
, value
, callback
) {
90 if (!callback
) callback = function () {}
91 return this.update({ _id: { $in: ids
} }, { $inc: { score: value
} }, { multi: true }, callback
)
94 function list (callback
) {
95 return this.find(callback
)
98 function listAllIds (callback
) {
99 return this.find({}, { _id: 1 }, function (err
, pods
) {
100 if (err
) return callback(err
)
102 return callback(null, map(pods
, '_id'))
106 function listBadPods (callback
) {
107 return this.find({ score: 0 }, callback
)
110 function load (id
, callback
) {
111 return this.findById(id
, callback
)
114 function loadByUrl (url
, callback
) {
115 return this.findOne({ url: url
}, callback
)
118 function removeAll (callback
) {
119 return this.remove({}, callback
)