4 Request Video for Quick And Dirty Updates like:
9 We can't put it in the same system than basic requests for efficiency.
10 Moreover we don't want to slow down the basic requests with a lot of views/likes/dislikes requests.
11 So we put it an independant request scheduler.
14 const values = require('lodash/values')
16 const constants = require('../initializers/constants')
18 // ---------------------------------------------------------------------------
20 module.exports = function (sequelize, DataTypes) {
21 const RequestVideoQadu = sequelize.define('RequestVideoQadu',
24 type: DataTypes.ENUM(values(constants.REQUEST_VIDEO_QADU_TYPES)),
41 listWithLimitAndRandom,
45 removeByRequestIdsAndPod
50 return RequestVideoQadu
53 // ------------------------------ STATICS ------------------------------
55 function associate (models) {
56 this.belongsTo(models.Pod, {
64 this.belongsTo(models.Video, {
73 function countTotalRequests (callback) {
75 return this.count(query).asCallback(callback)
78 function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
80 const Pod = this.sequelize.models.Pod
82 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', function (err, podIds) {
83 if (err) return callback(err)
85 // We don't have friends that have requests
86 if (podIds.length === 0) return callback(null, [])
91 model: self.sequelize.models.Pod,
99 model: self.sequelize.models.Video
104 self.findAll(query).asCallback(function (err, requests) {
105 if (err) return callback(err)
107 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
108 return callback(err, requestsGrouped)
113 function removeByRequestIdsAndPod (ids, podId, callback) {
123 this.destroy(query).asCallback(callback)
126 function removeAll (callback) {
127 // Delete all requests
128 this.truncate({ cascade: true }).asCallback(callback)
131 // ---------------------------------------------------------------------------
133 function groupAndTruncateRequests (requests, limitRequestsPerPod) {
134 const requestsGrouped = {}
136 requests.forEach(function (request) {
137 const pod = request.Pod
139 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
141 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
142 requestsGrouped[pod.id].push({
144 video: request.Video,
150 return requestsGrouped