]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Avoid always resuming the end of the video
authorChocobozzz <me@florianbigard.com>
Sat, 25 Feb 2023 15:18:28 +0000 (16:18 +0100)
committerChocobozzz <me@florianbigard.com>
Sat, 25 Feb 2023 15:18:28 +0000 (16:18 +0100)
client/src/app/+videos/+video-watch/video-watch.component.ts
client/src/assets/player/shared/manager-options/manager-options-builder.ts
client/src/assets/player/shared/peertube/peertube-plugin.ts
client/src/assets/player/types/manager-options.ts
client/src/assets/player/types/peertube-videojs-typings.ts
client/src/standalone/videos/shared/player-manager-options.ts

index 84548de97f8b9983e06a8d5b7f92210765946f3c..19ad97d42a2a4e6dbd32b7e8fcfd6dd78ef3b3fe 100644 (file)
@@ -91,6 +91,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
 
   private hotkeys: Hotkey[] = []
 
+  private static VIEW_VIDEO_INTERVAL_MS = 5000
+
   constructor (
     private elementRef: ElementRef,
     private route: ActivatedRoute,
@@ -613,16 +615,18 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
       const byLocalStorage = getStoredVideoWatchHistory(video.uuid)
 
       if (byUrl) return timeToInt(urlOptions.startTime)
-      if (byHistory) return video.userHistory.currentTime
-      if (byLocalStorage) return byLocalStorage.duration
 
-      return 0
-    }
+      let startTime = 0
+      if (byHistory) startTime = video.userHistory.currentTime
+      if (byLocalStorage) startTime = byLocalStorage.duration
 
-    let startTime = getStartTime()
+      // If we are at the end of the video, reset the timer
+      if (video.duration - startTime <= 1) startTime = 0
+
+      return startTime
+    }
 
-    // If we are at the end of the video, reset the timer
-    if (video.duration - startTime <= 1) startTime = 0
+    const startTime = getStartTime()
 
     const playerCaptions = videoCaptions.map(c => ({
       label: c.language.label,
@@ -679,6 +683,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
         videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE
           ? this.videoService.getVideoViewUrl(video.uuid)
           : null,
+        videoViewIntervalMs: VideoWatchComponent.VIEW_VIDEO_INTERVAL_MS,
         authorizationHeader: () => this.authService.getRequestHeaderValue(),
 
         serverUrl: environment.originServerUrl || window.location.origin,
index c820d637b3609aceb8cf6eaf46f86f35f62f5c61..5d3ee4c4a2cd2477f9f45e619dc82f61cc72c45a 100644 (file)
@@ -35,6 +35,7 @@ export class ManagerOptionsBuilder {
 
         ...pick(commonOptions, [
           'videoViewUrl',
+          'videoViewIntervalMs',
           'authorizationHeader',
           'startTime',
           'videoDuration',
index ec8fbb320b448b90e0e55cb5f4dcba47e8702ae3..b5f177f3050b7afd7a61f84006cc443ab4360fc0 100644 (file)
@@ -27,9 +27,7 @@ class PeerTubePlugin extends Plugin {
   private readonly videoUUID: string
   private readonly startTime: number
 
-  private readonly CONSTANTS = {
-    USER_VIEW_VIDEO_INTERVAL: 5000 // Every 5 seconds, notify the user is watching the video
-  }
+  private readonly videoViewIntervalMs: number
 
   private videoCaptions: VideoJSCaption[]
   private defaultSubtitle: string
@@ -48,6 +46,7 @@ class PeerTubePlugin extends Plugin {
     this.authorizationHeader = options.authorizationHeader
     this.videoUUID = options.videoUUID
     this.startTime = timeToInt(options.startTime)
+    this.videoViewIntervalMs = options.videoViewIntervalMs
 
     this.videoCaptions = options.videoCaptions
     this.initialInactivityTimeout = this.player.options_.inactivityTimeout
@@ -188,7 +187,7 @@ class PeerTubePlugin extends Plugin {
     })
 
     this.player.one('ended', () => {
-      const currentTime = Math.round(this.player.duration())
+      const currentTime = Math.floor(this.player.duration())
       lastCurrentTime = currentTime
 
       this.notifyUserIsWatching(currentTime, lastViewEvent)
@@ -197,7 +196,7 @@ class PeerTubePlugin extends Plugin {
     })
 
     this.videoViewInterval = setInterval(() => {
-      const currentTime = Math.round(this.player.currentTime())
+      const currentTime = Math.floor(this.player.currentTime())
 
       // No need to update
       if (currentTime === lastCurrentTime) return
@@ -213,7 +212,7 @@ class PeerTubePlugin extends Plugin {
       if (!this.authorizationHeader()) {
         saveVideoWatchHistory(this.videoUUID, currentTime)
       }
-    }, this.CONSTANTS.USER_VIEW_VIDEO_INTERVAL)
+    }, this.videoViewIntervalMs)
   }
 
   private notifyUserIsWatching (currentTime: number, viewEvent: VideoViewEvent) {
index 3fbcec29c79621f115ddceb9c66c03d0a5d52dd1..c14fd7e9977c38fd8c378df56989725b6d7ad76c 100644 (file)
@@ -55,6 +55,8 @@ export interface CommonOptions extends CustomizationOptions {
   inactivityTimeout: number
   poster: string
 
+  videoViewIntervalMs: number
+
   instanceName: string
 
   theaterButton: boolean
index 5674f78cbf2aa500c2da4eecc3d63c7a2315523b..eadf56cfa6e02cc81311036cb2b9030ddde294f3 100644 (file)
@@ -108,6 +108,8 @@ type PeerTubePluginOptions = {
   isLive: boolean
 
   videoUUID: string
+
+  videoViewIntervalMs: number
 }
 
 type MetricsPluginOptions = {
index f09c86d148bbf8c9c83ad55ca49e5c91d7dc50bd..43ae22a3bcc67c67d1f0c216e9ab878efdcd777a 100644 (file)
@@ -217,6 +217,7 @@ export class PlayerManagerOptions {
         videoCaptions,
         inactivityTimeout: 2500,
         videoViewUrl: this.videoFetcher.getVideoViewsUrl(video.uuid),
+        videoViewIntervalMs: 5000,
         metricsUrl: window.location.origin + '/api/v1/metrics/playback',
 
         videoShortUUID: video.shortUUID,