diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/models/request-video-qadu.ts | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/models/request-video-qadu.ts')
-rw-r--r-- | server/models/request-video-qadu.ts | 149 |
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 | |||
12 | import { values } from 'lodash' | ||
13 | |||
14 | import { REQUEST_VIDEO_QADU_TYPES } from '../initializers' | ||
15 | |||
16 | // --------------------------------------------------------------------------- | ||
17 | |||
18 | module.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 | |||
53 | function 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 | |||
71 | function countTotalRequests (callback) { | ||
72 | const query = {} | ||
73 | return this.count(query).asCallback(callback) | ||
74 | } | ||
75 | |||
76 | function 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 | |||
111 | function 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 | |||
124 | function removeAll (callback) { | ||
125 | // Delete all requests | ||
126 | this.truncate({ cascade: true }).asCallback(callback) | ||
127 | } | ||
128 | |||
129 | // --------------------------------------------------------------------------- | ||
130 | |||
131 | function 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 | } | ||