1
0
Fork 0
peertube/server/models/request.js

138 lines
3.1 KiB
JavaScript
Raw Normal View History

'use strict'
2016-12-28 09:49:23 -05:00
const values = require('lodash/values')
2016-03-16 17:29:27 -04:00
const constants = require('../initializers/constants')
// ---------------------------------------------------------------------------
2016-12-11 15:50:51 -05:00
module.exports = function (sequelize, DataTypes) {
const Request = sequelize.define('Request',
{
request: {
2016-12-28 09:49:23 -05:00
type: DataTypes.JSON,
allowNull: false
2016-12-11 15:50:51 -05:00
},
endpoint: {
2016-12-28 09:49:23 -05:00
type: DataTypes.ENUM(values(constants.REQUEST_ENDPOINTS)),
allowNull: false
2016-12-11 15:50:51 -05:00
}
},
2016-11-01 13:47:57 -04:00
{
2016-12-11 15:50:51 -05:00
classMethods: {
associate,
listWithLimitAndRandom,
2016-12-11 15:50:51 -05:00
countTotalRequests,
removeAll,
removeWithEmptyTo
2016-12-11 15:50:51 -05:00
}
2016-11-01 13:47:57 -04:00
}
2016-12-11 15:50:51 -05:00
)
2016-12-11 15:50:51 -05:00
return Request
}
// ------------------------------ STATICS ------------------------------
2016-12-11 15:50:51 -05:00
function associate (models) {
this.belongsToMany(models.Pod, {
foreignKey: {
name: 'requestId',
allowNull: false
},
through: models.RequestToPod,
onDelete: 'CASCADE'
})
}
function countTotalRequests (callback) {
2017-02-26 12:57:33 -05: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 15:50:51 -05:00
const query = {
include: [ this.sequelize.models.Pod ]
}
return this.count(query).asCallback(callback)
}
2017-01-10 16:24:42 -05:00
function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
const self = this
2017-01-10 16:24:42 -05:00
const Pod = this.sequelize.models.Pod
Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) {
if (err) return callback(err)
2017-01-10 16:24:42 -05: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 16:24:42 -05:00
// It is very important to sort by id ASC to keep the requests order!
2016-12-11 15:50:51 -05:00
const query = {
order: [
[ 'id', 'ASC' ]
],
2017-01-10 16:24:42 -05:00
include: [
{
model: self.sequelize.models.Pod,
where: {
id: {
$in: podIds
}
}
}
]
2016-12-11 15:50:51 -05:00
}
2017-01-10 16:24:42 -05: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 15:50:51 -05:00
// Delete all requests
2016-12-24 10:59:17 -05:00
this.truncate({ cascade: true }).asCallback(callback)
}
function removeWithEmptyTo (callback) {
if (!callback) callback = function () {}
2016-12-11 15:50:51 -05: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
}