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
{
13 // We limit the size of the requests
14 this.limitPods
= constants
.REQUESTS_VIDEO_QADU_LIMIT_PODS
15 this.limitPerPod
= constants
.REQUESTS_VIDEO_QADU_LIMIT_PER_POD
17 this.description
= 'video QADU requests'
21 return db
.RequestVideoQadu
24 getRequestToPodModel () {
25 return db
.RequestVideoQadu
28 buildRequestObjects (requests
) {
29 const requestsToMakeGrouped
= {}
31 Object
.keys(requests
).forEach(toPodId
=> {
32 requests
[toPodId
].forEach(data
=> {
33 const request
= data
.request
34 const video
= data
.video
36 const hashKey
= toPodId
38 if (!requestsToMakeGrouped
[hashKey
]) {
39 requestsToMakeGrouped
[hashKey
] = {
41 endpoint: constants
.REQUEST_ENDPOINTS
.QADU
,
42 ids: [], // request ids, to delete them from the DB in the future
43 datas: [], // requests data
49 switch (request
.type
) {
50 case constants
.REQUEST_VIDEO_QADU_TYPES
.LIKES:
51 videoData
.likes
= video
.likes
54 case constants
.REQUEST_VIDEO_QADU_TYPES
.DISLIKES:
55 videoData
.likes
= video
.dislikes
58 case constants
.REQUEST_VIDEO_QADU_TYPES
.VIEWS:
59 videoData
.views
= video
.views
63 logger
.error('Unknown request video QADU type %s.', request
.type
)
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
)
71 // Maybe there are multiple quick and dirty update for the same video
72 // We use this hashmap to dedupe them
73 requestsToMakeGrouped
[hashKey
].videos
[video
.id
] = videoData
77 // Now we deduped similar quick and dirty updates, we can build our requests datas
78 Object
.keys(requestsToMakeGrouped
).forEach(hashKey
=> {
79 Object
.keys(requestsToMakeGrouped
[hashKey
].videos
).forEach(videoId
=> {
80 const videoData
= requestsToMakeGrouped
[hashKey
].videos
[videoId
]
82 requestsToMakeGrouped
[hashKey
].datas
.push({
87 // We don't need it anymore, it was just to build our datas array
88 delete requestsToMakeGrouped
[hashKey
].videos
91 return requestsToMakeGrouped
94 // { type, videoId, transaction? }
95 createRequest (options
, callback
) {
96 const type
= options
.type
97 const videoId
= options
.videoId
98 const transaction
= options
.transaction
100 const dbRequestOptions
= {}
101 if (transaction
) dbRequestOptions
.transaction
= transaction
103 // Send the update to all our friends
104 db
.Pod
.listAllIds(options
.transaction
, function (err
, podIds
) {
105 if (err
) return callback(err
)
108 podIds
.forEach(podId
=> {
109 queries
.push({ type
, videoId
, podId
})
112 return db
.RequestVideoQadu
.bulkCreate(queries
, dbRequestOptions
).asCallback(callback
)