]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
00057e85 | 3 | const map = require('lodash/map') |
9f10b292 | 4 | |
f0f5567b | 5 | const constants = require('../initializers/constants') |
67bf9b96 | 6 | const customPodsValidators = require('../helpers/custom-validators').pods |
9f10b292 C |
7 | |
8 | // --------------------------------------------------------------------------- | |
9 | ||
feb4bdfd C |
10 | module.exports = function (sequelize, DataTypes) { |
11 | const Pod = sequelize.define('Pod', | |
12 | { | |
13 | host: { | |
67bf9b96 C |
14 | type: DataTypes.STRING, |
15 | allowNull: false, | |
16 | validate: { | |
17 | isHost: function (value) { | |
18 | const res = customPodsValidators.isHostValid(value) | |
19 | if (res === false) throw new Error('Host not valid.') | |
20 | } | |
21 | } | |
feb4bdfd C |
22 | }, |
23 | publicKey: { | |
67bf9b96 C |
24 | type: DataTypes.STRING(5000), |
25 | allowNull: false | |
feb4bdfd C |
26 | }, |
27 | score: { | |
28 | type: DataTypes.INTEGER, | |
67bf9b96 C |
29 | defaultValue: constants.FRIEND_SCORE.BASE, |
30 | allowNull: false, | |
31 | validate: { | |
32 | isInt: true, | |
33 | max: constants.FRIEND_SCORE.MAX | |
34 | } | |
feb4bdfd | 35 | } |
feb4bdfd C |
36 | }, |
37 | { | |
319d072e C |
38 | indexes: [ |
39 | { | |
40 | fields: [ 'host' ] | |
41 | }, | |
42 | { | |
43 | fields: [ 'score' ] | |
44 | } | |
45 | ], | |
feb4bdfd C |
46 | classMethods: { |
47 | associate, | |
48 | ||
49 | countAll, | |
50 | incrementScores, | |
51 | list, | |
52 | listAllIds, | |
bd14d16a | 53 | listRandomPodIdsWithRequest, |
feb4bdfd C |
54 | listBadPods, |
55 | load, | |
56 | loadByHost, | |
57 | removeAll | |
58 | }, | |
59 | instanceMethods: { | |
60 | toFormatedJSON | |
61 | } | |
62 | } | |
63 | ) | |
64 | ||
65 | return Pod | |
53572423 C |
66 | } |
67 | ||
53572423 C |
68 | // ------------------------------ METHODS ------------------------------ |
69 | ||
70 | function toFormatedJSON () { | |
71 | const json = { | |
feb4bdfd | 72 | id: this.id, |
49abbbbe | 73 | host: this.host, |
53572423 | 74 | score: this.score, |
feb4bdfd | 75 | createdAt: this.createdAt |
53572423 C |
76 | } |
77 | ||
78 | return json | |
79 | } | |
80 | ||
a3ee6fa2 C |
81 | // ------------------------------ Statics ------------------------------ |
82 | ||
feb4bdfd C |
83 | function associate (models) { |
84 | this.belongsToMany(models.Request, { | |
85 | foreignKey: 'podId', | |
86 | through: models.RequestToPod, | |
7920c273 | 87 | onDelete: 'cascade' |
feb4bdfd C |
88 | }) |
89 | } | |
90 | ||
a3ee6fa2 | 91 | function countAll (callback) { |
feb4bdfd | 92 | return this.count().asCallback(callback) |
9f10b292 | 93 | } |
45239549 | 94 | |
9f10b292 C |
95 | function incrementScores (ids, value, callback) { |
96 | if (!callback) callback = function () {} | |
feb4bdfd C |
97 | |
98 | const update = { | |
99 | score: this.sequelize.literal('score +' + value) | |
100 | } | |
101 | ||
67bf9b96 | 102 | const options = { |
feb4bdfd C |
103 | where: { |
104 | id: { | |
105 | $in: ids | |
106 | } | |
67bf9b96 C |
107 | }, |
108 | // In this case score is a literal and not an integer so we do not validate it | |
109 | validate: false | |
feb4bdfd C |
110 | } |
111 | ||
67bf9b96 | 112 | return this.update(update, options).asCallback(callback) |
9f10b292 | 113 | } |
45239549 | 114 | |
9f10b292 | 115 | function list (callback) { |
feb4bdfd | 116 | return this.findAll().asCallback(callback) |
9f10b292 | 117 | } |
8c308c2b | 118 | |
ed04d94f C |
119 | function listAllIds (transaction, callback) { |
120 | if (!callback) { | |
121 | callback = transaction | |
122 | transaction = null | |
123 | } | |
124 | ||
feb4bdfd C |
125 | const query = { |
126 | attributes: [ 'id' ] | |
127 | } | |
128 | ||
ed04d94f C |
129 | if (transaction) query.transaction = transaction |
130 | ||
feb4bdfd | 131 | return this.findAll(query).asCallback(function (err, pods) { |
00057e85 C |
132 | if (err) return callback(err) |
133 | ||
feb4bdfd | 134 | return callback(null, map(pods, 'id')) |
00057e85 | 135 | }) |
528a9efa C |
136 | } |
137 | ||
bd14d16a C |
138 | function listRandomPodIdsWithRequest (limit, callback) { |
139 | const self = this | |
140 | ||
141 | self.count().asCallback(function (err, count) { | |
142 | if (err) return callback(err) | |
143 | ||
144 | // Optimization... | |
145 | if (count === 0) return callback(null, []) | |
146 | ||
147 | let start = Math.floor(Math.random() * count) - limit | |
148 | if (start < 0) start = 0 | |
149 | ||
150 | const query = { | |
151 | attributes: [ 'id' ], | |
152 | order: [ | |
153 | [ 'id', 'ASC' ] | |
154 | ], | |
155 | offset: start, | |
156 | limit: limit, | |
157 | where: { | |
158 | id: { | |
159 | $in: [ | |
160 | this.sequelize.literal('SELECT "podId" FROM "RequestToPods"') | |
161 | ] | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | return this.findAll(query).asCallback(function (err, pods) { | |
167 | if (err) return callback(err) | |
168 | ||
169 | return callback(null, map(pods, 'id')) | |
170 | }) | |
171 | }) | |
172 | } | |
173 | ||
a3ee6fa2 | 174 | function listBadPods (callback) { |
feb4bdfd C |
175 | const query = { |
176 | where: { | |
177 | score: { $lte: 0 } | |
178 | } | |
179 | } | |
180 | ||
181 | return this.findAll(query).asCallback(callback) | |
9f10b292 | 182 | } |
8c308c2b | 183 | |
a3ee6fa2 | 184 | function load (id, callback) { |
feb4bdfd | 185 | return this.findById(id).asCallback(callback) |
9f10b292 | 186 | } |
c45f7f84 | 187 | |
49abbbbe | 188 | function loadByHost (host, callback) { |
feb4bdfd C |
189 | const query = { |
190 | where: { | |
191 | host: host | |
192 | } | |
193 | } | |
194 | ||
195 | return this.findOne(query).asCallback(callback) | |
9f10b292 | 196 | } |
c45f7f84 | 197 | |
a3ee6fa2 | 198 | function removeAll (callback) { |
feb4bdfd | 199 | return this.destroy().asCallback(callback) |
a3ee6fa2 | 200 | } |