]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/request/request-video-qadu-scheduler.ts
Update systemd service template
[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 { RequestVideoQaduType } from '../../../shared'
13
14 export type RequestVideoQaduSchedulerOptions = {
15 type: RequestVideoQaduType
16 videoId: string
17 transaction?: Sequelize.Transaction
18 }
19
20 class RequestVideoQaduScheduler extends AbstractRequestScheduler {
21 constructor () {
22 super()
23
24 // We limit the size of the requests
25 this.limitPods = REQUESTS_VIDEO_QADU_LIMIT_PODS
26 this.limitPerPod = REQUESTS_VIDEO_QADU_LIMIT_PER_POD
27
28 this.description = 'video QADU requests'
29 }
30
31 getRequestModel () {
32 return db.RequestVideoQadu
33 }
34
35 getRequestToPodModel () {
36 return db.RequestVideoQadu
37 }
38
39 buildRequestObjects (requests: { [ toPodId: number ]: any }[]) {
40 const requestsToMakeGrouped = {}
41
42 Object.keys(requests).forEach(toPodId => {
43 requests[toPodId].forEach(data => {
44 const request = data.request
45 const video = data.video
46 const pod = data.pod
47 const hashKey = toPodId
48
49 if (!requestsToMakeGrouped[hashKey]) {
50 requestsToMakeGrouped[hashKey] = {
51 toPod: pod,
52 endpoint: REQUEST_VIDEO_QADU_ENDPOINT,
53 ids: [], // request ids, to delete them from the DB in the future
54 datas: [], // requests data
55 videos: {}
56 }
57 }
58
59 // Maybe another attribute was filled for this video
60 let videoData = requestsToMakeGrouped[hashKey].videos[video.id]
61 if (!videoData) videoData = {}
62
63 switch (request.type) {
64 case REQUEST_VIDEO_QADU_TYPES.LIKES:
65 videoData.likes = video.likes
66 break
67
68 case REQUEST_VIDEO_QADU_TYPES.DISLIKES:
69 videoData.dislikes = video.dislikes
70 break
71
72 case REQUEST_VIDEO_QADU_TYPES.VIEWS:
73 videoData.views = video.views
74 break
75
76 default:
77 logger.error('Unknown request video QADU type %s.', request.type)
78 return
79 }
80
81 // Do not forget the remoteId so the remote pod can identify the video
82 videoData.remoteId = video.id
83 requestsToMakeGrouped[hashKey].ids.push(request.id)
84
85 // Maybe there are multiple quick and dirty update for the same video
86 // We use this hashmap to dedupe them
87 requestsToMakeGrouped[hashKey].videos[video.id] = videoData
88 })
89 })
90
91 // Now we deduped similar quick and dirty updates, we can build our requests datas
92 Object.keys(requestsToMakeGrouped).forEach(hashKey => {
93 Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoId => {
94 const videoData = requestsToMakeGrouped[hashKey].videos[videoId]
95
96 requestsToMakeGrouped[hashKey].datas.push({
97 data: videoData
98 })
99 })
100
101 // We don't need it anymore, it was just to build our datas array
102 delete requestsToMakeGrouped[hashKey].videos
103 })
104
105 return requestsToMakeGrouped
106 }
107
108 createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions, callback: (err: Error) => void) {
109 const dbRequestOptions: Sequelize.BulkCreateOptions = {}
110 if (transaction) dbRequestOptions.transaction = transaction
111
112 // Send the update to all our friends
113 db.Pod.listAllIds(transaction, function (err, podIds) {
114 if (err) return callback(err)
115
116 const queries = []
117 podIds.forEach(podId => {
118 queries.push({ type, videoId, podId })
119 })
120
121 return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions).asCallback(callback)
122 })
123 }
124 }
125
126 // ---------------------------------------------------------------------------
127
128 export {
129 RequestVideoQaduScheduler
130 }