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