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