]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Move adding a video view videojs peertube plugin
authorChocobozzz <me@florianbigard.com>
Wed, 14 Feb 2018 16:16:32 +0000 (17:16 +0100)
committerChocobozzz <me@florianbigard.com>
Wed, 14 Feb 2018 16:16:32 +0000 (17:16 +0100)
client/src/app/shared/video/infinite-scroller.directive.ts
client/src/app/shared/video/video.service.ts
client/src/app/videos/+video-watch/video-watch.component.ts
client/src/assets/player/peertube-videojs-plugin.ts
client/src/sass/video-js-custom.scss
client/src/standalone/videos/embed.ts
server/tools/import-youtube.ts

index 43e014cbddaa9bce7ba48b249f1f5deff196d94b..52d8b2b3718b8b144baf64b77df4184e97117082 100644 (file)
@@ -1,5 +1,8 @@
 import { Directive, EventEmitter, Input, OnInit, Output } from '@angular/core'
+import 'rxjs/add/operator/debounceTime'
 import 'rxjs/add/operator/distinct'
+import 'rxjs/add/operator/filter'
+import 'rxjs/add/operator/map'
 import 'rxjs/add/operator/startWith'
 import { fromEvent } from 'rxjs/observable/fromEvent'
 
index d4f5e258f155ff2ae3b697bf21d617e6e9ecbc6c..01d32176b0d61b77314aa6b3896837d9d23580f4 100644 (file)
@@ -29,6 +29,10 @@ export class VideoService {
     private restService: RestService
   ) {}
 
+  getVideoViewUrl (uuid: string) {
+    return VideoService.BASE_VIDEO_URL + uuid + '/views'
+  }
+
   getVideo (uuid: string): Observable<VideoDetails> {
     return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
                         .map(videoHash => new VideoDetails(videoHash))
@@ -36,7 +40,7 @@ export class VideoService {
   }
 
   viewVideo (uuid: string): Observable<VideoDetails> {
-    return this.authHttp.post(VideoService.BASE_VIDEO_URL + uuid + '/views', {})
+    return this.authHttp.post(this.getVideoViewUrl(uuid), {})
       .map(this.restExtractor.extractDataBool)
       .catch(this.restExtractor.handleError)
   }
index 8330aba15d5269cb3bb2e599ba47564e47ed56a8..b7779ae9a081aad4c2c93af437df768f4f85ac7a 100644 (file)
@@ -329,7 +329,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
               peertube: {
                 videoFiles: this.video.files,
                 playerElement: this.playerElement,
-                peerTubeLink: false
+                peerTubeLink: false,
+                videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid)
               },
               hotkeys: {
                 enableVolumeScroll: false
@@ -349,7 +350,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
             })
           })
         } else {
-          this.player.peertube().setVideoFiles(this.video.files)
+          this.player.peertube().setVideoFiles(this.video.files, this.videoService.getVideoViewUrl(this.video.uuid))
         }
 
         this.setVideoDescriptionHTML()
@@ -357,8 +358,6 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
 
         this.setOpenGraphTags()
         this.checkUserRating()
-
-        this.prepareViewAdd()
       }
     )
   }
@@ -431,19 +430,6 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
     this.metaService.setTag('url', window.location.href)
   }
 
