2 Request Video events (likes, dislikes, views...)
5 import { values } from 'lodash'
6 import * as Sequelize from 'sequelize'
8 import { database as db } from '../../initializers/database'
9 import { REQUEST_VIDEO_EVENT_TYPES } from '../../initializers'
10 import { isVideoEventCountValid } from '../../helpers'
11 import { addMethodsToModel } from '../utils'
13 RequestVideoEventInstance,
14 RequestVideoEventAttributes,
16 RequestVideoEventMethods,
17 RequestsVideoEventGrouped
18 } from './request-video-event-interface'
20 let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes>
21 let countTotalRequests: RequestVideoEventMethods.CountTotalRequests
22 let listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom
23 let removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod
24 let removeAll: RequestVideoEventMethods.RemoveAll
26 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
27 RequestVideoEvent = sequelize.define<RequestVideoEventInstance, RequestVideoEventAttributes>('RequestVideoEvent',
30 type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)),
34 type: DataTypes.INTEGER,
37 countValid: function (value) {
38 const res = isVideoEventCountValid(value)
39 if (res === false) throw new Error('Video event count is not valid.')
54 const classMethods = [
57 listWithLimitAndRandom,
60 removeByRequestIdsAndPod
62 addMethodsToModel(RequestVideoEvent, classMethods)
64 return RequestVideoEvent
67 // ------------------------------ STATICS ------------------------------
69 function associate (models) {
70 RequestVideoEvent.belongsTo(models.Video, {
79 countTotalRequests = function () {
81 return RequestVideoEvent.count(query)
84 listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
87 // We make a join between videos and authors to find the podId of our video event requests
88 const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' +
89 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"'
91 return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => {
92 // We don't have friends that have requests
93 if (podIds.length === 0) return []
101 model: RequestVideoEvent['sequelize'].models.Video,
104 model: RequestVideoEvent['sequelize'].models.Author,
107 model: RequestVideoEvent['sequelize'].models.Pod,
121 return RequestVideoEvent.findAll(query).then(requests => {
122 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
123 return requestsGrouped
128 removeByRequestIdsAndPod = function (ids: number[], podId: number) {
137 model: RequestVideoEvent['sequelize'].models.Video,
140 model: RequestVideoEvent['sequelize'].models.Author,
150 return RequestVideoEvent.destroy(query)
153 removeAll = function () {
154 // Delete all requests
155 return RequestVideoEvent.truncate({ cascade: true })
158 // ---------------------------------------------------------------------------
160 function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) {
161 const eventsGrouped: RequestsVideoEventGrouped = {}
163 events.forEach(event => {
164 const pod = event.Video.Author.Pod
166 if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = []
168 if (eventsGrouped[pod.id].length < limitRequestsPerPod) {
169 eventsGrouped[pod.id].push({