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 { RequestsVideoQaduGrouped } from '../../models'
13 import { RequestVideoQaduType } from '../../../shared'
15 export type RequestVideoQaduSchedulerOptions = {
16 type: RequestVideoQaduType
18 transaction?: Sequelize.Transaction
21 class RequestVideoQaduScheduler extends AbstractRequestScheduler<RequestsVideoQaduGrouped> {
25 // We limit the size of the requests
26 this.limitPods = REQUESTS_VIDEO_QADU_LIMIT_PODS
27 this.limitPerPod = REQUESTS_VIDEO_QADU_LIMIT_PER_POD
29 this.description = 'video QADU requests'
33 return db.RequestVideoQadu
36 getRequestToPodModel () {
37 return db.RequestVideoQadu
40 buildRequestObjects (requests: RequestsVideoQaduGrouped) {
41 const requestsToMakeGrouped = {}
43 Object.keys(requests).forEach(toPodId => {
44 requests[toPodId].forEach(data => {
45 const request = data.request
46 const video = data.video
48 const hashKey = toPodId
50 if (!requestsToMakeGrouped[hashKey]) {
51 requestsToMakeGrouped[hashKey] = {
53 endpoint: REQUEST_VIDEO_QADU_ENDPOINT,
54 ids: [], // request ids, to delete them from the DB in the future
55 datas: [], // requests data
60 // Maybe another attribute was filled for this video
61 let videoData = requestsToMakeGrouped[hashKey].videos[video.id]
62 if (!videoData) videoData = {}
64 switch (request.type) {
65 case REQUEST_VIDEO_QADU_TYPES.LIKES:
66 videoData.likes = video.likes
69 case REQUEST_VIDEO_QADU_TYPES.DISLIKES:
70 videoData.dislikes = video.dislikes
73 case REQUEST_VIDEO_QADU_TYPES.VIEWS:
74 videoData.views = video.views
78 logger.error('Unknown request video QADU type %s.', request.type)
82 // Do not forget the remoteId so the remote pod can identify the video
83 videoData.remoteId = video.id
84 requestsToMakeGrouped[hashKey].ids.push(request.id)
86 // Maybe there are multiple quick and dirty update for the same video
87 // We use this hashmap to dedupe them
88 requestsToMakeGrouped[hashKey].videos[video.id] = videoData
92 // Now we deduped similar quick and dirty updates, we can build our requests datas
93 Object.keys(requestsToMakeGrouped).forEach(hashKey => {
94 Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoId => {
95 const videoData = requestsToMakeGrouped[hashKey].videos[videoId]
97 requestsToMakeGrouped[hashKey].datas.push({
102 // We don't need it anymore, it was just to build our datas array
103 delete requestsToMakeGrouped[hashKey].videos
106 return requestsToMakeGrouped
109 createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions) {
110 const dbRequestOptions: Sequelize.BulkCreateOptions = {}
111 if (transaction) dbRequestOptions.transaction = transaction
113 // Send the update to all our friends
114 return db.Pod.listAllIds(transaction).then(podIds => {
116 podIds.forEach(podId => {
117 queries.push({ type, videoId, podId })
120 return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions)
125 // ---------------------------------------------------------------------------
128 RequestVideoQaduScheduler