2016-02-07 05:23:23 -05:00
|
|
|
'use strict'
|
|
|
|
|
2016-06-28 14:10:32 -04:00
|
|
|
const map = require('lodash/map')
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const constants = require('../initializers/constants')
|
2016-12-28 09:49:23 -05:00
|
|
|
const customPodsValidators = require('../helpers/custom-validators').pods
|
2016-02-07 05:23:23 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
module.exports = function (sequelize, DataTypes) {
|
|
|
|
const Pod = sequelize.define('Pod',
|
|
|
|
{
|
|
|
|
host: {
|
2016-12-28 09:49:23 -05: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 15:50:51 -05:00
|
|
|
},
|
|
|
|
publicKey: {
|
2016-12-28 09:49:23 -05:00
|
|
|
type: DataTypes.STRING(5000),
|
|
|
|
allowNull: false
|
2016-12-11 15:50:51 -05:00
|
|
|
},
|
|
|
|
score: {
|
|
|
|
type: DataTypes.INTEGER,
|
2016-12-28 09:49:23 -05:00
|
|
|
defaultValue: constants.FRIEND_SCORE.BASE,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isInt: true,
|
|
|
|
max: constants.FRIEND_SCORE.MAX
|
|
|
|
}
|
2017-02-16 13:19:56 -05:00
|
|
|
},
|
|
|
|
email: {
|
|
|
|
type: DataTypes.STRING(400),
|
2017-02-18 03:29:59 -05:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isEmail: true
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-12-29 03:33:28 -05:00
|
|
|
indexes: [
|
|
|
|
{
|
2017-02-16 13:24:34 -05:00
|
|
|
fields: [ 'host' ],
|
|
|
|
unique: true
|
2016-12-29 03:33:28 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'score' ]
|
|
|
|
}
|
|
|
|
],
|
2016-12-11 15:50:51 -05:00
|
|
|
classMethods: {
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countAll,
|
|
|
|
incrementScores,
|
|
|
|
list,
|
|
|
|
listAllIds,
|
2017-01-10 16:24:42 -05:00
|
|
|
listRandomPodIdsWithRequest,
|
2016-12-11 15:50:51 -05:00
|
|
|
listBadPods,
|
|
|
|
load,
|
|
|
|
loadByHost,
|
|
|
|
removeAll
|
|
|
|
},
|
|
|
|
instanceMethods: {
|
|
|
|
toFormatedJSON
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return Pod
|
2016-08-26 12:55:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
|
|
|
function toFormatedJSON () {
|
|
|
|
const json = {
|
2016-12-11 15:50:51 -05:00
|
|
|
id: this.id,
|
2016-11-14 14:03:04 -05:00
|
|
|
host: this.host,
|
2017-02-16 13:19:56 -05:00
|
|
|
email: this.email,
|
2016-08-26 12:55:10 -04:00
|
|
|
score: this.score,
|
2016-12-11 15:50:51 -05:00
|
|
|
createdAt: this.createdAt
|
2016-08-26 12:55:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
// ------------------------------ Statics ------------------------------
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
function associate (models) {
|
|
|
|
this.belongsToMany(models.Request, {
|
|
|
|
foreignKey: 'podId',
|
|
|
|
through: models.RequestToPod,
|
2016-12-24 10:59:17 -05:00
|
|
|
onDelete: 'cascade'
|
2016-12-11 15:50:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
function countAll (callback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
return this.count().asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-23 12:31:58 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function incrementScores (ids, value, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
2016-12-11 15:50:51 -05:00
|
|
|
|
|
|
|
const update = {
|
|
|
|
score: this.sequelize.literal('score +' + value)
|
|
|
|
}
|
|
|
|
|
2016-12-28 09:49:23 -05:00
|
|
|
const options = {
|
2016-12-11 15:50:51 -05:00
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
$in: ids
|
|
|
|
}
|
2016-12-28 09:49:23 -05:00
|
|
|
},
|
|
|
|
// In this case score is a literal and not an integer so we do not validate it
|
|
|
|
validate: false
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2016-12-28 09:49:23 -05:00
|
|
|
return this.update(update, options).asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-23 12:31:58 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function list (callback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
return this.findAll().asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2017-01-06 17:24:47 -05:00
|
|
|
function listAllIds (transaction, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = transaction
|
|
|
|
transaction = null
|
|
|
|
}
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
attributes: [ 'id' ]
|
|
|
|
}
|
|
|
|
|
2017-01-06 17:24:47 -05:00
|
|
|
if (transaction) query.transaction = transaction
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
return this.findAll(query).asCallback(function (err, pods) {
|
2016-06-28 14:10:32 -04:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
return callback(null, map(pods, 'id'))
|
2016-06-28 14:10:32 -04:00
|
|
|
})
|
2016-06-18 10:13:54 -04:00
|
|
|
}
|
|
|
|
|
2017-01-10 16:24:42 -05:00
|
|
|
function listRandomPodIdsWithRequest (limit, callback) {
|
|
|
|
const self = this
|
|
|
|
|
|
|
|
self.count().asCallback(function (err, count) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
// Optimization...
|
|
|
|
if (count === 0) return callback(null, [])
|
|
|
|
|
|
|
|
let start = Math.floor(Math.random() * count) - limit
|
|
|
|
if (start < 0) start = 0
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
attributes: [ 'id' ],
|
|
|
|
order: [
|
|
|
|
[ 'id', 'ASC' ]
|
|
|
|
],
|
|
|
|
offset: start,
|
|
|
|
limit: limit,
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
$in: [
|
|
|
|
this.sequelize.literal('SELECT "podId" FROM "RequestToPods"')
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findAll(query).asCallback(function (err, pods) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, map(pods, 'id'))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
function listBadPods (callback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
score: { $lte: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findAll(query).asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
function load (id, callback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
return this.findById(id).asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-11-14 14:03:04 -05:00
|
|
|
function loadByHost (host, callback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
host: host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findOne(query).asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
function removeAll (callback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
return this.destroy().asCallback(callback)
|
2016-06-30 16:39:08 -04:00
|
|
|
}
|