]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/request/request-video-event-scheduler.ts
Better typescript typing for a better world
[github/Chocobozzz/PeerTube.git] / server / lib / request / request-video-event-scheduler.ts
index bde50b1d33dfc3ae9c619758f249c89d54257f5c..8a008c51bbae9d3c15bb0bfc5152285c28c6c4e5 100644 (file)
@@ -1,12 +1,23 @@
+import * as Sequelize from 'sequelize'
+
 import { database as db } from '../../initializers/database'
-import { BaseRequestScheduler } from './base-request-scheduler'
+import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler'
 import {
   REQUESTS_VIDEO_EVENT_LIMIT_PODS,
   REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
   REQUEST_VIDEO_EVENT_ENDPOINT
 } from '../../initializers'
+import { RequestsVideoEventGrouped } from '../../models'
+import { RequestVideoEventType, RemoteVideoEventRequest, RemoteVideoEventType } from '../../../shared'
+
+export type RequestVideoEventSchedulerOptions = {
+  type: RequestVideoEventType
+  videoId: string
+  count?: number
+  transaction?: Sequelize.Transaction
+}
 
-class RequestVideoEventScheduler extends BaseRequestScheduler {
+class RequestVideoEventScheduler extends AbstractRequestScheduler<RequestsVideoEventGrouped> {
   constructor () {
     super()
 
@@ -25,8 +36,8 @@ class RequestVideoEventScheduler extends BaseRequestScheduler {
     return db.RequestVideoEvent
   }
 
-  buildRequestObjects (eventsToProcess) {
-    const requestsToMakeGrouped = {}
+  buildRequestsObjects (eventRequests: RequestsVideoEventGrouped) {
+    const requestsToMakeGrouped: RequestsObjects<RemoteVideoEventRequest> = {}
 
     /* Example:
         {
@@ -36,12 +47,20 @@ class RequestVideoEventScheduler extends BaseRequestScheduler {
           }
         }
     */
-    const eventsPerVideoPerPod = {}
+    const eventsPerVideoPerPod: {
+      [ podId: string ]: {
+        [ videoRemoteId: string ]: {
+          views?: number
+          likes?: number
+          dislikes?: number
+        }
+      }
+    } = {}
 
     // We group video events per video and per pod
     // We add the counts of the same event types
-    Object.keys(eventsToProcess).forEach(toPodId => {
-      eventsToProcess[toPodId].forEach(eventToProcess => {
+    Object.keys(eventRequests).forEach(toPodId => {
+      eventRequests[toPodId].forEach(eventToProcess => {
         if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
 
         if (!requestsToMakeGrouped[toPodId]) {
@@ -76,8 +95,8 @@ class RequestVideoEventScheduler extends BaseRequestScheduler {
           requestsToMakeGrouped[toPodId].datas.push({
             data: {
               remoteId,
-              eventType,
-              count: eventsForVideo[eventType]
+              eventType: eventType as RemoteVideoEventType,
+              count: +eventsForVideo[eventType]
             }
           })
         })
@@ -87,16 +106,10 @@ class RequestVideoEventScheduler extends BaseRequestScheduler {
     return requestsToMakeGrouped
   }
 
-  // { type, videoId, count?, transaction? }
-  createRequest (options, callback) {
-    const type = options.type
-    const videoId = options.videoId
-    const transaction = options.transaction
-    let count = options.count
-
+  createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions) {
     if (count === undefined) count = 1
 
-    const dbRequestOptions: { transaction?: any } = {}
+    const dbRequestOptions: Sequelize.CreateOptions = {}
     if (transaction) dbRequestOptions.transaction = transaction
 
     const createQuery = {
@@ -105,7 +118,7 @@ class RequestVideoEventScheduler extends BaseRequestScheduler {
       videoId
     }
 
-    return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback)
+    return db.RequestVideoEvent.create(createQuery, dbRequestOptions)
   }
 }