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

151 lines
3.8 KiB
TypeScript
Raw Normal View History

2017-05-15 16:22:03 -04:00
import { values } from 'lodash'
2017-05-22 14:58:25 -04:00
import * as Sequelize from 'sequelize'
2017-06-10 16:15:25 -04:00
import { database as db } from '../initializers/database'
2017-05-15 16:22:03 -04:00
import { REQUEST_ENDPOINTS } from '../initializers'
2017-05-22 14:58:25 -04:00
import { addMethodsToModel } from './utils'
import {
RequestClass,
RequestInstance,
RequestAttributes,
2017-06-10 16:15:25 -04:00
RequestMethods,
RequestsGrouped
2017-05-22 14:58:25 -04: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-05-22 14:58:25 -04:00
export default function (sequelize, DataTypes) {
Request = sequelize.define('Request',
2016-12-11 15:50:51 -05:00
{
request: {
2016-12-28 09:49:23 -05:00
type: DataTypes.JSON,
allowNull: false
2016-12-11 15:50:51 -05:00
},
endpoint: {
2017-05-15 16:22:03 -04:00
type: DataTypes.ENUM(values(REQUEST_ENDPOINTS)),
2016-12-28 09:49:23 -05:00
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
)
2017-05-22 14:58:25 -04:00
const classMethods = [
associate,
listWithLimitAndRandom,
countTotalRequests,
removeAll,
removeWithEmptyTo
]
addMethodsToModel(Request, classMethods)
2016-12-11 15:50:51 -05:00
return Request
}
// ------------------------------ STATICS ------------------------------
2016-12-11 15:50:51 -05:00
function associate (models) {
2017-05-22 14:58:25 -04:00
Request.belongsToMany(models.Pod, {
2016-12-11 15:50:51 -05:00
foreignKey: {
name: 'requestId',
allowNull: false
},
through: models.RequestToPod,
onDelete: 'CASCADE'
})
}
2017-06-10 16:15:25 -04:00
countTotalRequests = function (callback: RequestMethods.CountTotalRequestsCallback) {
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 = {
2017-05-22 14:58:25 -04:00
include: [ Request['sequelize'].models.Pod ]
2016-12-11 15:50:51 -05:00
}
2017-05-22 14:58:25 -04:00
return Request.count(query).asCallback(callback)
2016-12-11 15:50:51 -05:00
}
2017-06-10 16:15:25 -04:00
listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestMethods.ListWithLimitAndRandomCallback) {
const Pod = db.Pod
const tableJoin = ''
2017-06-10 16:15:25 -04:00
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: [
{
2017-05-22 14:58:25 -04:00
model: Request['sequelize'].models.Pod,
2017-01-10 16:24:42 -05:00
where: {
id: {
$in: podIds
}
}
}
]
2016-12-11 15:50:51 -05:00
}
2017-05-22 14:58:25 -04:00
Request.findAll(query).asCallback(function (err, requests) {
2017-01-10 16:24:42 -05:00
if (err) return callback(err)
const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
return callback(err, requestsGrouped)
})
})
}
2017-06-10 16:15:25 -04:00
removeAll = function (callback: RequestMethods.RemoveAllCallback) {
2016-12-11 15:50:51 -05:00
// Delete all requests
2017-05-22 14:58:25 -04:00
Request.truncate({ cascade: true }).asCallback(callback)
}
2017-06-10 16:15:25 -04:00
removeWithEmptyTo = function (callback?: RequestMethods.RemoveWithEmptyToCallback) {
2017-05-15 16:22:03 -04:00
if (!callback) callback = function () { /* empty */ }
2016-12-11 15:50:51 -05:00
const query = {
where: {
id: {
$notIn: [
2017-05-22 14:58:25 -04:00
Sequelize.literal('SELECT "requestId" FROM "RequestToPods"')
2016-12-11 15:50:51 -05:00
]
}
}
}
2017-05-22 14:58:25 -04:00
Request.destroy(query).asCallback(callback)
}
// ---------------------------------------------------------------------------
2017-06-10 16:15:25 -04:00
function groupAndTruncateRequests (requests: RequestInstance[], limitRequestsPerPod: number) {
const requestsGrouped: 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
}