]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/request-video-qadu.js
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 include: [ this.sequelize
.models
.Pod
]
78 return this.count(query
).asCallback(callback
)
81 function listWithLimitAndRandom (limitPods
, limitRequestsPerPod
, callback
) {
83 const Pod
= this.sequelize
.models
.Pod
85 Pod
.listRandomPodIdsWithRequest(limitPods
, 'RequestVideoQadus', function (err
, podIds
) {
86 if (err
) return callback(err
)
88 // We don't have friends that have requests
89 if (podIds
.length
=== 0) return callback(null, [])
94 model: self
.sequelize
.models
.Pod
,
102 model: self
.sequelize
.models
.Video
107 self
.findAll(query
).asCallback(function (err
, requests
) {
108 if (err
) return callback(err
)
110 const requestsGrouped
= groupAndTruncateRequests(requests
, limitRequestsPerPod
)
111 return callback(err
, requestsGrouped
)
116 function removeByRequestIdsAndPod (ids
, podId
, callback
) {
126 this.destroy(query
).asCallback(callback
)
129 function removeAll (callback
) {
130 // Delete all requests
131 this.truncate({ cascade: true }).asCallback(callback
)
134 // ---------------------------------------------------------------------------
136 function groupAndTruncateRequests (requests
, limitRequestsPerPod
) {
137 const requestsGrouped
= {}
139 requests
.forEach(function (request
) {
140 const pod
= request
.Pod
142 if (!requestsGrouped
[pod
.id
]) requestsGrouped
[pod
.id
] = []
144 if (requestsGrouped
[pod
.id
].length
< limitRequestsPerPod
) {
145 requestsGrouped
[pod
.id
].push({
147 video: request
.Video
,
153 return requestsGrouped