1 const db = require('../../initializers/database')
2 import { BaseRequestScheduler } from './base-request-scheduler'
3 import { logger } from '../../helpers'
5 REQUESTS_VIDEO_QADU_LIMIT_PODS,
6 REQUESTS_VIDEO_QADU_LIMIT_PER_POD,
7 REQUEST_VIDEO_QADU_ENDPOINT,
8 REQUEST_VIDEO_QADU_TYPES
9 } from '../../initializers'
11 class RequestVideoQaduScheduler extends BaseRequestScheduler {
15 // We limit the size of the requests
16 this.limitPods = REQUESTS_VIDEO_QADU_LIMIT_PODS
17 this.limitPerPod = REQUESTS_VIDEO_QADU_LIMIT_PER_POD
19 this.description = 'video QADU requests'
23 return db.RequestVideoQadu
26 getRequestToPodModel () {
27 return db.RequestVideoQadu
30 buildRequestObjects (requests) {
31 const requestsToMakeGrouped = {}
33 Object.keys(requests).forEach(toPodId => {
34 requests[toPodId].forEach(data => {
35 const request = data.request
36 const video = data.video
38 const hashKey = toPodId
40 if (!requestsToMakeGrouped[hashKey]) {
41 requestsToMakeGrouped[hashKey] = {
43 endpoint: REQUEST_VIDEO_QADU_ENDPOINT,
44 ids: [], // request ids, to delete them from the DB in the future
45 datas: [], // requests data
50 // Maybe another attribute was filled for this video
51 let videoData = requestsToMakeGrouped[hashKey].videos[video.id]
52 if (!videoData) videoData = {}
54 switch (request.type) {
55 case REQUEST_VIDEO_QADU_TYPES.LIKES:
56 videoData.likes = video.likes
59 case REQUEST_VIDEO_QADU_TYPES.DISLIKES:
60 videoData.dislikes = video.dislikes
63 case 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)
76 // Maybe there are multiple quick and dirty update for the same video
77 // We use this hashmap to dedupe them
78 requestsToMakeGrouped[hashKey].videos[video.id] = videoData
82 // Now we deduped similar quick and dirty updates, we can build our requests datas
83 Object.keys(requestsToMakeGrouped).forEach(hashKey => {
84 Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoId => {
85 const videoData = requestsToMakeGrouped[hashKey].videos[videoId]
87 requestsToMakeGrouped[hashKey].datas.push({
92 // We don't need it anymore, it was just to build our datas array
93 delete requestsToMakeGrouped[hashKey].videos
96 return requestsToMakeGrouped
99 // { type, videoId, transaction? }
100 createRequest (options, callback) {
101 const type = options.type
102 const videoId = options.videoId
103 const transaction = options.transaction
105 const dbRequestOptions: { transaction?: any } = {}
106 if (transaction) dbRequestOptions.transaction = transaction
108 // Send the update to all our friends
109 db.Pod.listAllIds(options.transaction, function (err, podIds) {
110 if (err) return callback(err)
113 podIds.forEach(podId => {
114 queries.push({ type, videoId, podId })
117 return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions).asCallback(callback)
122 // ---------------------------------------------------------------------------
125 RequestVideoQaduScheduler