PeerTube/server/models/pod.js

157 lines
3.0 KiB
JavaScript
Raw Normal View History

'use strict'
const map = require('lodash/map')
2016-03-16 16:29:27 -05:00
const constants = require('../initializers/constants')
2016-12-28 08:49:23 -06:00
const customPodsValidators = require('../helpers/custom-validators').pods
// ---------------------------------------------------------------------------
2016-12-11 14:50:51 -06:00
module.exports = function (sequelize, DataTypes) {
const Pod = sequelize.define('Pod',
{
host: {
2016-12-28 08:49:23 -06:00
type: DataTypes.STRING,
allowNull: false,
validate: {
isHost: function (value) {
const res = customPodsValidators.isHostValid(value)
if (res === false) throw new Error('Host not valid.')
}
}
2016-12-11 14:50:51 -06:00
},
publicKey: {
2016-12-28 08:49:23 -06:00
type: DataTypes.STRING(5000),
allowNull: false
2016-12-11 14:50:51 -06:00
},
score: {
type: DataTypes.INTEGER,
2016-12-28 08:49:23 -06:00
defaultValue: constants.FRIEND_SCORE.BASE,
allowNull: false,
validate: {
isInt: true,
max: constants.FRIEND_SCORE.MAX
}
2016-12-11 14:50:51 -06:00
}
},
{
2016-12-29 02:33:28 -06:00
indexes: [
{
fields: [ 'host' ]
},
{
fields: [ 'score' ]
}
],
2016-12-11 14:50:51 -06:00
classMethods: {
associate,
countAll,
incrementScores,
list,
listAllIds,
listBadPods,
load,
loadByHost,
removeAll
},
instanceMethods: {
toFormatedJSON
}
}
)
return Pod
}
// ------------------------------ METHODS ------------------------------
function toFormatedJSON () {
const json = {
2016-12-11 14:50:51 -06:00
id: this.id,
host: this.host,
score: this.score,
2016-12-11 14:50:51 -06:00
createdAt: this.createdAt
}
return json
}
// ------------------------------ Statics ------------------------------
2016-12-11 14:50:51 -06:00
function associate (models) {
this.belongsToMany(models.Request, {
foreignKey: 'podId',
through: models.RequestToPod,
2016-12-24 09:59:17 -06:00
onDelete: 'cascade'
2016-12-11 14:50:51 -06:00
})
}
function countAll (callback) {
2016-12-11 14:50:51 -06:00
return this.count().asCallback(callback)
}
function incrementScores (ids, value, callback) {
if (!callback) callback = function () {}
2016-12-11 14:50:51 -06:00
const update = {
score: this.sequelize.literal('score +' + value)
}
2016-12-28 08:49:23 -06:00
const options = {
2016-12-11 14:50:51 -06:00
where: {
id: {
$in: ids
}
2016-12-28 08:49:23 -06:00
},
// In this case score is a literal and not an integer so we do not validate it
validate: false
2016-12-11 14:50:51 -06:00
}
2016-12-28 08:49:23 -06:00
return this.update(update, options).asCallback(callback)
}
function list (callback) {
2016-12-11 14:50:51 -06:00
return this.findAll().asCallback(callback)
}
2015-06-09 10:41:40 -05:00
function listAllIds (callback) {
2016-12-11 14:50:51 -06:00
const query = {
attributes: [ 'id' ]
}
return this.findAll(query).asCallback(function (err, pods) {
if (err) return callback(err)
2016-12-11 14:50:51 -06:00
return callback(null, map(pods, 'id'))
})
}
function listBadPods (callback) {
2016-12-11 14:50:51 -06:00
const query = {
where: {
score: { $lte: 0 }
}
}
return this.findAll(query).asCallback(callback)
}
2015-06-09 10:41:40 -05:00
function load (id, callback) {
2016-12-11 14:50:51 -06:00
return this.findById(id).asCallback(callback)
}
2016-01-31 04:23:52 -06:00
function loadByHost (host, callback) {
2016-12-11 14:50:51 -06:00
const query = {
where: {
host: host
}
}
return this.findOne(query).asCallback(callback)
}
2016-01-31 04:23:52 -06:00
function removeAll (callback) {
2016-12-11 14:50:51 -06:00
return this.destroy().asCallback(callback)
}