PeerTube/server/models/request/request-video-event.ts

181 lines
4.7 KiB
TypeScript
Raw Normal View History

2017-02-26 11:57:33 -06:00
/*
Request Video events (likes, dislikes, views...)
*/
2017-05-15 15:22:03 -05:00
import { values } from 'lodash'
2017-05-22 13:58:25 -05:00
import * as Sequelize from 'sequelize'
2017-02-26 11:57:33 -06:00
2017-06-16 02:45:46 -05:00
import { database as db } from '../../initializers/database'
import { REQUEST_VIDEO_EVENT_TYPES } from '../../initializers'
import { isVideoEventCountValid } from '../../helpers'
import { addMethodsToModel } from '../utils'
2017-05-22 13:58:25 -05:00
import {
RequestVideoEventInstance,
RequestVideoEventAttributes,
2017-06-10 15:15:25 -05:00
RequestVideoEventMethods,
RequestsVideoEventGrouped
2017-05-22 13:58:25 -05:00
} from './request-video-event-interface'
let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes>
let countTotalRequests: RequestVideoEventMethods.CountTotalRequests
let listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom
let removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod
let removeAll: RequestVideoEventMethods.RemoveAll
2017-02-26 11:57:33 -06:00
2017-06-11 10:35:32 -05:00
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
RequestVideoEvent = sequelize.define<RequestVideoEventInstance, RequestVideoEventAttributes>('RequestVideoEvent',
2017-02-26 11:57:33 -06:00
{
type: {
2017-05-15 15:22:03 -05:00
type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)),
2017-02-26 11:57:33 -06:00
allowNull: false
},
count: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
countValid: function (value) {
2017-05-15 15:22:03 -05:00
const res = isVideoEventCountValid(value)
2017-02-26 11:57:33 -06:00
if (res === false) throw new Error('Video event count is not valid.')
}
}
}
},
{
updatedAt: false,
indexes: [
{
fields: [ 'videoId' ]
}
2017-05-22 13:58:25 -05:00
]
2017-02-26 11:57:33 -06:00
}
)
2017-05-22 13:58:25 -05:00
const classMethods = [
associate,
listWithLimitAndRandom,
countTotalRequests,
removeAll,
removeByRequestIdsAndPod
]
addMethodsToModel(RequestVideoEvent, classMethods)
2017-02-26 11:57:33 -06:00
return RequestVideoEvent
}
// ------------------------------ STATICS ------------------------------
function associate (models) {
2017-05-22 13:58:25 -05:00
RequestVideoEvent.belongsTo(models.Video, {
2017-02-26 11:57:33 -06:00
foreignKey: {
name: 'videoId',
allowNull: false
},
onDelete: 'CASCADE'
})
}
countTotalRequests = function () {
2017-02-26 11:57:33 -06:00
const query = {}
return RequestVideoEvent.count(query)
2017-02-26 11:57:33 -06:00
}
listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
2017-06-10 15:15:25 -05:00
const Pod = db.Pod
2017-02-26 11:57:33 -06:00
// We make a join between videos and authors to find the podId of our video event requests
const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' +
'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"'
return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => {
2017-02-26 11:57:33 -06:00
// We don't have friends that have requests
if (podIds.length === 0) return []
2017-02-26 11:57:33 -06:00
const query = {
2017-03-08 14:35:43 -06:00
order: [
[ 'id', 'ASC' ]
],
2017-02-26 11:57:33 -06:00
include: [
{
2017-05-22 13:58:25 -05:00
model: RequestVideoEvent['sequelize'].models.Video,
2017-02-26 11:57:33 -06:00
include: [
{
2017-05-22 13:58:25 -05:00
model: RequestVideoEvent['sequelize'].models.Author,
2017-02-26 11:57:33 -06:00
include: [
{
2017-05-22 13:58:25 -05:00
model: RequestVideoEvent['sequelize'].models.Pod,
2017-02-26 11:57:33 -06:00
where: {
id: {
$in: podIds
}
}
}
]
}
]
}
]
}
return RequestVideoEvent.findAll(query).then(requests => {
2017-02-26 11:57:33 -06:00
const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
return requestsGrouped
2017-02-26 11:57:33 -06:00
})
})
}
removeByRequestIdsAndPod = function (ids: number[], podId: number) {
2017-02-26 11:57:33 -06:00
const query = {
where: {
id: {
$in: ids
}
},
include: [
{
2017-05-22 13:58:25 -05:00
model: RequestVideoEvent['sequelize'].models.Video,
2017-02-26 11:57:33 -06:00
include: [
{
2017-05-22 13:58:25 -05:00
model: RequestVideoEvent['sequelize'].models.Author,
2017-02-26 11:57:33 -06:00
where: {
podId
}
}
]
}
]
}
return RequestVideoEvent.destroy(query)
2017-02-26 11:57:33 -06:00
}
removeAll = function () {
2017-02-26 11:57:33 -06:00
// Delete all requests
return RequestVideoEvent.truncate({ cascade: true })
2017-02-26 11:57:33 -06:00
}
// ---------------------------------------------------------------------------
2017-06-10 15:15:25 -05:00
function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) {
const eventsGrouped: RequestsVideoEventGrouped = {}
2017-02-26 11:57:33 -06:00
events.forEach(function (event) {
const pod = event.Video.Author.Pod
if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = []
if (eventsGrouped[pod.id].length < limitRequestsPerPod) {
eventsGrouped[pod.id].push({
id: event.id,
type: event.type,
count: event.count,
video: event.Video,
pod
})
}
})
return eventsGrouped
}