]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/standalone/videos/embed.ts
Use different p2p policy for embeds and webapp
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / embed.ts
index dad717108e1b83ddc5185c104f1ec14f9eabec0d..c04f94d202119cfeda13e0e55d5e3b60c7aac592 100644 (file)
@@ -7,6 +7,7 @@ import {
   OAuth2ErrorCode,
   ResultList,
   UserRefreshToken,
+  Video,
   VideoCaption,
   VideoDetails,
   VideoPlaylist,
@@ -16,9 +17,11 @@ import {
 import { P2PMediaLoaderOptions, PeertubePlayerManagerOptions, PlayerMode } from '../../assets/player/peertube-player-manager'
 import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings'
 import { TranslationsManager } from '../../assets/player/translations-manager'
+import { isP2PEnabled } from '../../assets/player/utils'
+import { getBoolOrDefault } from '../../root-helpers/local-storage-utils'
 import { peertubeLocalStorage } from '../../root-helpers/peertube-web-storage'
 import { PluginsManager } from '../../root-helpers/plugins-manager'
-import { Tokens } from '../../root-helpers/users'
+import { UserLocalStorageKeys, UserTokens } from '../../root-helpers/users'
 import { objectToUrlEncoded } from '../../root-helpers/utils'
 import { RegisterClientHelpers } from '../../types/register-client-option.model'
 import { PeerTubeEmbedApi } from './embed-api'
@@ -48,7 +51,7 @@ export class PeerTubeEmbed {
   mode: PlayerMode
   scope = 'peertube'
 
-  userTokens: Tokens
+  userTokens: UserTokens
   headers = new Headers()
   LOCAL_STORAGE_OAUTH_CLIENT_KEYS = {
     CLIENT_ID: 'client_id',
@@ -118,7 +121,7 @@ export class PeerTubeEmbed {
             return res.json()
           }).then((obj: UserRefreshToken & { code?: OAuth2ErrorCode }) => {
             if (!obj || obj.code === OAuth2ErrorCode.INVALID_GRANT) {
-              Tokens.flush()
+              UserTokens.flushLocalStorage(peertubeLocalStorage)
               this.removeTokensFromHeaders()
 
               return resolve()
@@ -126,7 +129,7 @@ export class PeerTubeEmbed {
 
             this.userTokens.accessToken = obj.access_token
             this.userTokens.refreshToken = obj.refresh_token
-            this.userTokens.save()
+            UserTokens.saveToLocalStorage(peertubeLocalStorage, this.userTokens)
 
             this.setHeadersFromTokens()
 
@@ -138,7 +141,7 @@ export class PeerTubeEmbed {
 
         return refreshingTokenPromise
           .catch(() => {
-            Tokens.flush()
+            UserTokens.flushLocalStorage(peertubeLocalStorage)
 
             this.removeTokensFromHeaders()
           }).then(() => fetch(url, {
@@ -258,12 +261,8 @@ export class PeerTubeEmbed {
   }
 
   async init () {
-    try {
-      this.userTokens = Tokens.load()
-      await this.initCore()
-    } catch (e) {
-      console.error(e)
-    }
+    this.userTokens = UserTokens.getUserTokens(peertubeLocalStorage)
+    await this.initCore()
   }
 
   private initializeApi () {
@@ -519,6 +518,8 @@ export class PeerTubeEmbed {
         muted: this.muted,
         loop: this.loop,
 
+        p2pEnabled: this.isP2PEnabled(videoInfo),
+
         captions: videoCaptions.length !== 0,
         subtitle: this.subtitle,
 
@@ -589,7 +590,7 @@ export class PeerTubeEmbed {
 
     this.buildCSS()
 
-    await this.buildDock(videoInfo)
+    this.buildDock(videoInfo)
 
     this.initializeApi()
 
@@ -657,7 +658,7 @@ export class PeerTubeEmbed {
   }
 
   private handleError (err: Error, translations?: { [ id: string ]: string }) {
-    if (err.message.indexOf('from xs param') !== -1) {
+    if (err.message.includes('from xs param')) {
       this.player.dispose()
       this.playerElement = null
       this.displayError('This video is not available because the remote instance is not responding.', translations)
@@ -665,7 +666,7 @@ export class PeerTubeEmbed {
     }
   }
 
-  private async buildDock (videoInfo: VideoDetails) {
+  private buildDock (videoInfo: VideoDetails) {
     if (!this.controls) return
 
     // On webtorrent fallback, player may have been disposed
@@ -673,7 +674,7 @@ export class PeerTubeEmbed {
 
     const title = this.title ? videoInfo.name : undefined
 
-    const description = this.config.tracker.enabled && this.warningTitle
+    const description = this.warningTitle && this.isP2PEnabled(videoInfo)
       ? '<span class="text">' + peertubeTranslate('Watching this video may reveal your IP address to others.') + '</span>'
       : undefined
 
@@ -762,8 +763,8 @@ export class PeerTubeEmbed {
 
     return {
       getBaseStaticRoute: unimplemented,
-
       getBaseRouterRoute: unimplemented,
+      getBasePluginClientPath: unimplemented,
 
       getSettings: unimplemented,
 
@@ -788,7 +789,20 @@ export class PeerTubeEmbed {
       translate: (value: string) => Promise.resolve(peertubeTranslate(value, translations))
     }
   }
+
+  private isP2PEnabled (video: Video) {
+    const userP2PEnabled = getBoolOrDefault(
+      peertubeLocalStorage.getItem(UserLocalStorageKeys.P2P_ENABLED),
+      this.config.defaults.p2p.embed.enabled
+    )
+
+    return isP2PEnabled(video, this.config, userP2PEnabled)
+  }
 }
 
 PeerTubeEmbed.main()
-  .catch(err => console.error('Cannot init embed.', err))
+  .catch(err => {
+    (window as any).displayIncompatibleBrowser()
+
+    console.error('Cannot init embed.', err)
+  })