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