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.js156
1 files changed, 88 insertions, 68 deletions
diff --git a/server/models/pods.js b/server/models/pods.js
index 49c73472a..2c1f56203 100644
--- a/server/models/pods.js
+++ b/server/models/pods.js
@@ -1,79 +1,62 @@
1'use strict' 1'use strict'
2 2
3const each = require('async/each')
4const mongoose = require('mongoose')
5const map = require('lodash/map') 3const map = require('lodash/map')
6const validator = require('express-validator').validator
7 4
8const constants = require('../initializers/constants') 5const constants = require('../initializers/constants')
9 6
10const Video = mongoose.model('Video')
11
12// --------------------------------------------------------------------------- 7// ---------------------------------------------------------------------------
13 8
14const PodSchema = mongoose.Schema({ 9module.exports = function (sequelize, DataTypes) {
15 host: String, 10 const Pod = sequelize.define('Pod',
16 publicKey: String, 11 {
17 score: { type: Number, max: constants.FRIEND_SCORE.MAX }, 12 host: {
18 createdDate: { 13 type: DataTypes.STRING
19 type: Date, 14 },
20 default: Date.now 15 publicKey: {
21 } 16 type: DataTypes.STRING(5000)
22}) 17 },
23 18 score: {
24PodSchema.path('host').validate(validator.isURL) 19 type: DataTypes.INTEGER,
25PodSchema.path('publicKey').required(true) 20 defaultValue: constants.FRIEND_SCORE.BASE
26PodSchema.path('score').validate(function (value) { return !isNaN(value) }) 21 }
27 22 // Check createdAt
28PodSchema.methods = { 23 },
29 toFormatedJSON 24 {
25 classMethods: {
26 associate,
27
28 countAll,
29 incrementScores,
30 list,
31 listAllIds,
32 listBadPods,
33 load,
34 loadByHost,
35 removeAll
36 },
37 instanceMethods: {
38 toFormatedJSON
39 }
40 }
41 )
42
43 return Pod
30} 44}
31 45
32PodSchema.statics = { 46// TODO: max score -> constants.FRIENDS_SCORE.MAX
33 countAll, 47// TODO: validation
34 incrementScores, 48// PodSchema.path('host').validate(validator.isURL)
35 list, 49// PodSchema.path('publicKey').required(true)
36 listAllIds, 50// PodSchema.path('score').validate(function (value) { return !isNaN(value) })
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 51
69// ------------------------------ METHODS ------------------------------ 52// ------------------------------ METHODS ------------------------------
70 53
71function toFormatedJSON () { 54function toFormatedJSON () {
72 const json = { 55 const json = {
73 id: this._id, 56 id: this.id,
74 host: this.host, 57 host: this.host,
75 score: this.score, 58 score: this.score,
76 createdDate: this.createdDate 59 createdAt: this.createdAt
77 } 60 }
78 61
79 return json 62 return json
@@ -81,39 +64,76 @@ function toFormatedJSON () {
81 64
82// ------------------------------ Statics ------------------------------ 65// ------------------------------ Statics ------------------------------
83 66
67function associate (models) {
68 this.belongsToMany(models.Request, {
69 foreignKey: 'podId',
70 through: models.RequestToPod,
71 onDelete: 'CASCADE'
72 })
73}
74
84function countAll (callback) { 75function countAll (callback) {
85 return this.count(callback) 76 return this.count().asCallback(callback)
86} 77}
87 78
88function incrementScores (ids, value, callback) { 79function incrementScores (ids, value, callback) {
89 if (!callback) callback = function () {} 80 if (!callback) callback = function () {}
90 return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) 81
82 const update = {
83 score: this.sequelize.literal('score +' + value)
84 }
85
86 const query = {
87 where: {
88 id: {
89 $in: ids
90 }
91 }
92 }
93
94 return this.update(update, query).asCallback(callback)
91} 95}
92 96
93function list (callback) { 97function list (callback) {
94 return this.find(callback) 98 return this.findAll().asCallback(callback)
95} 99}
96 100
97function listAllIds (callback) { 101function listAllIds (callback) {
98 return this.find({}, { _id: 1 }, function (err, pods) { 102 const query = {
103 attributes: [ 'id' ]
104 }
105
106 return this.findAll(query).asCallback(function (err, pods) {
99 if (err) return callback(err) 107 if (err) return callback(err)
100 108
101 return callback(null, map(pods, '_id')) 109 return callback(null, map(pods, 'id'))
102 }) 110 })
103} 111}
104 112
105function listBadPods (callback) { 113function listBadPods (callback) {
106 return this.find({ score: 0 }, callback) 114 const query = {
115 where: {
116 score: { $lte: 0 }
117 }
118 }
119
120 return this.findAll(query).asCallback(callback)
107} 121}
108 122
109function load (id, callback) { 123function load (id, callback) {
110 return this.findById(id, callback) 124 return this.findById(id).asCallback(callback)
111} 125}
112 126
113function loadByHost (host, callback) { 127function loadByHost (host, callback) {
114 return this.findOne({ host }, callback) 128 const query = {
129 where: {
130 host: host
131 }
132 }
133
134 return this.findOne(query).asCallback(callback)
115} 135}
116 136
117function removeAll (callback) { 137function removeAll (callback) {
118 return this.remove({}, callback) 138 return this.destroy().asCallback(callback)
119} 139}