]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/pods.js
3 const mongoose
= require('mongoose')
4 const map
= require('lodash/map')
6 const constants
= require('../initializers/constants')
7 const logger
= require('../helpers/logger')
9 // ---------------------------------------------------------------------------
11 const podsSchema
= mongoose
.Schema({
14 score: { type: Number
, max: constants
.FRIEND_BASE_SCORE
}
16 const PodsDB
= mongoose
.model('pods', podsSchema
)
18 // ---------------------------------------------------------------------------
25 findBadPods: findBadPods
,
26 incrementScores: incrementScores
,
28 listAllIds: listAllIds
,
29 listAllUrls: listAllUrls
,
32 removeAllByIds: removeAllByIds
35 // TODO: check if the pod is not already a friend
36 function add (data
, callback
) {
37 if (!callback
) callback = function () {}
40 publicKey: data
.publicKey
,
41 score: constants
.FRIEND_BASE_SCORE
44 PodsDB
.create(params
, callback
)
47 function count (callback
) {
48 return PodsDB
.count(callback
)
51 function findBadPods (callback
) {
52 PodsDB
.find({ score: 0 }, callback
)
55 function findById (id
, callback
) {
56 PodsDB
.findById(id
, callback
)
59 function findByUrl (url
, callback
) {
60 PodsDB
.findOne({ url: url
}, callback
)
63 function incrementScores (ids
, value
, callback
) {
64 if (!callback
) callback = function () {}
65 PodsDB
.update({ _id: { $in: ids
} }, { $inc: { score: value
} }, { multi: true }, callback
)
68 function list (callback
) {
69 PodsDB
.find(function (err
, podsList
) {
71 logger
.error('Cannot get the list of the pods.')
75 return callback(null, podsList
)
79 function listAllIds (callback
) {
80 return PodsDB
.find({}, { _id: 1 }, function (err
, pods
) {
81 if (err
) return callback(err
)
83 return callback(null, map(pods
, '_id'))
87 function listAllUrls (callback
) {
88 return PodsDB
.find({}, { _id: 0, url: 1 }, callback
)
91 function remove (url
, callback
) {
92 if (!callback
) callback = function () {}
93 PodsDB
.remove({ url: url
}, callback
)
96 function removeAll (callback
) {
97 if (!callback
) callback = function () {}
98 PodsDB
.remove(callback
)
101 function removeAllByIds (ids
, callback
) {
102 if (!callback
) callback = function () {}
103 PodsDB
.remove({ _id: { $in: ids
} }, callback
)
106 // ---------------------------------------------------------------------------
108 module
.exports
= Pods