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