]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/request/request-video-qadu-scheduler.ts
5ec7de9c206e1291aa2a6bb4bd9f13d0473f3991
[github/Chocobozzz/PeerTube.git] / server / lib / request / request-video-qadu-scheduler.ts
1 import * as Sequelize from 'sequelize'
2
3 import { database as db } from '../../initializers/database'
4 import { AbstractRequestScheduler } from './abstract-request-scheduler'
5 import { logger } from '../../helpers'
6 import {
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'
14
15 export type RequestVideoQaduSchedulerOptions = {
16 type: RequestVideoQaduType
17 videoId: string
18 transaction?: Sequelize.Transaction
19 }
20
21 class RequestVideoQaduScheduler extends AbstractRequestScheduler<RequestsVideoQaduGrouped> {
22 constructor () {
23 super()
24
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
28
29 this.description = 'video QADU requests'
30 }
31
32 getRequestModel () {
33 return db.RequestVideoQadu
34 }
35
36 getRequestToPodModel () {
37 return db.RequestVideoQadu
38 }
39
40 buildRequestObjects (requests: RequestsVideoQaduGrouped) {
41 const requestsToMakeGrouped = {}
42
43 Object.keys(requests).forEach(toPodId => {
44 requests[toPodId].forEach(data => {
45 const request = data.request
46 const video = data.video
47 const pod = data.pod
48 const hashKey = toPodId
49
50 if (!requestsToMakeGrouped[hashKey]) {
51 requestsToMakeGrouped[hashKey] = {
52 toPod: pod,
53 endpoint: REQUEST_VIDEO_QADU_ENDPOINT,
54 ids: [], // request ids, to delete them from the DB in the future
55 datas: [], // requests data
56 videos: {}
57 }
58 }
59
60 // Maybe another attribute was filled for this video
61 let videoData = requestsToMakeGrouped[hashKey].videos[video.id]
62 if (!videoData) videoData = {}
63
64 switch (request.type) {
65 case REQUEST_VIDEO_QADU_TYPES.LIKES:
66 videoData.likes = video.likes
67 break
68
69 case REQUEST_VIDEO_QADU_TYPES.DISLIKES:
70 videoData.dislikes = video.dislikes
71 break
72
73 case REQUEST_VIDEO_QADU_TYPES.VIEWS:
74 videoData.views = video.views
75 break
76
77 default:
78 logger.error('Unknown request video QADU type %s.', request.type)
79 return
80 }
81
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)
85
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
89 })
90 })
91
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]
96
97 requestsToMakeGrouped[hashKey].datas.push({
98 data: videoData
99 })
100 })
101
102 // We don't need it anymore, it was just to build our datas array
103 delete requestsToMakeGrouped[hashKey].videos
104 })
105
106 return requestsToMakeGrouped
107 }
108
109 createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions) {
110 const dbRequestOptions: Sequelize.BulkCreateOptions = {}
111 if (transaction) dbRequestOptions.transaction = transaction
112
113 // Send the update to all our friends
114 return db.Pod.listAllIds(transaction).then(podIds => {
115 const queries = []
116 podIds.forEach(podId => {
117 queries.push({ type, videoId, podId })
118 })
119
120 return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions)
121 })
122 }
123 }
124
125 // ---------------------------------------------------------------------------
126
127 export {
128 RequestVideoQaduScheduler
129 }