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