1 import * as Sequelize from 'sequelize'
3 import { database as db } from '../../initializers/database'
4 import { AbstractRequestScheduler } from './abstract-request-scheduler'
5 import { logger } from '../../helpers'
7 REQUESTS_VIDEO_QADU_LIMIT_PODS,
8 REQUESTS_VIDEO_QADU_LIMIT_PER_POD,
9 REQUEST_VIDEO_QADU_ENDPOINT,
10 REQUEST_VIDEO_QADU_TYPES
11 } from '../../initializers'
12 import { RequestVideoQaduType } from '../../../shared'
14 export type RequestVideoQaduSchedulerOptions = {
15 type: RequestVideoQaduType
17 transaction?: Sequelize.Transaction
20 class RequestVideoQaduScheduler extends AbstractRequestScheduler {
24 // We limit the size of the requests
25 this.limitPods = REQUESTS_VIDEO_QADU_LIMIT_PODS
26 this.limitPerPod = REQUESTS_VIDEO_QADU_LIMIT_PER_POD
28 this.description = 'video QADU requests'
32 return db.RequestVideoQadu
35 getRequestToPodModel () {
36 return db.RequestVideoQadu
39 buildRequestObjects (requests: { [ toPodId: number ]: any }[]) {
40 const requestsToMakeGrouped = {}
42 Object.keys(requests).forEach(toPodId => {
43 requests[toPodId].forEach(data => {
44 const request = data.request
45 const video = data.video
47 const hashKey = toPodId
49 if (!requestsToMakeGrouped[hashKey]) {
50 requestsToMakeGrouped[hashKey] = {
52 endpoint: REQUEST_VIDEO_QADU_ENDPOINT,
53 ids: [], // request ids, to delete them from the DB in the future
54 datas: [], // requests data
59 // Maybe another attribute was filled for this video
60 let videoData = requestsToMakeGrouped[hashKey].videos[video.id]
61 if (!videoData) videoData = {}
63 switch (request.type) {
64 case REQUEST_VIDEO_QADU_TYPES.LIKES:
65 videoData.likes = video.likes
68 case REQUEST_VIDEO_QADU_TYPES.DISLIKES:
69 videoData.dislikes = video.dislikes
72 case REQUEST_VIDEO_QADU_TYPES.VIEWS:
73 videoData.views = video.views
77 logger.error('Unknown request video QADU type %s.', request.type)
81 // Do not forget the remoteId so the remote pod can identify the video
82 videoData.remoteId = video.id
83 requestsToMakeGrouped[hashKey].ids.push(request.id)
85 // Maybe there are multiple quick and dirty update for the same video
86 // We use this hashmap to dedupe them
87 requestsToMakeGrouped[hashKey].videos[video.id] = videoData
91 // Now we deduped similar quick and dirty updates, we can build our requests datas
92 Object.keys(requestsToMakeGrouped).forEach(hashKey => {
93 Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoId => {
94 const videoData = requestsToMakeGrouped[hashKey].videos[videoId]
96 requestsToMakeGrouped[hashKey].datas.push({
101 // We don't need it anymore, it was just to build our datas array
102 delete requestsToMakeGrouped[hashKey].videos
105 return requestsToMakeGrouped
108 createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions, callback: (err: Error) => void) {
109 const dbRequestOptions: Sequelize.BulkCreateOptions = {}
110 if (transaction) dbRequestOptions.transaction = transaction
112 // Send the update to all our friends
113 db.Pod.listAllIds(transaction, function (err, podIds) {
114 if (err) return callback(err)
117 podIds.forEach(podId => {
118 queries.push({ type, videoId, podId })
121 return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions).asCallback(callback)
126 // ---------------------------------------------------------------------------
129 RequestVideoQaduScheduler