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 RequestVideoEventClass,
14 RequestVideoEventInstance,
15 RequestVideoEventAttributes,
17 RequestVideoEventMethods,
18 RequestsVideoEventGrouped
19 } from './request-video-event-interface'
21 let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes>
22 let countTotalRequests: RequestVideoEventMethods.CountTotalRequests
23 let listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom
24 let removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod
25 let removeAll: RequestVideoEventMethods.RemoveAll
27 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
28 RequestVideoEvent = sequelize.define<RequestVideoEventInstance, RequestVideoEventAttributes>('RequestVideoEvent',
31 type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)),
35 type: DataTypes.INTEGER,
38 countValid: function (value) {
39 const res = isVideoEventCountValid(value)
40 if (res === false) throw new Error('Video event count is not valid.')
55 const classMethods = [
58 listWithLimitAndRandom,
61 removeByRequestIdsAndPod
63 addMethodsToModel(RequestVideoEvent, classMethods)
65 return RequestVideoEvent
68 // ------------------------------ STATICS ------------------------------
70 function associate (models) {
71 RequestVideoEvent.belongsTo(models.Video, {
80 countTotalRequests = function (callback: RequestVideoEventMethods.CountTotalRequestsCallback) {
82 return RequestVideoEvent.count(query).asCallback(callback)
85 listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoEventMethods.ListWithLimitAndRandomCallback) {
88 // We make a join between videos and authors to find the podId of our video event requests
89 const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' +
90 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"'
92 Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins, function (err, podIds) {
93 if (err) return callback(err)
95 // We don't have friends that have requests
96 if (podIds.length === 0) return callback(null, [])
104 model: RequestVideoEvent['sequelize'].models.Video,
107 model: RequestVideoEvent['sequelize'].models.Author,
110 model: RequestVideoEvent['sequelize'].models.Pod,
124 RequestVideoEvent.findAll(query).asCallback(function (err, requests) {
125 if (err) return callback(err)
127 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
128 return callback(err, requestsGrouped)
133 removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoEventMethods.RemoveByRequestIdsAndPodCallback) {
142 model: RequestVideoEvent['sequelize'].models.Video,
145 model: RequestVideoEvent['sequelize'].models.Author,
155 RequestVideoEvent.destroy(query).asCallback(callback)
158 removeAll = function (callback: RequestVideoEventMethods.RemoveAllCallback) {
159 // Delete all requests
160 RequestVideoEvent.truncate({ cascade: true }).asCallback(callback)
163 // ---------------------------------------------------------------------------
165 function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) {
166 const eventsGrouped: RequestsVideoEventGrouped = {}
168 events.forEach(function (event) {
169 const pod = event.Video.Author.Pod
171 if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = []
173 if (eventsGrouped[pod.id].length < limitRequestsPerPod) {
174 eventsGrouped[pod.id].push({