diff options
Diffstat (limited to 'server/lib/job-queue/handlers/video-views.ts')
-rw-r--r-- | server/lib/job-queue/handlers/video-views.ts | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/server/lib/job-queue/handlers/video-views.ts b/server/lib/job-queue/handlers/video-views.ts index cf180a11a..fa1fd13b3 100644 --- a/server/lib/job-queue/handlers/video-views.ts +++ b/server/lib/job-queue/handlers/video-views.ts | |||
@@ -3,8 +3,9 @@ import { logger } from '../../../helpers/logger' | |||
3 | import { VideoModel } from '../../../models/video/video' | 3 | import { VideoModel } from '../../../models/video/video' |
4 | import { VideoViewModel } from '../../../models/video/video-views' | 4 | import { VideoViewModel } from '../../../models/video/video-views' |
5 | import { isTestInstance } from '../../../helpers/core-utils' | 5 | import { isTestInstance } from '../../../helpers/core-utils' |
6 | import { federateVideoIfNeeded } from '../../activitypub' | ||
6 | 7 | ||
7 | async function processVideosViewsViews () { | 8 | async function processVideosViews () { |
8 | const lastHour = new Date() | 9 | const lastHour = new Date() |
9 | 10 | ||
10 | // In test mode, we run this function multiple times per hour, so we don't want the values of the previous hour | 11 | // In test mode, we run this function multiple times per hour, so we don't want the values of the previous hour |
@@ -22,13 +23,9 @@ async function processVideosViewsViews () { | |||
22 | for (const videoId of videoIds) { | 23 | for (const videoId of videoIds) { |
23 | try { | 24 | try { |
24 | const views = await Redis.Instance.getVideoViews(videoId, hour) | 25 | const views = await Redis.Instance.getVideoViews(videoId, hour) |
25 | if (isNaN(views)) { | 26 | if (views) { |
26 | logger.error('Cannot process videos views of video %d in hour %d: views number is NaN.', videoId, hour) | ||
27 | } else { | ||
28 | logger.debug('Adding %d views to video %d in hour %d.', views, videoId, hour) | 27 | logger.debug('Adding %d views to video %d in hour %d.', views, videoId, hour) |
29 | 28 | ||
30 | await VideoModel.incrementViews(videoId, views) | ||
31 | |||
32 | try { | 29 | try { |
33 | await VideoViewModel.create({ | 30 | await VideoViewModel.create({ |
34 | startDate, | 31 | startDate, |
@@ -36,6 +33,16 @@ async function processVideosViewsViews () { | |||
36 | views, | 33 | views, |
37 | videoId | 34 | videoId |
38 | }) | 35 | }) |
36 | |||
37 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) | ||
38 | if (video.isOwned()) { | ||
39 | // If this is a remote video, the origin instance will send us an update | ||
40 | await VideoModel.incrementViews(videoId, views) | ||
41 | |||
42 | // Send video update | ||
43 | video.views += views | ||
44 | await federateVideoIfNeeded(video, false) | ||
45 | } | ||
39 | } catch (err) { | 46 | } catch (err) { |
40 | logger.debug('Cannot create video views for video %d in hour %d. Maybe the video does not exist anymore?', videoId, hour) | 47 | logger.debug('Cannot create video views for video %d in hour %d. Maybe the video does not exist anymore?', videoId, hour) |
41 | } | 48 | } |
@@ -51,5 +58,5 @@ async function processVideosViewsViews () { | |||
51 | // --------------------------------------------------------------------------- | 58 | // --------------------------------------------------------------------------- |
52 | 59 | ||
53 | export { | 60 | export { |
54 | processVideosViewsViews | 61 | processVideosViews |
55 | } | 62 | } |