]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/pod.js
Add email to users
[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),
ad4a8a1c
C
38 allowNull: false,
39 validate: {
40 isEmail: true
41 }
feb4bdfd 42 }
feb4bdfd
C
43 },
44 {
319d072e
C
45 indexes: [
46 {
5d67f289
C
47 fields: [ 'host' ],
48 unique: true
319d072e
C
49 },
50 {
51 fields: [ 'score' ]
52 }
53 ],
feb4bdfd
C
54 classMethods: {
55 associate,
56
57 countAll,
58 incrementScores,
59 list,
60 listAllIds,
bd14d16a 61 listRandomPodIdsWithRequest,
feb4bdfd
C
62 listBadPods,
63 load,
64 loadByHost,
65 removeAll
66 },
67 instanceMethods: {
68 toFormatedJSON
69 }
70 }
71 )
72
73 return Pod
53572423
C
74}
75
53572423
C
76// ------------------------------ METHODS ------------------------------
77
78function toFormatedJSON () {
79 const json = {
feb4bdfd 80 id: this.id,
49abbbbe 81 host: this.host,
4793c343 82 email: this.email,
53572423 83 score: this.score,
feb4bdfd 84 createdAt: this.createdAt
53572423
C
85 }
86
87 return json
88}
89
a3ee6fa2
C
90// ------------------------------ Statics ------------------------------
91
feb4bdfd
C
92function associate (models) {
93 this.belongsToMany(models.Request, {
94 foreignKey: 'podId',
95 through: models.RequestToPod,
7920c273 96 onDelete: 'cascade'
feb4bdfd
C
97 })
98}
99
a3ee6fa2 100function countAll (callback) {
feb4bdfd 101 return this.count().asCallback(callback)
9f10b292 102}
45239549 103
9f10b292
C
104function incrementScores (ids, value, callback) {
105 if (!callback) callback = function () {}
feb4bdfd
C
106
107 const update = {
108 score: this.sequelize.literal('score +' + value)
109 }
110
67bf9b96 111 const options = {
feb4bdfd
C
112 where: {
113 id: {
114 $in: ids
115 }
67bf9b96
C
116 },
117 // In this case score is a literal and not an integer so we do not validate it
118 validate: false
feb4bdfd
C
119 }
120
67bf9b96 121 return this.update(update, options).asCallback(callback)
9f10b292 122}
45239549 123
9f10b292 124function list (callback) {
feb4bdfd 125 return this.findAll().asCallback(callback)
9f10b292 126}
8c308c2b 127
ed04d94f
C
128function listAllIds (transaction, callback) {
129 if (!callback) {
130 callback = transaction
131 transaction = null
132 }
133
feb4bdfd
C
134 const query = {
135 attributes: [ 'id' ]
136 }
137
ed04d94f
C
138 if (transaction) query.transaction = transaction
139
feb4bdfd 140 return this.findAll(query).asCallback(function (err, pods) {
00057e85
C
141 if (err) return callback(err)
142
feb4bdfd 143 return callback(null, map(pods, 'id'))
00057e85 144 })
528a9efa
C
145}
146
bd14d16a
C
147function listRandomPodIdsWithRequest (limit, callback) {
148 const self = this
149
150 self.count().asCallback(function (err, count) {
151 if (err) return callback(err)
152
153 // Optimization...
154 if (count === 0) return callback(null, [])
155
156 let start = Math.floor(Math.random() * count) - limit
157 if (start < 0) start = 0
158
159 const query = {
160 attributes: [ 'id' ],
161 order: [
162 [ 'id', 'ASC' ]
163 ],
164 offset: start,
165 limit: limit,
166 where: {
167 id: {
168 $in: [
169 this.sequelize.literal('SELECT "podId" FROM "RequestToPods"')
170 ]
171 }
172 }
173 }
174
175 return this.findAll(query).asCallback(function (err, pods) {
176 if (err) return callback(err)
177
178 return callback(null, map(pods, 'id'))
179 })
180 })
181}
182
a3ee6fa2 183function listBadPods (callback) {
feb4bdfd
C
184 const query = {
185 where: {
186 score: { $lte: 0 }
187 }
188 }
189
190 return this.findAll(query).asCallback(callback)
9f10b292 191}
8c308c2b 192
a3ee6fa2 193function load (id, callback) {
feb4bdfd 194 return this.findById(id).asCallback(callback)
9f10b292 195}
c45f7f84 196
49abbbbe 197function loadByHost (host, callback) {
feb4bdfd
C
198 const query = {
199 where: {
200 host: host
201 }
202 }
203
204 return this.findOne(query).asCallback(callback)
9f10b292 205}
c45f7f84 206
a3ee6fa2 207function removeAll (callback) {
feb4bdfd 208 return this.destroy().asCallback(callback)
a3ee6fa2 209}