Fix views events with lives
This commit is contained in:
parent
d5fd8227b4
commit
63c4a02ce0
|
@ -244,6 +244,7 @@ class PeerTubePlugin extends Plugin {
|
||||||
|
|
||||||
let lastCurrentTime = startTime
|
let lastCurrentTime = startTime
|
||||||
let lastViewEvent: VideoViewEvent
|
let lastViewEvent: VideoViewEvent
|
||||||
|
let ended = false // player.ended() is too "slow", so handle ended manually
|
||||||
|
|
||||||
if (this.videoViewInterval) clearInterval(this.videoViewInterval)
|
if (this.videoViewInterval) clearInterval(this.videoViewInterval)
|
||||||
if (this.videoViewOnPlayHandler) this.player.off('play', this.videoViewOnPlayHandler)
|
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)
|
if (this.videoViewOnEndedHandler) this.player.off('ended', this.videoViewOnEndedHandler)
|
||||||
|
|
||||||
this.videoViewOnPlayHandler = () => {
|
this.videoViewOnPlayHandler = () => {
|
||||||
|
debugLogger('Notify user is watching on play: ' + startTime)
|
||||||
|
|
||||||
this.notifyUserIsWatching(startTime, lastViewEvent)
|
this.notifyUserIsWatching(startTime, lastViewEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,13 +269,21 @@ class PeerTubePlugin extends Plugin {
|
||||||
// Don't take into account small forwards
|
// Don't take into account small forwards
|
||||||
if (diff > 0 && diff < 3) return
|
if (diff > 0 && diff < 3) return
|
||||||
|
|
||||||
|
debugLogger('Detected seek event for user watching')
|
||||||
|
|
||||||
lastViewEvent = 'seek'
|
lastViewEvent = 'seek'
|
||||||
}
|
}
|
||||||
|
|
||||||
this.videoViewOnEndedHandler = () => {
|
this.videoViewOnEndedHandler = () => {
|
||||||
|
ended = true
|
||||||
|
|
||||||
|
if (this.options.isLive()) return
|
||||||
|
|
||||||
const currentTime = Math.floor(this.player.duration())
|
const currentTime = Math.floor(this.player.duration())
|
||||||
lastCurrentTime = currentTime
|
lastCurrentTime = currentTime
|
||||||
|
|
||||||
|
debugLogger('Notify user is watching on end: ' + currentTime)
|
||||||
|
|
||||||
this.notifyUserIsWatching(currentTime, lastViewEvent)
|
this.notifyUserIsWatching(currentTime, lastViewEvent)
|
||||||
|
|
||||||
lastViewEvent = undefined
|
lastViewEvent = undefined
|
||||||
|
@ -283,11 +294,15 @@ class PeerTubePlugin extends Plugin {
|
||||||
this.player.one('ended', this.videoViewOnEndedHandler)
|
this.player.one('ended', this.videoViewOnEndedHandler)
|
||||||
|
|
||||||
this.videoViewInterval = setInterval(() => {
|
this.videoViewInterval = setInterval(() => {
|
||||||
|
if (ended) return
|
||||||
|
|
||||||
const currentTime = Math.floor(this.player.currentTime())
|
const currentTime = Math.floor(this.player.currentTime())
|
||||||
|
|
||||||
// No need to update
|
// No need to update
|
||||||
if (currentTime === lastCurrentTime) return
|
if (currentTime === lastCurrentTime) return
|
||||||
|
|
||||||
|
debugLogger('Notify user is watching: ' + currentTime)
|
||||||
|
|
||||||
lastCurrentTime = currentTime
|
lastCurrentTime = currentTime
|
||||||
|
|
||||||
this.notifyUserIsWatching(currentTime, lastViewEvent)
|
this.notifyUserIsWatching(currentTime, lastViewEvent)
|
||||||
|
|
|
@ -50,8 +50,8 @@ export class LocalVideoViewerWatchSectionModel extends Model<Partial<AttributesO
|
||||||
|
|
||||||
for (const section of watchSections) {
|
for (const section of watchSections) {
|
||||||
const model = await this.create({
|
const model = await this.create({
|
||||||
watchStart: section.start,
|
watchStart: section.start || 0,
|
||||||
watchEnd: section.end,
|
watchEnd: section.end || 0,
|
||||||
localVideoViewerId
|
localVideoViewerId
|
||||||
}, { transaction })
|
}, { transaction })
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue