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

182 lines
4.8 KiB
TypeScript
Raw Normal View History

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