]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/pod.js
8e7dd1fd8d7f71a805ff087fd98072ef4f3df9a7
3 const map
= require('lodash/map')
5 const constants
= require('../initializers/constants')
6 const customPodsValidators
= require('../helpers/custom-validators').pods
8 // ---------------------------------------------------------------------------
10 module
.exports = function (sequelize
, DataTypes
) {
11 const Pod
= sequelize
.define('Pod',
14 type: DataTypes
.STRING
,
17 isHost: function (value
) {
18 const res
= customPodsValidators
.isHostValid(value
)
19 if (res
=== false) throw new Error('Host not valid.')
24 type: DataTypes
.STRING(5000),
28 type: DataTypes
.INTEGER
,
29 defaultValue: constants
.FRIEND_SCORE
.BASE
,
33 max: constants
.FRIEND_SCORE
.MAX
67 // ------------------------------ METHODS ------------------------------
69 function toFormatedJSON () {
74 createdAt: this.createdAt
80 // ------------------------------ Statics ------------------------------
82 function associate (models
) {
83 this.belongsToMany(models
.Request
, {
85 through: models
.RequestToPod
,
90 function countAll (callback
) {
91 return this.count().asCallback(callback
)
94 function incrementScores (ids
, value
, callback
) {
95 if (!callback
) callback = function () {}
98 score: this.sequelize
.literal('score +' + value
)
107 // In this case score is a literal and not an integer so we do not validate it
111 return this.update(update
, options
).asCallback(callback
)
114 function list (callback
) {
115 return this.findAll().asCallback(callback
)
118 function listAllIds (transaction
, callback
) {
120 callback
= transaction
128 if (transaction
) query
.transaction
= transaction
130 return this.findAll(query
).asCallback(function (err
, pods
) {
131 if (err
) return callback(err
)
133 return callback(null, map(pods
, 'id'))
137 function listBadPods (callback
) {
144 return this.findAll(query
).asCallback(callback
)
147 function load (id
, callback
) {
148 return this.findById(id
).asCallback(callback
)
151 function loadByHost (host
, callback
) {
158 return this.findOne(query
).asCallback(callback
)
161 function removeAll (callback
) {
162 return this.destroy().asCallback(callback
)