2 Request Video for Quick And Dirty Updates like:
7 We can't put it in the same system than basic requests for efficiency.
8 Moreover we don't want to slow down the basic requests with a lot of views/likes/dislikes requests.
9 So we put it an independant request scheduler.
12 import { values } from 'lodash'
13 import * as Sequelize from 'sequelize'
15 import { database as db } from '../../initializers/database'
16 import { REQUEST_VIDEO_QADU_TYPES } from '../../initializers'
17 import { addMethodsToModel } from '../utils'
19 RequestVideoQaduInstance,
20 RequestVideoQaduAttributes,
22 RequestVideoQaduMethods
23 } from './request-video-qadu-interface'
25 let RequestVideoQadu: Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes>
26 let countTotalRequests: RequestVideoQaduMethods.CountTotalRequests
27 let listWithLimitAndRandom: RequestVideoQaduMethods.ListWithLimitAndRandom
28 let removeByRequestIdsAndPod: RequestVideoQaduMethods.RemoveByRequestIdsAndPod
29 let removeAll: RequestVideoQaduMethods.RemoveAll
31 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
32 RequestVideoQadu = sequelize.define<RequestVideoQaduInstance, RequestVideoQaduAttributes>('RequestVideoQadu',
35 type: DataTypes.ENUM(values(REQUEST_VIDEO_QADU_TYPES)),
52 const classMethods = [
55 listWithLimitAndRandom,
58 removeByRequestIdsAndPod
60 addMethodsToModel(RequestVideoQadu, classMethods)
62 return RequestVideoQadu
65 // ------------------------------ STATICS ------------------------------
67 function associate (models) {
68 RequestVideoQadu.belongsTo(models.Pod, {
76 RequestVideoQadu.belongsTo(models.Video, {
85 countTotalRequests = function () {
87 return RequestVideoQadu.count(query)
90 listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
94 return Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', tableJoin).then(podIds => {
95 // We don't have friends that have requests
96 if (podIds.length === 0) return []
101 model: RequestVideoQadu['sequelize'].models.Pod,
109 model: RequestVideoQadu['sequelize'].models.Video
114 return RequestVideoQadu.findAll(query).then(requests => {
115 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
116 return requestsGrouped
121 removeByRequestIdsAndPod = function (ids: number[], podId: number) {
131 return RequestVideoQadu.destroy(query)
134 removeAll = function () {
135 // Delete all requests
136 return RequestVideoQadu.truncate({ cascade: true })
139 // ---------------------------------------------------------------------------
141 function groupAndTruncateRequests (requests: RequestVideoQaduInstance[], limitRequestsPerPod: number) {
142 const requestsGrouped = {}
144 requests.forEach(request => {
145 const pod = request.Pod
147 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
149 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
150 requestsGrouped[pod.id].push({
152 video: request.Video,
158 return requestsGrouped