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