Server: implement video views
[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
10   constructor () {
11     super()
12
13     // We limit the size of the requests
14     this.limitPods = constants.REQUESTS_VIDEO_QADU_LIMIT_PODS
15     this.limitPerPod = constants.REQUESTS_VIDEO_QADU_LIMIT_PER_POD
16
17     this.description = 'video QADU requests'
18   }
19
20   getRequestModel () {
21     return db.RequestVideoQadu
22   }
23
24   getRequestToPodModel () {
25     return db.RequestVideoQadu
26   }
27
28   buildRequestObjects (requests) {
29     const requestsToMakeGrouped = {}
30
31     Object.keys(requests).forEach(toPodId => {
32       requests[toPodId].forEach(data => {
33         const request = data.request
34         const video = data.video
35         const pod = data.pod
36         const hashKey = toPodId
37
38         if (!requestsToMakeGrouped[hashKey]) {
39           requestsToMakeGrouped[hashKey] = {
40             toPod: pod,
41             endpoint: constants.REQUEST_ENDPOINTS.QADU,
42             ids: [], // request ids, to delete them from the DB in the future
43             datas: [], // requests data
44             videos: {}
45           }
46         }
47
48         if (!requestsToMakeGrouped[hashKey].videos[video.id]) {
49           requestsToMakeGrouped[hashKey].videos[video.id] = {}
50         }
51
52         const videoData = requestsToMakeGrouped[hashKey].videos[video.id]
53
54         switch (request.type) {
55           case constants.REQUEST_VIDEO_QADU_TYPES.LIKES:
56             videoData.likes = video.likes
57             break
58
59           case constants.REQUEST_VIDEO_QADU_TYPES.DISLIKES:
60             videoData.likes = video.dislikes
61             break
62
63           case constants.REQUEST_VIDEO_QADU_TYPES.VIEWS:
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)
75         requestsToMakeGrouped[hashKey].videos[video.id] = videoData
76       })
77     })
78
79     Object.keys(requestsToMakeGrouped).forEach(hashKey => {
80       Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoId => {
81         const videoData = requestsToMakeGrouped[hashKey].videos[videoId]
82
83         requestsToMakeGrouped[hashKey].datas.push({
84           data: videoData
85         })
86       })
87
88       // We don't need it anymore, it was just to build our datas array
89       delete requestsToMakeGrouped[hashKey].videos
90     })
91
92     return requestsToMakeGrouped
93   }
94
95   // { type, videoId, transaction? }
96   createRequest (options, callback) {
97     const type = options.type
98     const videoId = options.videoId
99     const transaction = options.transaction
100
101     const dbRequestOptions = {}
102     if (transaction) dbRequestOptions.transaction = transaction
103
104     // Send the update to all our friends
105     db.Pod.listAllIds(options.transaction, function (err, podIds) {
106       if (err) return callback(err)
107
108       const queries = []
109       podIds.forEach(podId => {
110         queries.push({ type, videoId, podId })
111       })
112
113       return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions).asCallback(callback)
114     })
115   }
116 }