]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/plugins/plugin.service.ts
Translate plugin settings
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / plugin.service.ts
index 16108e17a866664926bc53c94fc535dc2df2584f..bb9125fe1ff2abfc107deaa511c99aff2c49e02f 100644 (file)
@@ -1,4 +1,4 @@
-import { Observable, of } from 'rxjs'
+import { firstValueFrom, Observable, of } from 'rxjs'
 import { catchError, map, shareReplay } from 'rxjs/operators'
 import { HttpClient } from '@angular/common/http'
 import { Inject, Injectable, LOCALE_ID, NgZone } from '@angular/core'
@@ -20,7 +20,8 @@ import {
   PluginType,
   PublicServerSetting,
   RegisterClientFormFieldOptions,
-  RegisterClientSettingsScript,
+  RegisterClientRouteOptions,
+  RegisterClientSettingsScriptOptions,
   RegisterClientVideoFieldOptions,
   ServerConfigPlugin
 } from '@shared/models'
@@ -29,6 +30,7 @@ import { RegisterClientHelpers } from '../../../types/register-client-option.mod
 
 type FormFields = {
   video: {
+    pluginInfo: PluginInfo
     commonOptions: RegisterClientFormFieldOptions
     videoFormOptions: RegisterClientVideoFieldOptions
   }[]
@@ -43,12 +45,11 @@ export class PluginService implements ClientHook {
 
   customModal: CustomModalComponent
 
-  private helpers: { [ npmName: string ]: RegisterClientHelpers } = {}
-
   private formFields: FormFields = {
     video: []
   }
-  private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {}
+  private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScriptOptions } = {}
+  private clientRoutes: { [ route: string ]: RegisterClientRouteOptions } = {}
 
   private pluginsManager: PluginsManager
 
@@ -67,7 +68,8 @@ export class PluginService implements ClientHook {
     this.pluginsManager = new PluginsManager({
       peertubeHelpersFactory: this.buildPeerTubeHelpers.bind(this),
       onFormFields: this.onFormFields.bind(this),
-      onSettingsScripts: this.onSettingsScripts.bind(this)
+      onSettingsScripts: this.onSettingsScripts.bind(this),
+      onClientRoute: this.onClientRoute.bind(this)
     })
   }
 
@@ -123,32 +125,70 @@ export class PluginService implements ClientHook {
     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
+  getRegisteredClientRoute (route: string) {
+    return this.clientRoutes[route]
+  }
+
+  getAllRegisteredClientRoutes () {
+    return Object.keys(this.clientRoutes)
+  }
+
+  async translateSetting (npmName: string, setting: RegisterClientFormFieldOptions) {
+    for (const key of [ 'label', 'html', 'descriptionHTML' ]) {
+      if (setting[key]) setting[key] = await this.translateBy(npmName, setting[key])
+    }
+
+    if (Array.isArray(setting.options)) {
+      const newOptions = []
+
+      for (const o of setting.options) {
+        newOptions.push({
+          value: o.value,
+          label: await this.translateBy(npmName, o.label)
+        })
+      }
+
+      setting.options = newOptions
     }
+  }
+
+  translateBy (npmName: string, toTranslate: string) {
+    const obs = this.translationsObservable
+        .pipe(
+          map(allTranslations => allTranslations[npmName]),
+          map(translations => peertubeTranslate(toTranslate, translations))
+        )
 
-    return helpers.translate(toTranslate)
+    return firstValueFrom(obs)
   }
 
-  private onFormFields (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) {
+  private onFormFields (
+    pluginInfo: PluginInfo,
+    commonOptions: RegisterClientFormFieldOptions,
+    videoFormOptions: RegisterClientVideoFieldOptions
+  ) {
     this.formFields.video.push({
+      pluginInfo,
       commonOptions,
       videoFormOptions
     })
   }
 
-  private onSettingsScripts (pluginInfo: PluginInfo, options: RegisterClientSettingsScript) {
-    const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
+  private onSettingsScripts (pluginInfo: PluginInfo, options: RegisterClientSettingsScriptOptions) {
+    this.settingsScripts[pluginInfo.plugin.npmName] = options
+  }
 
-    this.settingsScripts[npmName] = options
+  private onClientRoute (options: RegisterClientRouteOptions) {
+    const route = options.route.startsWith('/')
+      ? options.route
+      : `/${options.route}`
+
+    this.clientRoutes[route] = options
   }
 
   private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers {
     const { plugin } = pluginInfo
-    const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
+    const npmName = pluginInfo.plugin.npmName
 
     return {
       getBaseStaticRoute: () => {
@@ -161,21 +201,27 @@ export class PluginService implements ClientHook {
         return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/router`
       },
 
+      getBasePluginClientPath: () => {
+        return '/p'
+      },
+
       getSettings: () => {
         const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings'
 
-        return this.authHttp.get<PublicServerSetting>(path)
+        const obs = this.authHttp.get<PublicServerSetting>(path)
                    .pipe(
                      map(p => p.publicSettings),
                      catchError(res => this.restExtractor.handleError(res))
                    )
-                   .toPromise()
+
+        return firstValueFrom(obs)
       },
 
       getServerConfig: () => {
-        return this.server.getConfig()
+        const obs = this.server.getConfig()
           .pipe(catchError(res => this.restExtractor.handleError(res)))
-          .toPromise()
+
+        return firstValueFrom(obs)
       },
 
       isLoggedIn: () => {
@@ -186,7 +232,7 @@ export class PluginService implements ClientHook {
         if (!this.authService.isLoggedIn()) return undefined
 
         const value = this.authService.getRequestHeaderValue()
-        return { 'Authorization': value }
+        return { Authorization: value }
       },
 
       notifier: {
@@ -196,10 +242,10 @@ export class PluginService implements ClientHook {
       },
 
       showModal: (input: {
-        title: string,
-        content: string,
-        close?: boolean,
-        cancel?: { value: string, action?: () => void },
+        title: string
+        content: string
+        close?: boolean
+        cancel?: { value: string, action?: () => void }
         confirm?: { value: string, action?: () => void }
       }) => {
         this.zone.run(() => this.customModal.show(input))
@@ -216,10 +262,7 @@ export class PluginService implements ClientHook {
       },
 
       translate: (value: string) => {
-        return this.translationsObservable
-            .pipe(map(allTranslations => allTranslations[npmName]))
-            .pipe(map(translations => peertubeTranslate(value, translations)))
-            .toPromise()
+        return this.translateBy(npmName, value)
       }
     }
   }