]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/request/request-video-qadu-scheduler.ts
Upgrade common server dependencies
[github/Chocobozzz/PeerTube.git] / server / lib / request / request-video-qadu-scheduler.ts
index d818227238a8b638980a0dd847bde7c81af458a4..afb9d5c23ba9cabb5e13e26bc1c4a425d27d1c0a 100644 (file)
@@ -1,5 +1,7 @@
-const db = require('../../initializers/database')
-import { BaseRequestScheduler } from './base-request-scheduler'
+import * as Sequelize from 'sequelize'
+
+import { database as db } from '../../initializers/database'
+import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler'
 import { logger } from '../../helpers'
 import {
   REQUESTS_VIDEO_QADU_LIMIT_PODS,
@@ -7,8 +9,35 @@ import {
   REQUEST_VIDEO_QADU_ENDPOINT,
   REQUEST_VIDEO_QADU_TYPES
 } from '../../initializers'
+import { RequestsVideoQaduGrouped, PodInstance } from '../../models'
+import { RemoteQaduVideoRequest, RequestVideoQaduType } from '../../../shared'
+
+// We create a custom interface because we need "videos" attribute for our computations
+interface RequestsObjectsCustom<U> extends RequestsObjects<U> {
+  [ id: string ]: {
+    toPod: PodInstance
+    endpoint: string
+    ids: number[] // ids
+    datas: U[]
+
+    videos: {
+      [ uuid: string ]: {
+        uuid: string
+        likes?: number
+        dislikes?: number
+        views?: number
+      }
+    }
+  }
+}
 
-class RequestVideoQaduScheduler extends BaseRequestScheduler {
+export type RequestVideoQaduSchedulerOptions = {
+  type: RequestVideoQaduType
+  videoId: number
+  transaction?: Sequelize.Transaction
+}
+
+class RequestVideoQaduScheduler extends AbstractRequestScheduler<RequestsVideoQaduGrouped> {
   constructor () {
     super()
 
@@ -27,8 +56,8 @@ class RequestVideoQaduScheduler extends BaseRequestScheduler {
     return db.RequestVideoQadu
   }
 
-  buildRequestObjects (requests) {
-    const requestsToMakeGrouped = {}
+  buildRequestsObjects (requests: RequestsVideoQaduGrouped) {
+    const requestsToMakeGrouped: RequestsObjectsCustom<RemoteQaduVideoRequest> = {}
 
     Object.keys(requests).forEach(toPodId => {
       requests[toPodId].forEach(data => {
@@ -49,7 +78,7 @@ class RequestVideoQaduScheduler extends BaseRequestScheduler {
 
         // Maybe another attribute was filled for this video
         let videoData = requestsToMakeGrouped[hashKey].videos[video.id]
-        if (!videoData) videoData = {}
+        if (!videoData) videoData = { uuid: null }
 
         switch (request.type) {
           case REQUEST_VIDEO_QADU_TYPES.LIKES:
@@ -69,8 +98,8 @@ class RequestVideoQaduScheduler extends BaseRequestScheduler {
             return
         }
 
-        // Do not forget the remoteId so the remote pod can identify the video
-        videoData.remoteId = video.id
+        // Do not forget the uuid so the remote pod can identify the video
+        videoData.uuid = video.uuid
         requestsToMakeGrouped[hashKey].ids.push(request.id)
 
         // Maybe there are multiple quick and dirty update for the same video
@@ -81,8 +110,8 @@ class RequestVideoQaduScheduler extends BaseRequestScheduler {
 
     // Now we deduped similar quick and dirty updates, we can build our requests datas
     Object.keys(requestsToMakeGrouped).forEach(hashKey => {
-      Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoId => {
-        const videoData = requestsToMakeGrouped[hashKey].videos[videoId]
+      Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoUUID => {
+        const videoData = requestsToMakeGrouped[hashKey].videos[videoUUID]
 
         requestsToMakeGrouped[hashKey].datas.push({
           data: videoData
@@ -96,25 +125,18 @@ class RequestVideoQaduScheduler extends BaseRequestScheduler {
     return requestsToMakeGrouped
   }
 
-  // { type, videoId, transaction? }
-  createRequest (options, callback) {
-    const type = options.type
-    const videoId = options.videoId
-    const transaction = options.transaction
-
-    const dbRequestOptions: { transaction?: any } = {}
+  createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions) {
+    const dbRequestOptions: Sequelize.BulkCreateOptions = {}
     if (transaction) dbRequestOptions.transaction = transaction
 
     // Send the update to all our friends
-    db.Pod.listAllIds(options.transaction, function (err, podIds) {
-      if (err) return callback(err)
-
+    return db.Pod.listAllIds(transaction).then(podIds => {
       const queries = []
       podIds.forEach(podId => {
         queries.push({ type, videoId, podId })
       })
 
-      return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions).asCallback(callback)
+      return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions)
     })
   }
 }