aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request-video-qadu.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/request-video-qadu.ts')
-rw-r--r--server/models/request-video-qadu.ts149
1 files changed, 149 insertions, 0 deletions
diff --git a/server/models/request-video-qadu.ts b/server/models/request-video-qadu.ts
new file mode 100644
index 000000000..2b1ed07c9
--- /dev/null
+++ b/server/models/request-video-qadu.ts
@@ -0,0 +1,149 @@
1/*
2 Request Video for Quick And Dirty Updates like:
3 - views
4 - likes
5 - dislikes
6
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.
10*/
11
12import { values } from 'lodash'
13
14import { REQUEST_VIDEO_QADU_TYPES } from '../initializers'
15
16// ---------------------------------------------------------------------------
17
18module.exports = function (sequelize, DataTypes) {
19 const RequestVideoQadu = sequelize.define('RequestVideoQadu',
20 {
21 type: {
22 type: DataTypes.ENUM(values(REQUEST_VIDEO_QADU_TYPES)),
23 allowNull: false
24 }
25 },
26 {
27 timestamps: false,
28 indexes: [
29 {
30 fields: [ 'podId' ]
31 },
32 {
33 fields: [ 'videoId' ]
34 }
35 ],
36 classMethods: {
37 associate,
38
39 listWithLimitAndRandom,
40
41 countTotalRequests,
42 removeAll,
43 removeByRequestIdsAndPod
44 }
45 }
46 )
47
48 return RequestVideoQadu
49}
50
51// ------------------------------ STATICS ------------------------------
52
53function associate (models) {
54 this.belongsTo(models.Pod, {
55 foreignKey: {
56 name: 'podId',
57 allowNull: false
58 },
59 onDelete: 'CASCADE'
60 })
61
62 this.belongsTo(models.Video, {
63 foreignKey: {
64 name: 'videoId',
65 allowNull: false
66 },
67 onDelete: 'CASCADE'
68 })
69}
70
71function countTotalRequests (callback) {
72 const query = {}
73 return this.count(query).asCallback(callback)
74}
75
76function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
77 const self = this
78 const Pod = this.sequelize.models.Pod
79
80 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', function (err, podIds) {
81 if (err) return callback(err)
82
83 // We don't have friends that have requests
84 if (podIds.length === 0) return callback(null, [])
85
86 const query = {
87 include: [
88 {
89 model: self.sequelize.models.Pod,
90 where: {
91 id: {
92 $in: podIds
93 }
94 }
95 },
96 {
97 model: self.sequelize.models.Video
98 }
99 ]
100 }
101
102 self.findAll(query).asCallback(function (err, requests) {
103 if (err) return callback(err)
104
105 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
106 return callback(err, requestsGrouped)
107 })
108 })
109}
110
111function removeByRequestIdsAndPod (ids, podId, callback) {
112 const query = {
113 where: {
114 id: {
115 $in: ids
116 },
117 podId
118 }
119 }
120
121 this.destroy(query).asCallback(callback)
122}
123
124function removeAll (callback) {
125 // Delete all requests
126 this.truncate({ cascade: true }).asCallback(callback)
127}
128
129// ---------------------------------------------------------------------------
130
131function groupAndTruncateRequests (requests, limitRequestsPerPod) {
132 const requestsGrouped = {}
133
134 requests.forEach(function (request) {
135 const pod = request.Pod
136
137 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
138
139 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
140 requestsGrouped[pod.id].push({
141 request: request,
142 video: request.Video,
143 pod
144 })
145 }
146 })
147
148 return requestsGrouped
149}