]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/models/pods.js
Do not generate a random password for test env
[github/Chocobozzz/PeerTube.git] / server / models / pods.js
... / ...
CommitLineData
1'use strict'
2
3const mongoose = require('mongoose')
4const map = require('lodash/map')
5
6const constants = require('../initializers/constants')
7const logger = require('../helpers/logger')
8
9// ---------------------------------------------------------------------------
10
11const podsSchema = mongoose.Schema({
12 url: String,
13 publicKey: String,
14 score: { type: Number, max: constants.FRIEND_BASE_SCORE }
15})
16const PodsDB = mongoose.model('pods', podsSchema)
17
18// ---------------------------------------------------------------------------
19
20const Pods = {
21 add: add,
22 count: count,
23 findById: findById,
24 findByUrl: findByUrl,
25 findBadPods: findBadPods,
26 incrementScores: incrementScores,
27 list: list,
28 listAllIds: listAllIds,
29 listAllUrls: listAllUrls,
30 remove: remove,
31 removeAll: removeAll,
32 removeAllByIds: removeAllByIds
33}
34
35// TODO: check if the pod is not already a friend
36function add (data, callback) {
37 if (!callback) callback = function () {}
38 const params = {
39 url: data.url,
40 publicKey: data.publicKey,
41 score: constants.FRIEND_BASE_SCORE
42 }
43
44 PodsDB.create(params, callback)
45}
46
47function count (callback) {
48 return PodsDB.count(callback)
49}
50
51function findBadPods (callback) {
52 PodsDB.find({ score: 0 }, callback)
53}
54
55function findById (id, callback) {
56 PodsDB.findById(id, callback)
57}
58
59function findByUrl (url, callback) {
60 PodsDB.findOne({ url: url }, callback)
61}
62
63function incrementScores (ids, value, callback) {
64 if (!callback) callback = function () {}
65 PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
66}
67
68function list (callback) {
69 PodsDB.find(function (err, podsList) {
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}
78
79function listAllIds (callback) {
80 return PodsDB.find({}, { _id: 1 }, function (err, pods) {
81 if (err) return callback(err)
82
83 return callback(null, map(pods, '_id'))
84 })
85}
86
87function listAllUrls (callback) {
88 return PodsDB.find({}, { _id: 0, url: 1 }, callback)
89}
90
91function remove (url, callback) {
92 if (!callback) callback = function () {}
93 PodsDB.remove({ url: url }, callback)
94}
95
96function removeAll (callback) {
97 if (!callback) callback = function () {}
98 PodsDB.remove(callback)
99}
100
101function removeAllByIds (ids, callback) {
102 if (!callback) callback = function () {}
103 PodsDB.remove({ _id: { $in: ids } }, callback)
104}
105
106// ---------------------------------------------------------------------------
107
108module.exports = Pods