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