diff options
Diffstat (limited to 'server/lib/request/request-video-qadu-scheduler.ts')
-rw-r--r-- | server/lib/request/request-video-qadu-scheduler.ts | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/server/lib/request/request-video-qadu-scheduler.ts b/server/lib/request/request-video-qadu-scheduler.ts new file mode 100644 index 000000000..d81822723 --- /dev/null +++ b/server/lib/request/request-video-qadu-scheduler.ts | |||
@@ -0,0 +1,126 @@ | |||
1 | const db = require('../../initializers/database') | ||
2 | import { BaseRequestScheduler } from './base-request-scheduler' | ||
3 | import { logger } from '../../helpers' | ||
4 | import { | ||
5 | REQUESTS_VIDEO_QADU_LIMIT_PODS, | ||
6 | REQUESTS_VIDEO_QADU_LIMIT_PER_POD, | ||
7 | REQUEST_VIDEO_QADU_ENDPOINT, | ||
8 | REQUEST_VIDEO_QADU_TYPES | ||
9 | } from '../../initializers' | ||
10 | |||
11 | class RequestVideoQaduScheduler extends BaseRequestScheduler { | ||
12 | constructor () { | ||
13 | super() | ||
14 | |||
15 | // We limit the size of the requests | ||
16 | this.limitPods = REQUESTS_VIDEO_QADU_LIMIT_PODS | ||
17 | this.limitPerPod = REQUESTS_VIDEO_QADU_LIMIT_PER_POD | ||
18 | |||
19 | this.description = 'video QADU requests' | ||
20 | } | ||
21 | |||
22 | getRequestModel () { | ||
23 | return db.RequestVideoQadu | ||
24 | } | ||
25 | |||
26 | getRequestToPodModel () { | ||
27 | return db.RequestVideoQadu | ||
28 | } | ||
29 | |||
30 | buildRequestObjects (requests) { | ||
31 | const requestsToMakeGrouped = {} | ||
32 | |||
33 | Object.keys(requests).forEach(toPodId => { | ||
34 | requests[toPodId].forEach(data => { | ||
35 | const request = data.request | ||
36 | const video = data.video | ||
37 | const pod = data.pod | ||
38 | const hashKey = toPodId | ||
39 | |||
40 | if (!requestsToMakeGrouped[hashKey]) { | ||
41 | requestsToMakeGrouped[hashKey] = { | ||
42 | toPod: pod, | ||
43 | endpoint: REQUEST_VIDEO_QADU_ENDPOINT, | ||
44 | ids: [], // request ids, to delete them from the DB in the future | ||
45 | datas: [], // requests data | ||
46 | videos: {} | ||
47 | } | ||
48 | } | ||
49 | |||
50 | // Maybe another attribute was filled for this video | ||
51 | let videoData = requestsToMakeGrouped[hashKey].videos[video.id] | ||
52 | if (!videoData) videoData = {} | ||
53 | |||
54 | switch (request.type) { | ||
55 | case REQUEST_VIDEO_QADU_TYPES.LIKES: | ||
56 | videoData.likes = video.likes | ||
57 | break | ||
58 | |||
59 | case REQUEST_VIDEO_QADU_TYPES.DISLIKES: | ||
60 | videoData.dislikes = video.dislikes | ||
61 | break | ||
62 | |||
63 | case REQUEST_VIDEO_QADU_TYPES.VIEWS: | ||
64 | videoData.views = video.views | ||
65 | break | ||
66 | |||
67 | default: | ||
68 | logger.error('Unknown request video QADU type %s.', request.type) | ||
69 | return | ||
70 | } | ||
71 | |||
72 | // Do not forget the remoteId so the remote pod can identify the video | ||
73 | videoData.remoteId = video.id | ||
74 | requestsToMakeGrouped[hashKey].ids.push(request.id) | ||
75 | |||
76 | // Maybe there are multiple quick and dirty update for the same video | ||
77 | // We use this hashmap to dedupe them | ||
78 | requestsToMakeGrouped[hashKey].videos[video.id] = videoData | ||
79 | }) | ||
80 | }) | ||
81 | |||
82 | // Now we deduped similar quick and dirty updates, we can build our requests datas | ||
83 | Object.keys(requestsToMakeGrouped).forEach(hashKey => { | ||
84 | Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoId => { | ||
85 | const videoData = requestsToMakeGrouped[hashKey].videos[videoId] | ||
86 | |||
87 | requestsToMakeGrouped[hashKey].datas.push({ | ||
88 | data: videoData | ||
89 | }) | ||
90 | }) | ||
91 | |||
92 | // We don't need it anymore, it was just to build our datas array | ||
93 | delete requestsToMakeGrouped[hashKey].videos | ||
94 | }) | ||
95 | |||
96 | return requestsToMakeGrouped | ||
97 | } | ||
98 | |||
99 | // { type, videoId, transaction? } | ||
100 | createRequest (options, callback) { | ||
101 | const type = options.type | ||
102 | const videoId = options.videoId | ||
103 | const transaction = options.transaction | ||
104 | |||
105 | const dbRequestOptions: { transaction?: any } = {} | ||
106 | if (transaction) dbRequestOptions.transaction = transaction | ||
107 | |||
108 | // Send the update to all our friends | ||
109 | db.Pod.listAllIds(options.transaction, function (err, podIds) { | ||
110 | if (err) return callback(err) | ||
111 | |||
112 | const queries = [] | ||
113 | podIds.forEach(podId => { | ||
114 | queries.push({ type, videoId, podId }) | ||
115 | }) | ||
116 | |||
117 | return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions).asCallback(callback) | ||
118 | }) | ||
119 | } | ||
120 | } | ||
121 | |||
122 | // --------------------------------------------------------------------------- | ||
123 | |||
124 | export { | ||
125 | RequestVideoQaduScheduler | ||
126 | } | ||