PeerTube/server/models/request/request.ts

145 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-05-15 20:22:03 +00:00
import { values } from 'lodash'
2017-05-22 18:58:25 +00:00
import * as Sequelize from 'sequelize'
2017-06-16 07:45:46 +00:00
import { database as db } from '../../initializers/database'
import { REQUEST_ENDPOINTS } from '../../initializers'
import { addMethodsToModel } from '../utils'
2017-05-22 18:58:25 +00:00
import {
RequestInstance,
RequestAttributes,
2017-06-10 20:15:25 +00:00
RequestMethods,
RequestsGrouped
2017-05-22 18:58:25 +00:00
} from './request-interface'
let Request: Sequelize.Model<RequestInstance, RequestAttributes>
let countTotalRequests: RequestMethods.CountTotalRequests
let listWithLimitAndRandom: RequestMethods.ListWithLimitAndRandom
let removeWithEmptyTo: RequestMethods.RemoveWithEmptyTo
let removeAll: RequestMethods.RemoveAll
2017-06-11 15:35:32 +00:00
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
Request = sequelize.define<RequestInstance, RequestAttributes>('Request',
2016-12-11 20:50:51 +00:00
{
request: {
2016-12-28 14:49:23 +00:00
type: DataTypes.JSON,
allowNull: false
2016-12-11 20:50:51 +00:00
},
endpoint: {
2017-05-15 20:22:03 +00:00
type: DataTypes.ENUM(values(REQUEST_ENDPOINTS)),
2016-12-28 14:49:23 +00:00
allowNull: false
2016-12-11 20:50:51 +00:00
}
2016-11-01 17:47:57 +00:00
}
2016-12-11 20:50:51 +00:00
)
2017-05-22 18:58:25 +00:00
const classMethods = [
associate,
listWithLimitAndRandom,
countTotalRequests,
removeAll,
removeWithEmptyTo
]
addMethodsToModel(Request, classMethods)
2016-12-11 20:50:51 +00:00
return Request
}
// ------------------------------ STATICS ------------------------------
2016-12-11 20:50:51 +00:00
function associate (models) {
2017-05-22 18:58:25 +00:00
Request.belongsToMany(models.Pod, {
2016-12-11 20:50:51 +00:00
foreignKey: {
name: 'requestId',
allowNull: false
},
through: models.RequestToPod,
onDelete: 'CASCADE'
})
}
countTotalRequests = function () {
2017-02-26 17:57:33 +00: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 20:50:51 +00:00
const query = {
2017-05-22 18:58:25 +00:00
include: [ Request['sequelize'].models.Pod ]
2016-12-11 20:50:51 +00:00
}
return Request.count(query)
2016-12-11 20:50:51 +00:00
}
listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
2017-06-10 20:15:25 +00:00
const Pod = db.Pod
const tableJoin = ''
return Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', tableJoin).then(podIds => {
2017-01-10 21:24:42 +00:00
// We don't have friends that have requests
if (podIds.length === 0) return []
// The first x requests of these pods
2017-01-10 21:24:42 +00:00
// It is very important to sort by id ASC to keep the requests order!
2016-12-11 20:50:51 +00:00
const query = {
order: [
[ 'id', 'ASC' ]
],
2017-01-10 21:24:42 +00:00
include: [
{
2017-05-22 18:58:25 +00:00
model: Request['sequelize'].models.Pod,
2017-01-10 21:24:42 +00:00
where: {
id: {
$in: podIds
}
}
}
]
2016-12-11 20:50:51 +00:00
}
return Request.findAll(query).then(requests => {
2017-01-10 21:24:42 +00:00
const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
return requestsGrouped
2017-01-10 21:24:42 +00:00
})
})
}
removeAll = function () {
2016-12-11 20:50:51 +00:00
// Delete all requests
return Request.truncate({ cascade: true })
}
removeWithEmptyTo = function () {
2016-12-11 20:50:51 +00:00
const query = {
where: {
id: {
$notIn: [
2017-05-22 18:58:25 +00:00
Sequelize.literal('SELECT "requestId" FROM "RequestToPods"')
2016-12-11 20:50:51 +00:00
]
}
}
}
return Request.destroy(query)
}
// ---------------------------------------------------------------------------
2017-06-10 20:15:25 +00:00
function groupAndTruncateRequests (requests: RequestInstance[], limitRequestsPerPod: number) {
const requestsGrouped: RequestsGrouped = {}
2017-07-11 15:04:57 +00:00
requests.forEach(request => {
request.Pods.forEach(pod => {
if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
requestsGrouped[pod.id].push({
request,
pod
})
}
})
})
return requestsGrouped
}