]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/pod.js
Server: add unique to unique indexes
[github/Chocobozzz/PeerTube.git] / server / models / pod.js
CommitLineData
9f10b292
C
1'use strict'
2
00057e85 3const map = require('lodash/map')
9f10b292 4
f0f5567b 5const constants = require('../initializers/constants')
67bf9b96 6const customPodsValidators = require('../helpers/custom-validators').pods
9f10b292
C
7
8// ---------------------------------------------------------------------------
9
feb4bdfd
C
10module.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 }
4793c343
C
35 },
36 email: {
37 type: DataTypes.STRING(400),
38 allowNull: false
feb4bdfd 39 }
feb4bdfd
C
40 },
41 {
319d072e
C
42 indexes: [
43 {
5d67f289
C
44 fields: [ 'host' ],
45 unique: true
319d072e
C
46 },
47 {
48 fields: [ 'score' ]
49 }
50 ],
feb4bdfd
C
51 classMethods: {
52 associate,
53
54 countAll,
55 incrementScores,
56 list,
57 listAllIds,
bd14d16a 58 listRandomPodIdsWithRequest,
feb4bdfd
C
59 listBadPods,
60 load,
61 loadByHost,
62 removeAll
63 },
64 instanceMethods: {
65 toFormatedJSON
66 }
67 }
68 )
69
70 return Pod
53572423
C
71}
72
53572423
C
73// ------------------------------ METHODS ------------------------------
74
75function toFormatedJSON () {
76 const json = {
feb4bdfd 77 id: this.id,
49abbbbe 78 host: this.host,
4793c343 79 email: this.email,
53572423 80 score: this.score,
feb4bdfd 81 createdAt: this.createdAt
53572423
C
82 }
83
84 return json
85}
86
a3ee6fa2
C
87// ------------------------------ Statics ------------------------------
88
feb4bdfd
C
89function associate (models) {
90 this.belongsToMany(models.Request, {
91 foreignKey: 'podId',
92 through: models.RequestToPod,
7920c273 93 onDelete: 'cascade'
feb4bdfd
C
94 })
95}
96
a3ee6fa2 97function countAll (callback) {
feb4bdfd 98 return this.count().asCallback(callback)
9f10b292 99}
45239549 100
9f10b292
C
101function incrementScores (ids, value, callback) {
102 if (!callback) callback = function () {}
feb4bdfd
C
103
104 const update = {
105 score: this.sequelize.literal('score +' + value)
106 }
107
67bf9b96 108 const options = {
feb4bdfd
C
109 where: {
110 id: {
111 $in: ids
112 }
67bf9b96
C
113 },
114 // In this case score is a literal and not an integer so we do not validate it
115 validate: false
feb4bdfd
C
116 }
117
67bf9b96 118 return this.update(update, options).asCallback(callback)
9f10b292 119}
45239549 120
9f10b292 121function list (callback) {
feb4bdfd 122 return this.findAll().asCallback(callback)
9f10b292 123}
8c308c2b 124
ed04d94f
C
125function listAllIds (transaction, callback) {
126 if (!callback) {
127 callback = transaction
128 transaction = null
129 }
130
feb4bdfd
C
131 const query = {
132 attributes: [ 'id' ]
133 }
134
ed04d94f
C
135 if (transaction) query.transaction = transaction
136
feb4bdfd 137 return this.findAll(query).asCallback(function (err, pods) {
00057e85
C
138 if (err) return callback(err)
139
feb4bdfd 140 return callback(null, map(pods, 'id'))
00057e85 141 })
528a9efa
C
142}
143
bd14d16a
C
144function 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
a3ee6fa2 180function listBadPods (callback) {
feb4bdfd
C
181 const query = {
182 where: {
183 score: { $lte: 0 }
184 }
185 }
186
187 return this.findAll(query).asCallback(callback)
9f10b292 188}
8c308c2b 189
a3ee6fa2 190function load (id, callback) {
feb4bdfd 191 return this.findById(id).asCallback(callback)
9f10b292 192}
c45f7f84 193
49abbbbe 194function loadByHost (host, callback) {
feb4bdfd
C
195 const query = {
196 where: {
197 host: host
198 }
199 }
200
201 return this.findOne(query).asCallback(callback)
9f10b292 202}
c45f7f84 203
a3ee6fa2 204function removeAll (callback) {
feb4bdfd 205 return this.destroy().asCallback(callback)
a3ee6fa2 206}