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

186 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 {
RequestVideoEventClass,
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'
})
}
2017-06-10 15:15:25 -05:00
countTotalRequests = function (callback: RequestVideoEventMethods.CountTotalRequestsCallback) {
2017-02-26 11:57:33 -06:00
const query = {}
2017-05-22 13:58:25 -05:00
return RequestVideoEvent.count(query).asCallback(callback)
2017-02-26 11:57:33 -06:00
}
2017-06-10 15:15:25 -05:00
listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoEventMethods.ListWithLimitAndRandomCallback) {
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"'
Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins, function (err, podIds) {
if (err) return callback(err)
// We don't have friends that have requests
if (podIds.length === 0) return callback(null, [])
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
}
}
}
]
}
]
}
]
}
2017-05-22 13:58:25 -05:00
RequestVideoEvent.findAll(query).asCallback(function (err, requests) {
2017-02-26 11:57:33 -06:00
if (err) return callback(err)
const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
return callback(err, requestsGrouped)
})
})
}
2017-06-10 15:15:25 -05:00
removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoEventMethods.RemoveByRequestIdsAndPodCallback) {
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
}
}
]
}
]
}
2017-05-22 13:58:25 -05:00
RequestVideoEvent.destroy(query).asCallback(callback)
2017-02-26 11:57:33 -06:00
}
2017-06-10 15:15:25 -05:00
removeAll = function (callback: RequestVideoEventMethods.RemoveAllCallback) {
2017-02-26 11:57:33 -06:00
// Delete all requests
2017-05-22 13:58:25 -05:00
RequestVideoEvent.truncate({ cascade: true }).asCallback(callback)
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
}