diff options
Diffstat (limited to 'server/models/pods.js')
-rw-r--r-- | server/models/pods.js | 119 |
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 | |||
3 | const each = require('async/each') | ||
4 | const mongoose = require('mongoose') | ||
5 | const map = require('lodash/map') | ||
6 | const validator = require('express-validator').validator | ||
7 | |||
8 | const constants = require('../initializers/constants') | ||
9 | |||
10 | const Video = mongoose.model('Video') | ||
11 | |||
12 | // --------------------------------------------------------------------------- | ||
13 | |||
14 | const 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 | |||
24 | PodSchema.path('host').validate(validator.isURL) | ||
25 | PodSchema.path('publicKey').required(true) | ||
26 | PodSchema.path('score').validate(function (value) { return !isNaN(value) }) | ||
27 | |||
28 | PodSchema.methods = { | ||
29 | toFormatedJSON | ||
30 | } | ||
31 | |||
32 | PodSchema.statics = { | ||
33 | countAll, | ||
34 | incrementScores, | ||
35 | list, | ||
36 | listAllIds, | ||
37 | listBadPods, | ||
38 | load, | ||
39 | loadByHost, | ||
40 | removeAll | ||
41 | } | ||
42 | |||
43 | PodSchema.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 | |||
56 | PodSchema.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 | |||
67 | const Pod = mongoose.model('Pod', PodSchema) | ||
68 | |||
69 | // ------------------------------ METHODS ------------------------------ | ||
70 | |||
71 | function 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 | |||
84 | function countAll (callback) { | ||
85 | return this.count(callback) | ||
86 | } | ||
87 | |||
88 | function incrementScores (ids, value, callback) { | ||
89 | if (!callback) callback = function () {} | ||
90 | return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) | ||
91 | } | ||
92 | |||
93 | function list (callback) { | ||
94 | return this.find(callback) | ||
95 | } | ||
96 | |||
97 | function 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 | |||
105 | function listBadPods (callback) { | ||
106 | return this.find({ score: 0 }, callback) | ||
107 | } | ||
108 | |||
109 | function load (id, callback) { | ||
110 | return this.findById(id, callback) | ||
111 | } | ||
112 | |||
113 | function loadByHost (host, callback) { | ||
114 | return this.findOne({ host }, callback) | ||
115 | } | ||
116 | |||
117 | function removeAll (callback) { | ||
118 | return this.remove({}, callback) | ||
119 | } | ||