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.js139
1 files changed, 0 insertions, 139 deletions
diff --git a/server/models/pods.js b/server/models/pods.js
deleted file mode 100644
index 2c1f56203..000000000
--- a/server/models/pods.js
+++ /dev/null
@@ -1,139 +0,0 @@
1'use strict'
2
3const map = require('lodash/map')
4
5const constants = require('../initializers/constants')
6
7// ---------------------------------------------------------------------------
8
9module.exports = function (sequelize, DataTypes) {
10 const Pod = sequelize.define('Pod',
11 {
12 host: {
13 type: DataTypes.STRING
14 },
15 publicKey: {
16 type: DataTypes.STRING(5000)
17 },
18 score: {
19 type: DataTypes.INTEGER,
20 defaultValue: constants.FRIEND_SCORE.BASE
21 }
22 // Check createdAt
23 },
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
44}
45
46// TODO: max score -> constants.FRIENDS_SCORE.MAX
47// TODO: validation
48// PodSchema.path('host').validate(validator.isURL)
49// PodSchema.path('publicKey').required(true)
50// PodSchema.path('score').validate(function (value) { return !isNaN(value) })
51
52// ------------------------------ METHODS ------------------------------
53
54function toFormatedJSON () {
55 const json = {
56 id: this.id,
57 host: this.host,
58 score: this.score,
59 createdAt: this.createdAt
60 }
61
62 return json
63}
64
65// ------------------------------ Statics ------------------------------
66
67function associate (models) {
68 this.belongsToMany(models.Request, {
69 foreignKey: 'podId',
70 through: models.RequestToPod,
71 onDelete: 'CASCADE'
72 })
73}
74
75function countAll (callback) {
76 return this.count().asCallback(callback)
77}
78
79function incrementScores (ids, value, callback) {
80 if (!callback) callback = function () {}
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)
95}
96
97function list (callback) {
98 return this.findAll().asCallback(callback)
99}
100
101function listAllIds (callback) {
102 const query = {
103 attributes: [ 'id' ]
104 }
105
106 return this.findAll(query).asCallback(function (err, pods) {
107 if (err) return callback(err)
108
109 return callback(null, map(pods, 'id'))
110 })
111}
112
113function listBadPods (callback) {
114 const query = {
115 where: {
116 score: { $lte: 0 }
117 }
118 }
119
120 return this.findAll(query).asCallback(callback)
121}
122
123function load (id, callback) {
124 return this.findById(id).asCallback(callback)
125}
126
127function loadByHost (host, callback) {
128 const query = {
129 where: {
130 host: host
131 }
132 }
133
134 return this.findOne(query).asCallback(callback)
135}
136
137function removeAll (callback) {
138 return this.destroy().asCallback(callback)
139}