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