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'
14 import { REQUEST_VIDEO_QADU_TYPES } from '../initializers'
16 // ---------------------------------------------------------------------------
18 module.exports = function (sequelize, DataTypes) {
19 const RequestVideoQadu = sequelize.define('RequestVideoQadu',
22 type: DataTypes.ENUM(values(REQUEST_VIDEO_QADU_TYPES)),
39 listWithLimitAndRandom,
43 removeByRequestIdsAndPod
48 return RequestVideoQadu
51 // ------------------------------ STATICS ------------------------------
53 function associate (models) {
54 this.belongsTo(models.Pod, {
62 this.belongsTo(models.Video, {
71 function countTotalRequests (callback) {
73 return this.count(query).asCallback(callback)
76 function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
78 const Pod = this.sequelize.models.Pod
80 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', function (err, podIds) {
81 if (err) return callback(err)
83 // We don't have friends that have requests
84 if (podIds.length === 0) return callback(null, [])
89 model: self.sequelize.models.Pod,
97 model: self.sequelize.models.Video
102 self.findAll(query).asCallback(function (err, requests) {
103 if (err) return callback(err)
105 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
106 return callback(err, requestsGrouped)
111 function removeByRequestIdsAndPod (ids, podId, callback) {
121 this.destroy(query).asCallback(callback)
124 function removeAll (callback) {
125 // Delete all requests
126 this.truncate({ cascade: true }).asCallback(callback)
129 // ---------------------------------------------------------------------------
131 function groupAndTruncateRequests (requests, limitRequestsPerPod) {
132 const requestsGrouped = {}
134 requests.forEach(function (request) {
135 const pod = request.Pod
137 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
139 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
140 requestsGrouped[pod.id].push({
142 video: request.Video,
148 return requestsGrouped