Better view handling with looped videos

This commit is contained in:
Chocobozzz 2024-07-02 09:41:33 +02:00
parent 9ee467b9cd
commit 01b3396c80
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 36 additions and 7 deletions

View File

@ -298,12 +298,9 @@ class PeerTubePlugin extends Plugin {
let lastCurrentTime = startTime
let lastViewEvent: VideoViewEvent
let ended = false // player.ended() is too "slow", so handle ended manually
let ended = false // player.ended() is too "slow", so store ended state manually
if (this.videoViewInterval) clearInterval(this.videoViewInterval)
if (this.videoViewOnPlayHandler) this.player.off('play', this.videoViewOnPlayHandler)
if (this.videoViewOnSeekedHandler) this.player.off('seeked', this.videoViewOnSeekedHandler)
if (this.videoViewOnEndedHandler) this.player.off('ended', this.videoViewOnEndedHandler)
this.disableUserViewing()
this.videoViewOnPlayHandler = () => {
debugLogger('Notify user is watching on play: ' + startTime)
@ -318,7 +315,14 @@ class PeerTubePlugin extends Plugin {
return
}
const diff = Math.floor(this.player.currentTime()) - lastCurrentTime
const currentTime = Math.floor(this.player.currentTime())
if (currentTime === 0 && this.player.loop()) {
debugLogger('Disabling viewing notification after first video loop.')
this.disableUserViewing()
return
}
const diff = currentTime - lastCurrentTime
// Don't take into account small forwards
if (diff > 0 && diff < 3) return
@ -366,6 +370,28 @@ class PeerTubePlugin extends Plugin {
}, this.options.videoViewIntervalMs)
}
private disableUserViewing () {
if (this.videoViewInterval) {
clearInterval(this.videoViewInterval)
this.videoViewInterval = undefined
}
if (this.videoViewOnPlayHandler) {
this.player.off('play', this.videoViewOnPlayHandler)
this.videoViewOnPlayHandler = undefined
}
if (this.videoViewOnSeekedHandler) {
this.player.off('seeked', this.videoViewOnSeekedHandler)
this.videoViewOnSeekedHandler = undefined
}
if (this.videoViewOnEndedHandler) {
this.player.off('ended', this.videoViewOnEndedHandler)
this.videoViewOnEndedHandler = undefined
}
}
private notifyUserIsWatching (currentTime: number, viewEvent: VideoViewEvent) {
// Server won't save history, so save the video position in local storage
if (!this.authorizationHeader()) {

View File

@ -65,7 +65,10 @@ export class VideoViewerStats {
let stats: LocalViewerStats = await this.getLocalVideoViewer({ sessionId, videoId: video.id })
if (stats && stats.watchSections.length >= MAX_LOCAL_VIEWER_WATCH_SECTIONS) {
logger.warn('Too much watch section to store for a viewer, skipping this one', { currentTime, viewEvent, ...lTags(video.uuid) })
logger.warn(
'Too much watch section to store for a viewer, skipping this one',
{ sessionId, currentTime, viewEvent, ...lTags(video.uuid) }
)
return
}