]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/pods.js
Server: set manually the post host of a remote video throught the
[github/Chocobozzz/PeerTube.git] / server / models / pods.js
CommitLineData
9f10b292
C
1'use strict'
2
80a6c9e7 3const each = require('async/each')
f0f5567b 4const mongoose = require('mongoose')
00057e85 5const map = require('lodash/map')
a3ee6fa2 6const validator = require('express-validator').validator
9f10b292 7
f0f5567b 8const constants = require('../initializers/constants')
9f10b292 9
80a6c9e7
C
10const Video = mongoose.model('Video')
11
9f10b292
C
12// ---------------------------------------------------------------------------
13
a3ee6fa2 14const PodSchema = mongoose.Schema({
49abbbbe 15 host: String,
9f10b292 16 publicKey: String,
53572423
C
17 score: { type: Number, max: constants.FRIEND_SCORE.MAX },
18 createdDate: {
19 type: Date,
20 default: Date.now
21 }
9f10b292 22})
9f10b292 23
49abbbbe 24PodSchema.path('host').validate(validator.isURL)
a3ee6fa2
C
25PodSchema.path('publicKey').required(true)
26PodSchema.path('score').validate(function (value) { return !isNaN(value) })
9f10b292 27
53572423 28PodSchema.methods = {
c4403b29 29 toFormatedJSON
53572423
C
30}
31
a3ee6fa2 32PodSchema.statics = {
c4403b29
C
33 countAll,
34 incrementScores,
35 list,
36 listAllIds,
37 listBadPods,
38 load,
49abbbbe 39 loadByHost,
c4403b29 40 removeAll
9f10b292
C
41}
42
a3ee6fa2
C
43PodSchema.pre('save', function (next) {
44 const self = this
8c308c2b 45
49abbbbe 46 Pod.loadByHost(this.host, function (err, pod) {
a3ee6fa2 47 if (err) return next(err)
45239549 48
a3ee6fa2 49 if (pod) return next(new Error('Pod already exists.'))
8c308c2b 50
a3ee6fa2
C
51 self.score = constants.FRIEND_SCORE.BASE
52 return next()
53 })
54})
45239549 55
80a6c9e7
C
56PodSchema.pre('remove', function (next) {
57 // Remove the videos owned by this pod too
49abbbbe 58 Video.listByHost(this.host, function (err, videos) {
80a6c9e7
C
59 if (err) return next(err)
60
61 each(videos, function (video, callbackEach) {
62 video.remove(callbackEach)
63 }, next)
64 })
65})
66
a3ee6fa2 67const Pod = mongoose.model('Pod', PodSchema)
528a9efa 68
53572423
C
69// ------------------------------ METHODS ------------------------------
70
71function toFormatedJSON () {
72 const json = {
73 id: this._id,
49abbbbe 74 host: this.host,
53572423
C
75 score: this.score,
76 createdDate: this.createdDate
77 }
78
79 return json
80}
81
a3ee6fa2
C
82// ------------------------------ Statics ------------------------------
83
84function countAll (callback) {
85 return this.count(callback)
9f10b292 86}
45239549 87
9f10b292
C
88function incrementScores (ids, value, callback) {
89 if (!callback) callback = function () {}
a3ee6fa2 90 return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
9f10b292 91}
45239549 92
9f10b292 93function list (callback) {
a3ee6fa2 94 return this.find(callback)
9f10b292 95}
8c308c2b 96
528a9efa 97function listAllIds (callback) {
a3ee6fa2 98 return this.find({}, { _id: 1 }, function (err, pods) {
00057e85
C
99 if (err) return callback(err)
100
101 return callback(null, map(pods, '_id'))
102 })
528a9efa
C
103}
104
a3ee6fa2
C
105function listBadPods (callback) {
106 return this.find({ score: 0 }, callback)
9f10b292 107}
8c308c2b 108
a3ee6fa2
C
109function load (id, callback) {
110 return this.findById(id, callback)
9f10b292 111}
c45f7f84 112
49abbbbe
C
113function loadByHost (host, callback) {
114 return this.findOne({ host }, callback)
9f10b292 115}
c45f7f84 116
a3ee6fa2
C
117function removeAll (callback) {
118 return this.remove({}, callback)
119}