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

192 lines
5.1 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
2017-10-24 12:41:09 -05:00
const podJoins = 'INNER JOIN "VideoChannels" ON "VideoChannels"."authorId" = "Authors"."id" ' +
'INNER JOIN "Videos" ON "Videos"."channelId" = "VideoChannels"."id" ' +
2017-02-26 11:57:33 -06:00
'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-10-26 04:42:36 -05:00
model: RequestVideoEvent['sequelize'].models.VideoChannel,
2017-02-26 11:57:33 -06:00
include: [
{
2017-10-26 04:42:36 -05:00
model: RequestVideoEvent['sequelize'].models.Author,
include: [
{
model: RequestVideoEvent['sequelize'].models.Pod,
where: {
id: {
$in: podIds
}
}
2017-02-26 11:57:33 -06:00
}
2017-10-26 04:42:36 -05:00
]
2017-02-26 11:57:33 -06:00
}
]
}
]
}
]
}
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-10-26 04:42:36 -05:00
model: RequestVideoEvent['sequelize'].models.VideoChannel,
include: [
{
model: RequestVideoEvent['sequelize'].models.Author,
where: {
podId
}
}
]
2017-02-26 11:57:33 -06:00
}
]
}
]
}
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
2017-07-11 10:04:57 -05:00
events.forEach(event => {
2017-10-24 12:41:09 -05:00
const pod = event.Video.VideoChannel.Author.Pod
2017-02-26 11:57:33 -06:00
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
}