aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pods.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/pods.js')
-rw-r--r--server/models/pods.js119
1 files changed, 0 insertions, 119 deletions
diff --git a/server/models/pods.js b/server/models/pods.js
deleted file mode 100644
index 49c73472a..000000000
--- a/server/models/pods.js
+++ /dev/null
@@ -1,119 +0,0 @@
1'use strict'
2
3const each = require('async/each')
4const mongoose = require('mongoose')
5const map = require('lodash/map')
6const validator = require('express-validator').validator
7
8const constants = require('../initializers/constants')
9
10const Video = mongoose.model('Video')
11
12// ---------------------------------------------------------------------------
13
14const PodSchema = mongoose.Schema({
15 host: String,
16 publicKey: String,
17 score: { type: Number, max: constants.FRIEND_SCORE.MAX },
18 createdDate: {
19 type: Date,
20 default: Date.now
21 }
22})
23
24PodSchema.path('host').validate(validator.isURL)
25PodSchema.path('publicKey').required(true)
26PodSchema.path('score').validate(function (value) { return !isNaN(value) })
27
28PodSchema.methods = {
29 toFormatedJSON
30}
31
32PodSchema.statics = {
33 countAll,
34 incrementScores,
35 list,
36 listAllIds,
37 listBadPods,
38 load,
39 loadByHost,
40 removeAll
41}
42
43PodSchema.pre('save', function (next) {
44 const self = this
45
46 Pod.loadByHost(this.host, function (err, pod) {
47 if (err) return next(err)
48
49 if (pod) return next(new Error('Pod already exists.'))
50
51 self.score = constants.FRIEND_SCORE.BASE
52 return next()
53 })
54})
55
56PodSchema.pre('remove', function (next) {
57 // Remove the videos owned by this pod too
58 Video.listByHost(this.host, function (err, videos) {
59 if (err) return next(err)
60
61 each(videos, function (video, callbackEach) {
62 video.remove(callbackEach)
63 }, next)
64 })
65})
66
67const Pod = mongoose.model('Pod', PodSchema)
68
69// ------------------------------ METHODS ------------------------------
70
71function toFormatedJSON () {
72 const json = {
73 id: this._id,
74 host: this.host,
75 score: this.score,
76 createdDate: this.createdDate
77 }
78
79 return json
80}
81
82// ------------------------------ Statics ------------------------------
83
84function countAll (callback) {
85 return this.count(callback)
86}
87
88function incrementScores (ids, value, callback) {
89 if (!callback) callback = function () {}
90 return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
91}
92
93function list (callback) {
94 return this.find(callback)
95}
96
97function listAllIds (callback) {
98 return this.find({}, { _id: 1 }, function (err, pods) {
99 if (err) return callback(err)
100
101 return callback(null, map(pods, '_id'))
102 })
103}
104
105function listBadPods (callback) {
106 return this.find({ score: 0 }, callback)
107}
108
109function load (id, callback) {
110 return this.findById(id, callback)
111}
112
113function loadByHost (host, callback) {
114 return this.findOne({ host }, callback)
115}
116
117function removeAll (callback) {
118 return this.remove({}, callback)
119}