aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/friends.js22
-rw-r--r--server/lib/request-video-event-scheduler.js109
-rw-r--r--server/lib/request-video-qadu-scheduler.js2
3 files changed, 131 insertions, 2 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 424a30801..203f0e52c 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -11,13 +11,16 @@ const db = require('../initializers/database')
11const logger = require('../helpers/logger') 11const logger = require('../helpers/logger')
12const peertubeCrypto = require('../helpers/peertube-crypto') 12const peertubeCrypto = require('../helpers/peertube-crypto')
13const requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
14const utils = require('../helpers/utils')
14const RequestScheduler = require('./request-scheduler') 15const RequestScheduler = require('./request-scheduler')
15const RequestVideoQaduScheduler = require('./request-video-qadu-scheduler') 16const RequestVideoQaduScheduler = require('./request-video-qadu-scheduler')
17const RequestVideoEventScheduler = require('./request-video-event-scheduler')
16 18
17const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] 19const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
18 20
19const requestScheduler = new RequestScheduler() 21const requestScheduler = new RequestScheduler()
20const requestSchedulerVideoQadu = new RequestVideoQaduScheduler() 22const requestSchedulerVideoQadu = new RequestVideoQaduScheduler()
23const requestSchedulerVideoEvent = new RequestVideoEventScheduler()
21 24
22const friends = { 25const friends = {
23 activate, 26 activate,
@@ -25,6 +28,7 @@ const friends = {
25 updateVideoToFriends, 28 updateVideoToFriends,
26 reportAbuseVideoToFriend, 29 reportAbuseVideoToFriend,
27 quickAndDirtyUpdateVideoToFriends, 30 quickAndDirtyUpdateVideoToFriends,
31 addEventToRemoteVideo,
28 hasFriends, 32 hasFriends,
29 makeFriends, 33 makeFriends,
30 quitFriends, 34 quitFriends,
@@ -35,6 +39,7 @@ const friends = {
35function activate () { 39function activate () {
36 requestScheduler.activate() 40 requestScheduler.activate()
37 requestSchedulerVideoQadu.activate() 41 requestSchedulerVideoQadu.activate()
42 requestSchedulerVideoEvent.activate()
38} 43}
39 44
40function addVideoToFriends (videoData, transaction, callback) { 45function addVideoToFriends (videoData, transaction, callback) {
@@ -85,6 +90,15 @@ function quickAndDirtyUpdateVideoToFriends (videoId, type, transaction, callback
85 return createVideoQaduRequest(options, callback) 90 return createVideoQaduRequest(options, callback)
86} 91}
87 92
93function addEventToRemoteVideo (videoId, type, transaction, callback) {
94 const options = {
95 videoId,
96 type,
97 transaction
98 }
99 createVideoEventRequest(options, callback)
100}
101
88function hasFriends (callback) { 102function hasFriends (callback) {
89 db.Pod.countAll(function (err, count) { 103 db.Pod.countAll(function (err, count) {
90 if (err) return callback(err) 104 if (err) return callback(err)
@@ -329,11 +343,17 @@ function createRequest (options, callback) {
329} 343}
330 344
331function createVideoQaduRequest (options, callback) { 345function createVideoQaduRequest (options, callback) {
332 if (!callback) callback = function () {} 346 if (!callback) callback = utils.createEmptyCallback()
333 347
334 requestSchedulerVideoQadu.createRequest(options, callback) 348 requestSchedulerVideoQadu.createRequest(options, callback)
335} 349}
336 350
351function createVideoEventRequest (options, callback) {
352 if (!callback) callback = utils.createEmptyCallback()
353
354 requestSchedulerVideoEvent.createRequest(options, callback)
355}
356
337function isMe (host) { 357function isMe (host) {
338 return host === constants.CONFIG.WEBSERVER.HOST 358 return host === constants.CONFIG.WEBSERVER.HOST
339} 359}
diff --git a/server/lib/request-video-event-scheduler.js b/server/lib/request-video-event-scheduler.js
new file mode 100644
index 000000000..5ea5631b0
--- /dev/null
+++ b/server/lib/request-video-event-scheduler.js
@@ -0,0 +1,109 @@
1'use strict'
2
3const BaseRequestScheduler = require('./base-request-scheduler')
4const constants = require('../initializers/constants')
5const db = require('../initializers/database')
6
7module.exports = class RequestVideoEventScheduler extends BaseRequestScheduler {
8
9 constructor () {
10 super()
11
12 // We limit the size of the requests
13 this.limitPods = constants.REQUESTS_VIDEO_EVENT_LIMIT_PODS
14 this.limitPerPod = constants.REQUESTS_VIDEO_EVENT_LIMIT_PER_POD
15
16 this.description = 'video event requests'
17 }
18
19 getRequestModel () {
20 return db.RequestVideoEvent
21 }
22
23 getRequestToPodModel () {
24 return db.RequestVideoEvent
25 }
26
27 buildRequestObjects (eventsToProcess) {
28 const requestsToMakeGrouped = {}
29
30 /* Example:
31 {
32 pod1: {
33 video1: { views: 4, likes: 5 },
34 video2: { likes: 5 }
35 }
36 }
37 */
38 const eventsPerVideoPerPod = {}
39
40 // We group video events per video and per pod
41 // We add the counts of the same event types
42 Object.keys(eventsToProcess).forEach(toPodId => {
43 eventsToProcess[toPodId].forEach(eventToProcess => {
44 if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
45
46 if (!requestsToMakeGrouped[toPodId]) {
47 requestsToMakeGrouped[toPodId] = {
48 toPod: eventToProcess.pod,
49 endpoint: constants.REQUEST_ENDPOINTS.EVENT,
50 ids: [], // request ids, to delete them from the DB in the future
51 datas: [] // requests data
52 }
53 }
54 requestsToMakeGrouped[toPodId].ids.push(eventToProcess.id)
55
56 const eventsPerVideo = eventsPerVideoPerPod[toPodId]
57 const remoteId = eventToProcess.video.remoteId
58 if (!eventsPerVideo[remoteId]) eventsPerVideo[remoteId] = {}
59
60 const events = eventsPerVideo[remoteId]
61 if (!events[eventToProcess.type]) events[eventToProcess.type] = 0
62
63 events[eventToProcess.type] += eventToProcess.count
64 })
65 })
66
67 // Now we build our requests array per pod
68 Object.keys(eventsPerVideoPerPod).forEach(toPodId => {
69 const eventsForPod = eventsPerVideoPerPod[toPodId]
70
71 Object.keys(eventsForPod).forEach(remoteId => {
72 const eventsForVideo = eventsForPod[remoteId]
73
74 Object.keys(eventsForVideo).forEach(eventType => {
75 requestsToMakeGrouped[toPodId].datas.push({
76 data: {
77 remoteId,
78 eventType,
79 count: eventsForVideo[eventType]
80 }
81 })
82 })
83 })
84 })
85
86 return requestsToMakeGrouped
87 }
88
89 // { type, videoId, count?, transaction? }
90 createRequest (options, callback) {
91 const type = options.type
92 const videoId = options.videoId
93 const transaction = options.transaction
94 let count = options.count
95
96 if (count === undefined) count = 1
97
98 const dbRequestOptions = {}
99 if (transaction) dbRequestOptions.transaction = transaction
100
101 const createQuery = {
102 type,
103 count,
104 videoId
105 }
106
107 return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback)
108 }
109}
diff --git a/server/lib/request-video-qadu-scheduler.js b/server/lib/request-video-qadu-scheduler.js
index 401b2fb44..29e44a6c4 100644
--- a/server/lib/request-video-qadu-scheduler.js
+++ b/server/lib/request-video-qadu-scheduler.js
@@ -12,7 +12,7 @@ module.exports = class RequestVideoQaduScheduler extends BaseRequestScheduler {
12 12
13 // We limit the size of the requests 13 // We limit the size of the requests
14 this.limitPods = constants.REQUESTS_VIDEO_QADU_LIMIT_PODS 14 this.limitPods = constants.REQUESTS_VIDEO_QADU_LIMIT_PODS
15 this.limitPerPod = constants.REQUESTS_VIDEO_QADU_LIMIT_PODS 15 this.limitPerPod = constants.REQUESTS_VIDEO_QADU_LIMIT_PER_POD
16 16
17 this.description = 'video QADU requests' 17 this.description = 'video QADU requests'
18 } 18 }