PeerTube/server/models/request.js

138 lines
3.1 KiB
JavaScript
Raw Normal View History

'use strict'
2016-12-28 08:49:23 -06:00
const values = require('lodash/values')
2016-03-16 16:29:27 -05:00
const constants = require('../initializers/constants')
// ---------------------------------------------------------------------------
2016-12-11 14:50:51 -06:00
module.exports = function (sequelize, DataTypes) {
const Request = sequelize.define('Request',
{
request: {
2016-12-28 08:49:23 -06:00
type: DataTypes.JSON,
allowNull: false
2016-12-11 14:50:51 -06:00
},
endpoint: {
2016-12-28 08:49:23 -06:00
type: DataTypes.ENUM(values(constants.REQUEST_ENDPOINTS)),
allowNull: false
2016-12-11 14:50:51 -06:00
}
},
2016-11-01 12:47:57 -05:00
{
2016-12-11 14:50:51 -06:00
classMethods: {
associate,
listWithLimitAndRandom,
2016-12-11 14:50:51 -06:00
countTotalRequests,
removeAll,
removeWithEmptyTo
2016-12-11 14:50:51 -06:00
}
2016-11-01 12:47:57 -05:00
}
2016-12-11 14:50:51 -06:00
)
2016-12-11 14:50:51 -06:00
return Request
}
// ------------------------------ STATICS ------------------------------
2016-12-11 14:50:51 -06:00
function associate (models) {
this.belongsToMany(models.Pod, {
foreignKey: {
name: 'requestId',
allowNull: false
},
through: models.RequestToPod,
onDelete: 'CASCADE'
})
}
function countTotalRequests (callback) {
2017-02-26 11:57:33 -06:00
// We need to include Pod because there are no cascade delete when a pod is removed
// So we could count requests that do not have existing pod anymore
2016-12-11 14:50:51 -06:00
const query = {
include: [ this.sequelize.models.Pod ]
}
return this.count(query).asCallback(callback)
}
2017-01-10 15:24:42 -06:00
function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
const self = this
2017-01-10 15:24:42 -06:00
const Pod = this.sequelize.models.Pod
Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) {
if (err) return callback(err)
2017-01-10 15:24:42 -06:00
// We don't have friends that have requests
if (podIds.length === 0) return callback(null, [])
// The first x requests of these pods
2017-01-10 15:24:42 -06:00
// It is very important to sort by id ASC to keep the requests order!
2016-12-11 14:50:51 -06:00
const query = {
order: [
[ 'id', 'ASC' ]
],
2017-01-10 15:24:42 -06:00
include: [
{
model: self.sequelize.models.Pod,
where: {
id: {
$in: podIds
}
}
}
]
2016-12-11 14:50:51 -06:00
}
2017-01-10 15:24:42 -06:00
self.findAll(query).asCallback(function (err, requests) {
if (err) return callback(err)
const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
return callback(err, requestsGrouped)
})
})
}
function removeAll (callback) {
2016-12-11 14:50:51 -06:00
// Delete all requests
2016-12-24 09:59:17 -06:00
this.truncate({ cascade: true }).asCallback(callback)
}
function removeWithEmptyTo (callback) {
if (!callback) callback = function () {}
2016-12-11 14:50:51 -06:00
const query = {
where: {
id: {
$notIn: [
this.sequelize.literal('SELECT "requestId" FROM "RequestToPods"')
]
}
}
}
this.destroy(query).asCallback(callback)
}
// ---------------------------------------------------------------------------
function groupAndTruncateRequests (requests, limitRequestsPerPod) {
const requestsGrouped = {}
requests.forEach(function (request) {
request.Pods.forEach(function (pod) {
if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
requestsGrouped[pod.id].push({
request,
pod
})
}
})
})
return requestsGrouped
}