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