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')
8 module
.exports
= class RequestVideoQaduScheduler
extends BaseRequestScheduler
{
12 // We limit the size of the requests
13 this.limitPods
= constants
.REQUESTS_VIDEO_QADU_LIMIT_PODS
14 this.limitPerPod
= constants
.REQUESTS_VIDEO_QADU_LIMIT_PER_POD
16 this.description
= 'video QADU requests'
20 return db
.RequestVideoQadu
23 getRequestToPodModel () {
24 return db
.RequestVideoQadu
27 buildRequestObjects (requests
) {
28 const requestsToMakeGrouped
= {}
30 Object
.keys(requests
).forEach(toPodId
=> {
31 requests
[toPodId
].forEach(data
=> {
32 const request
= data
.request
33 const video
= data
.video
35 const hashKey
= toPodId
37 if (!requestsToMakeGrouped
[hashKey
]) {
38 requestsToMakeGrouped
[hashKey
] = {
40 endpoint: constants
.REQUEST_ENDPOINTS
.QADU
,
41 ids: [], // request ids, to delete them from the DB in the future
42 datas: [], // requests data
48 switch (request
.type
) {
49 case constants
.REQUEST_VIDEO_QADU_TYPES
.LIKES:
50 videoData
.likes
= video
.likes
53 case constants
.REQUEST_VIDEO_QADU_TYPES
.DISLIKES:
54 videoData
.likes
= video
.dislikes
57 case constants
.REQUEST_VIDEO_QADU_TYPES
.VIEWS:
58 videoData
.views
= video
.views
62 logger
.error('Unknown request video QADU type %s.', request
.type
)
66 // Do not forget the remoteId so the remote pod can identify the video
67 videoData
.remoteId
= video
.id
68 requestsToMakeGrouped
[hashKey
].ids
.push(request
.id
)
70 // Maybe there are multiple quick and dirty update for the same video
71 // We use this hashmap to dedupe them
72 requestsToMakeGrouped
[hashKey
].videos
[video
.id
] = videoData
76 // Now we deduped similar quick and dirty updates, we can build our requests datas
77 Object
.keys(requestsToMakeGrouped
).forEach(hashKey
=> {
78 Object
.keys(requestsToMakeGrouped
[hashKey
].videos
).forEach(videoId
=> {
79 const videoData
= requestsToMakeGrouped
[hashKey
].videos
[videoId
]
81 requestsToMakeGrouped
[hashKey
].datas
.push({
86 // We don't need it anymore, it was just to build our datas array
87 delete requestsToMakeGrouped
[hashKey
].videos
90 return requestsToMakeGrouped
93 // { type, videoId, transaction? }
94 createRequest (options
, callback
) {
95 const type
= options
.type
96 const videoId
= options
.videoId
97 const transaction
= options
.transaction
99 const dbRequestOptions
= {}
100 if (transaction
) dbRequestOptions
.transaction
= transaction
102 // Send the update to all our friends
103 db
.Pod
.listAllIds(options
.transaction
, function (err
, podIds
) {
104 if (err
) return callback(err
)
107 podIds
.forEach(podId
=> {
108 queries
.push({ type
, videoId
, podId
})
111 return db
.RequestVideoQadu
.bulkCreate(queries
, dbRequestOptions
).asCallback(callback
)