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