]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/plugins/plugin.service.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / plugin.service.ts
index 4e44a18658329aad439aecf4b90500ca6ef642b7..6a1a46e73c875dfa625aefd9a2f153dd8a3677a7 100644 (file)
@@ -2,6 +2,7 @@ import { Observable, of, ReplaySubject } from 'rxjs'
 import { catchError, first, map, shareReplay } from 'rxjs/operators'
 import { HttpClient } from '@angular/common/http'
 import { Inject, Injectable, LOCALE_ID, NgZone } from '@angular/core'
+import { VideoEditType } from '@app/+videos/+video-edit/shared/video-edit.type'
 import { AuthService } from '@app/core/auth'
 import { Notifier } from '@app/core/notification'
 import { MarkdownService } from '@app/core/renderer'
@@ -18,6 +19,7 @@ import {
   PluginTranslation,
   PluginType,
   PublicServerSetting,
+  RegisterClientSettingsScript,
   ServerConfigPlugin
 } from '@shared/models'
 import { environment } from '../../../environments/environment'
@@ -32,6 +34,7 @@ export class PluginService implements ClientHook {
 
   pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = {
     common: new ReplaySubject<boolean>(1),
+    'admin-plugin': new ReplaySubject<boolean>(1),
     search: new ReplaySubject<boolean>(1),
     'video-watch': new ReplaySubject<boolean>(1),
     signup: new ReplaySubject<boolean>(1),
@@ -45,7 +48,10 @@ export class PluginService implements ClientHook {
   customModal: CustomModalComponent
 
   private plugins: ServerConfigPlugin[] = []
+  private helpers: { [ npmName: string ]: RegisterClientHelpers } = {}
+
   private scopes: { [ scopeName: string ]: PluginInfo[] } = {}
+
   private loadedScripts: { [ script: string ]: boolean } = {}
   private loadedScopes: PluginClientScope[] = []
   private loadingScopes: { [id in PluginClientScope]?: boolean } = {}
@@ -54,6 +60,7 @@ export class PluginService implements ClientHook {
   private formFields: FormFields = {
     video: []
   }
+  private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {}
 
   constructor (
     private authService: AuthService,
@@ -192,17 +199,37 @@ export class PluginService implements ClientHook {
       : PluginType.THEME
   }
 
-  getRegisteredVideoFormFields (type: 'import-url' | 'import-torrent' | 'upload' | 'update') {
+  getRegisteredVideoFormFields (type: VideoEditType) {
     return this.formFields.video.filter(f => f.videoFormOptions.type === type)
   }
 
+  getRegisteredSettingsScript (npmName: string) {
+    return this.settingsScripts[npmName]
+  }
+
+  translateBy (npmName: string, toTranslate: string) {
+    const helpers = this.helpers[npmName]
+    if (!helpers) {
+      console.error('Unknown helpers to translate %s from %s.', toTranslate, npmName)
+      return toTranslate
+    }
+
+    return helpers.translate(toTranslate)
+  }
+
   private loadPlugin (pluginInfo: PluginInfo) {
     return this.zone.runOutsideAngular(() => {
+      const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
+
+      const helpers = this.buildPeerTubeHelpers(pluginInfo)
+      this.helpers[npmName] = helpers
+
       return loadPlugin({
         hooks: this.hooks,
         formFields: this.formFields,
+        onSettingsScripts: options => this.settingsScripts[npmName] = options,
         pluginInfo,
-        peertubeHelpersFactory: pluginInfo => this.buildPeerTubeHelpers(pluginInfo)
+        peertubeHelpersFactory: () => helpers
       })
     })
   }
@@ -234,10 +261,23 @@ export class PluginService implements ClientHook {
                    .toPromise()
       },
 
+      getServerConfig: () => {
+        return this.server.getConfig()
+          .pipe(catchError(res => this.restExtractor.handleError(res)))
+          .toPromise()
+      },
+
       isLoggedIn: () => {
         return this.authService.isLoggedIn()
       },
 
+      getAuthHeader: () => {
+        if (!this.authService.isLoggedIn()) return undefined
+
+        const value = this.authService.getRequestHeaderValue()
+        return { 'Authorization': value }
+      },
+
       notifier: {
         info: (text: string, title?: string, timeout?: number) => this.notifier.info(text, title, timeout),
         error: (text: string, title?: string, timeout?: number) => this.notifier.error(text, title, timeout),