From 63c4a02ce0fca3d5efa07e0444d1bed4fec69eaa Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Dec 2023 11:07:55 +0100 Subject: [PATCH] Fix views events with lives --- .../player/shared/peertube/peertube-plugin.ts | 15 +++++++++++++++ .../view/local-video-viewer-watch-section.ts | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/client/src/assets/player/shared/peertube/peertube-plugin.ts b/client/src/assets/player/shared/peertube/peertube-plugin.ts index 1a7ca4d4d..3587a3e92 100644 --- a/client/src/assets/player/shared/peertube/peertube-plugin.ts +++ b/client/src/assets/player/shared/peertube/peertube-plugin.ts @@ -244,6 +244,7 @@ class PeerTubePlugin extends Plugin { let lastCurrentTime = startTime let lastViewEvent: VideoViewEvent + let ended = false // player.ended() is too "slow", so handle ended manually if (this.videoViewInterval) clearInterval(this.videoViewInterval) if (this.videoViewOnPlayHandler) this.player.off('play', this.videoViewOnPlayHandler) @@ -251,6 +252,8 @@ class PeerTubePlugin extends Plugin { if (this.videoViewOnEndedHandler) this.player.off('ended', this.videoViewOnEndedHandler) this.videoViewOnPlayHandler = () => { + debugLogger('Notify user is watching on play: ' + startTime) + this.notifyUserIsWatching(startTime, lastViewEvent) } @@ -266,13 +269,21 @@ class PeerTubePlugin extends Plugin { // Don't take into account small forwards if (diff > 0 && diff < 3) return + debugLogger('Detected seek event for user watching') + lastViewEvent = 'seek' } this.videoViewOnEndedHandler = () => { + ended = true + + if (this.options.isLive()) return + const currentTime = Math.floor(this.player.duration()) lastCurrentTime = currentTime + debugLogger('Notify user is watching on end: ' + currentTime) + this.notifyUserIsWatching(currentTime, lastViewEvent) lastViewEvent = undefined @@ -283,11 +294,15 @@ class PeerTubePlugin extends Plugin { this.player.one('ended', this.videoViewOnEndedHandler) this.videoViewInterval = setInterval(() => { + if (ended) return + const currentTime = Math.floor(this.player.currentTime()) // No need to update if (currentTime === lastCurrentTime) return + debugLogger('Notify user is watching: ' + currentTime) + lastCurrentTime = currentTime this.notifyUserIsWatching(currentTime, lastViewEvent) diff --git a/server/core/models/view/local-video-viewer-watch-section.ts b/server/core/models/view/local-video-viewer-watch-section.ts index b04dcf4bd..eeb7fafd3 100644 --- a/server/core/models/view/local-video-viewer-watch-section.ts +++ b/server/core/models/view/local-video-viewer-watch-section.ts @@ -50,8 +50,8 @@ export class LocalVideoViewerWatchSectionModel extends Model