aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/request/request-video-qadu-scheduler.ts
blob: 49970a2e6a8c8b390a5f41af59d027d962985ba6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import * as Sequelize from 'sequelize'

import { database as db } from '../../initializers/database'
import { AbstractRequestScheduler } from './abstract-request-scheduler'
import { logger } from '../../helpers'
import {
  REQUESTS_VIDEO_QADU_LIMIT_PODS,
  REQUESTS_VIDEO_QADU_LIMIT_PER_POD,
  REQUEST_VIDEO_QADU_ENDPOINT,
  REQUEST_VIDEO_QADU_TYPES
} from '../../initializers'

export type RequestVideoQaduSchedulerOptions = {
  type: string
  videoId: string
  transaction?: Sequelize.Transaction
}

class RequestVideoQaduScheduler extends AbstractRequestScheduler {
  constructor () {
    super()

    // We limit the size of the requests
    this.limitPods = REQUESTS_VIDEO_QADU_LIMIT_PODS
    this.limitPerPod = REQUESTS_VIDEO_QADU_LIMIT_PER_POD

    this.description = 'video QADU requests'
  }

  getRequestModel () {
    return db.RequestVideoQadu
  }

  getRequestToPodModel () {
    return db.RequestVideoQadu
  }

  buildRequestObjects (requests: { [ toPodId: number ]: any }[]) {
    const requestsToMakeGrouped = {}

    Object.keys(requests).forEach(toPodId => {
      requests[toPodId].forEach(data => {
        const request = data.request
        const video = data.video
        const pod = data.pod
        const hashKey = toPodId

        if (!requestsToMakeGrouped[hashKey]) {
          requestsToMakeGrouped[hashKey] = {
            toPod: pod,
            endpoint: REQUEST_VIDEO_QADU_ENDPOINT,
            ids: [], // request ids, to delete them from the DB in the future
            datas: [], // requests data
            videos: {}
          }
        }

        // Maybe another attribute was filled for this video
        let videoData = requestsToMakeGrouped[hashKey].videos[video.id]
        if (!videoData) videoData = {}

        switch (request.type) {
          case REQUEST_VIDEO_QADU_TYPES.LIKES:
            videoData.likes = video.likes
            break

          case REQUEST_VIDEO_QADU_TYPES.DISLIKES:
            videoData.dislikes = video.dislikes
            break

          case REQUEST_VIDEO_QADU_TYPES.VIEWS:
            videoData.views = video.views
            break

          default:
            logger.error('Unknown request video QADU type %s.', request.type)
            return
        }

        // Do not forget the remoteId so the remote pod can identify the video
        videoData.remoteId = video.id
        requestsToMakeGrouped[hashKey].ids.push(request.id)

        // Maybe there are multiple quick and dirty update for the same video
        // We use this hashmap to dedupe them
        requestsToMakeGrouped[hashKey].videos[video.id] = videoData
      })
    })

    // 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]

        requestsToMakeGrouped[hashKey].datas.push({
          data: videoData
        })
      })

      // We don't need it anymore, it was just to build our datas array
      delete requestsToMakeGrouped[hashKey].videos
    })

    return requestsToMakeGrouped
  }

  createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions, callback: (err: Error) => void) {
    const dbRequestOptions: Sequelize.BulkCreateOptions = {}
    if (transaction) dbRequestOptions.transaction = transaction

    // Send the update to all our friends
    db.Pod.listAllIds(transaction, function (err, podIds) {
      if (err) return callback(err)

      const queries = []
      podIds.forEach(podId => {
        queries.push({ type, videoId, podId })
      })

      return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions).asCallback(callback)
    })
  }
}

// ---------------------------------------------------------------------------

export {
  RequestVideoQaduScheduler
}