]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/pods.js
Server: fix remaining milli seconds before the next requests feature
[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')
a3ee6fa2 5const validator = require('express-validator').validator
9f10b292 6
f0f5567b 7const constants = require('../initializers/constants')
9f10b292
C
8
9// ---------------------------------------------------------------------------
10
a3ee6fa2 11const PodSchema = mongoose.Schema({
9f10b292
C
12 url: String,
13 publicKey: String,
53572423
C
14 score: { type: Number, max: constants.FRIEND_SCORE.MAX },
15 createdDate: {
16 type: Date,
17 default: Date.now
18 }
9f10b292 19})
9f10b292 20
a3ee6fa2
C
21// TODO: set options (TLD...)
22PodSchema.path('url').validate(validator.isURL)
23PodSchema.path('publicKey').required(true)
24PodSchema.path('score').validate(function (value) { return !isNaN(value) })
9f10b292 25
53572423
C
26PodSchema.methods = {
27 toFormatedJSON: toFormatedJSON
28}
29
a3ee6fa2
C
30PodSchema.statics = {
31 countAll: countAll,
9f10b292
C
32 incrementScores: incrementScores,
33 list: list,
528a9efa 34 listAllIds: listAllIds,
a3ee6fa2
C
35 listBadPods: listBadPods,
36 load: load,
37 loadByUrl: loadByUrl,
38 removeAll: removeAll
9f10b292
C
39}
40
a3ee6fa2
C
41PodSchema.pre('save', function (next) {
42 const self = this
8c308c2b 43
a3ee6fa2
C
44 Pod.loadByUrl(this.url, function (err, pod) {
45 if (err) return next(err)
45239549 46
a3ee6fa2 47 if (pod) return next(new Error('Pod already exists.'))
8c308c2b 48
a3ee6fa2
C
49 self.score = constants.FRIEND_SCORE.BASE
50 return next()
51 })
52})
45239549 53
a3ee6fa2 54const Pod = mongoose.model('Pod', PodSchema)
528a9efa 55
53572423
C
56// ------------------------------ METHODS ------------------------------
57
58function toFormatedJSON () {
59 const json = {
60 id: this._id,
61 url: this.url,
62 score: this.score,
63 createdDate: this.createdDate
64 }
65
66 return json
67}
68
a3ee6fa2
C
69// ------------------------------ Statics ------------------------------
70
71function countAll (callback) {
72 return this.count(callback)
9f10b292 73}
45239549 74
9f10b292
C
75function incrementScores (ids, value, callback) {
76 if (!callback) callback = function () {}
a3ee6fa2 77 return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
9f10b292 78}
45239549 79
9f10b292 80function list (callback) {
a3ee6fa2 81 return this.find(callback)
9f10b292 82}
8c308c2b 83
528a9efa 84function listAllIds (callback) {
a3ee6fa2 85 return this.find({}, { _id: 1 }, function (err, pods) {
00057e85
C
86 if (err) return callback(err)
87
88 return callback(null, map(pods, '_id'))
89 })
528a9efa
C
90}
91
a3ee6fa2
C
92function listBadPods (callback) {
93 return this.find({ score: 0 }, callback)
9f10b292 94}
8c308c2b 95
a3ee6fa2
C
96function load (id, callback) {
97 return this.findById(id, callback)
9f10b292 98}
c45f7f84 99
a3ee6fa2
C
100function loadByUrl (url, callback) {
101 return this.findOne({ url: url }, callback)
9f10b292 102}
c45f7f84 103
a3ee6fa2
C
104function removeAll (callback) {
105 return this.remove({}, callback)
106}