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