-  private prepareViewAdd () {
-    // After 30 seconds (or 3/4 of the video), increment add a view
-    let viewTimeoutSeconds = 30
-    if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
-
-    setTimeout(() => {
-      this.videoService
-        .viewVideo(this.video.uuid)
-        .subscribe()
-
-    }, viewTimeoutSeconds * 1000)
-  }
-
   private isAutoplay () {
     // True by default
     if (!this.user) return true
index 125ef64a4663d8db8f620dc1c031c2490a13c9d2..fecd4edec7b03f3a4f50f19487fe8bfdd1989e2b 100644 (file)
@@ -1,5 +1,6 @@
 // Big thanks to: https://github.com/kmoskwiak/videojs-resolution-switcher
 
+import { VideoService } from '@app/shared/video/video.service'
 import * as videojs from 'video.js'
 import * as WebTorrent from 'webtorrent'
 import { VideoFile } from '../../../../shared/models/videos/video.model'
@@ -23,6 +24,7 @@ type PeertubePluginOptions = {
   videoFiles: VideoFile[]
   playerElement: HTMLVideoElement
   peerTubeLink: boolean
+  videoViewUrl: string
 }
 
 // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
@@ -218,6 +220,8 @@ class PeerTubePlugin extends Plugin {
   private videoFiles: VideoFile[]
   private torrent: WebTorrent.Torrent
   private autoplay = false
+  private videoViewUrl: string
+  private videoViewInterval
 
   constructor (player: videojs.Player, options: PeertubePluginOptions) {
     super(player, options)
@@ -227,6 +231,7 @@ class PeerTubePlugin extends Plugin {
     this.player.options_.autoplay = false
 
     this.videoFiles = options.videoFiles
+    this.videoViewUrl = options.videoViewUrl
 
     // Hack to "simulate" src link in video.js >= 6
     // Without this, we can't play the video after pausing it
@@ -240,6 +245,7 @@ class PeerTubePlugin extends Plugin {
     this.player.ready(() => {
       this.initializePlayer(options)
       this.runTorrentInfoScheduler()
+      this.prepareRunViewAdd()
     })
   }
 
@@ -334,9 +340,12 @@ class PeerTubePlugin extends Plugin {
     }
   }
 
-  setVideoFiles (files: VideoFile[]) {
+  setVideoFiles (files: VideoFile[], videoViewUrl: string) {
+    this.videoViewUrl = videoViewUrl
     this.videoFiles = files
 
+    // Re run view add for the new video
+    this.prepareRunViewAdd()
     this.updateVideoFile(undefined, () => this.player.play())
   }
 
@@ -377,6 +386,48 @@ class PeerTubePlugin extends Plugin {
     }, 1000)
   }
 
+  private prepareRunViewAdd () {
+    if (this.player.readyState() < 1) {
+      return this.player.one('loadedmetadata', () => this.runViewAdd())
+    }
+
+    this.runViewAdd()
+  }
+
+  private runViewAdd () {
+    this.clearVideoViewInterval()
+
+    // After 30 seconds (or 3/4 of the video), add a view to the video
+    let minSecondsToView = 30
+
+    const duration = this.player.duration()
+    if (duration < minSecondsToView) minSecondsToView = (duration * 3) / 4
+
+    let secondsViewed = 0
+    this.videoViewInterval = setInterval(() => {
+      if (this.player && !this.player.paused()) {
+        secondsViewed += 1
+
+        if (secondsViewed > minSecondsToView) {
+          this.clearVideoViewInterval()
+
+          this.addViewToVideo().catch(err => console.error(err))
+        }
+      }
+    }, 1000)
+  }
+
+  private clearVideoViewInterval () {
+    if (this.videoViewInterval !== undefined) {
+      clearInterval(this.videoViewInterval)
+      this.videoViewInterval = undefined
+    }
+  }
+
+  private addViewToVideo () {
+    return fetch(this.videoViewUrl, { method: 'POST' })
+  }
+
   private handleError (err: Error | string) {
     return this.player.trigger('customError', { err })
   }
index 8ff96357354536bd00cc9bbf19f5c8ce31778051..209fb16831c209ac2759a112b816d7362af731a3 100644 (file)
@@ -24,19 +24,22 @@ $control-bar-height: 34px;
 
   .vjs-big-play-button {
     outline: 0;
-    font-size: 8em;
+    font-size: 7em;
 
-    $big-play-width: 3em;
-    $big-play-height: 1.5em;
+    $big-play-width: 1.5em;
+    $big-play-height: 1em;
 
     border: 0;
     border-radius: 0.3em;
 
     left: 50%;
     top: 50%;
+    width: $big-play-width;
+    height: $big-play-height;
+    line-height: $big-play-height;
     margin-left: -($big-play-width / 2);
     margin-top: -($big-play-height / 2);
-    background-color: transparent !important;
+    transition: opacity 0.5s;
 
     &::-moz-focus-inner {
       border: 0;
@@ -47,8 +50,12 @@ $control-bar-height: 34px;
       transition: text-shadow 0.3s;
     }
 
-    &:hover .vjs-icon-placeholder::before {
-      text-shadow: 0 0 2px rgba(255, 255, 255, 0.8);
+    &:hover {
+      opacity: 0.9;
+
+      .vjs-icon-placeholder::before {
+        text-shadow: 0 0 1px rgba(255, 255, 255, 0.8);
+      }
     }
   }
 
index 3193ae6ef560df29a8e4674d41fd194039a45ca4..9076f1dc950e238a5f4c9feaea86e8ac78542c9e 100644 (file)
@@ -6,8 +6,12 @@ import '../../assets/player/peertube-videojs-plugin'
 import 'videojs-dock/dist/videojs-dock.es.js'
 import { VideoDetails } from '../../../../shared'
 
+function getVideoUrl (id: string) {
+  return window.location.origin + '/api/v1/videos/' + videoId
+}
+
 async function loadVideoInfo (videoId: string): Promise<VideoDetails> {
-  const response = await fetch(window.location.origin + '/api/v1/videos/' + videoId)
+  const response = await fetch(getVideoUrl(videoId))
   return response.json()
 }
 
@@ -27,7 +31,8 @@ loadVideoInfo(videoId)
         peertube: {
           videoFiles: videoInfo.files,
           playerElement: videoElement,
-          peerTubeLink: true
+          peerTubeLink: true,
+          videoViewUrl: getVideoUrl(videoId) + '/views'
         },
         hotkeys: {
           enableVolumeScroll: false
index 325e9f7ba883e85861748d263b7a9b7997e342f3..12e996e5b42c3855caa3c10b60f64e73034c4282 100644 (file)
@@ -120,7 +120,8 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string) {
     tags: videoInfo.tags.slice(0, 5),
     privacy: VideoPrivacy.PUBLIC,
     fixture: videoPath,
-    thumbnailfile
+    thumbnailfile,
+    previewfile: thumbnailfile
   }
 
   console.log('\nUploading on PeerTube video "%s".', videoAttributes.name)