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_PODS
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
48 if (!requestsToMakeGrouped[hashKey].videos[video.id]) {
49 requestsToMakeGrouped[hashKey].videos[video.id] = {}
52 const videoData = requestsToMakeGrouped[hashKey].videos[video.id]
54 switch (request.type) {
55 case constants.REQUEST_VIDEO_QADU_TYPES.LIKES:
56 videoData.likes = video.likes
59 case constants.REQUEST_VIDEO_QADU_TYPES.DISLIKES:
60 videoData.likes = video.dislikes
63 case constants.REQUEST_VIDEO_QADU_TYPES.VIEWS:
64 videoData.views = video.views
68 logger.error('Unknown request video QADU type %s.', request.type)
72 // Do not forget the remoteId so the remote pod can identify the video
73 videoData.remoteId = video.id
74 requestsToMakeGrouped[hashKey].ids.push(request.id)
75 requestsToMakeGrouped[hashKey].videos[video.id] = videoData
79 Object.keys(requestsToMakeGrouped).forEach(hashKey => {
80 Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoId => {
81 const videoData = requestsToMakeGrouped[hashKey].videos[videoId]
83 requestsToMakeGrouped[hashKey].datas.push({
88 // We don't need it anymore, it was just to build our datas array
89 delete requestsToMakeGrouped[hashKey].videos
92 return requestsToMakeGrouped
95 // { type, videoId, transaction? }
96 createRequest (options, callback) {
97 const type = options.type
98 const videoId = options.videoId
99 const transaction = options.transaction
101 const dbRequestOptions = {}
102 if (transaction) dbRequestOptions.transaction = transaction
104 // Send the update to all our friends
105 db.Pod.listAllIds(options.transaction, function (err, podIds) {
106 if (err) return callback(err)
109 podIds.forEach(podId => {
110 queries.push({ type, videoId, podId })
113 return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions).asCallback(callback)