]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add setting helper to client plugins
authorChocobozzz <me@florianbigard.com>
Thu, 25 Jul 2019 17:02:54 +0000 (19:02 +0200)
committerChocobozzz <me@florianbigard.com>
Fri, 26 Jul 2019 13:18:28 +0000 (15:18 +0200)
client/src/app/+admin/plugins/plugin-list-installed/plugin-list-installed.component.ts
client/src/app/+admin/plugins/shared/plugin-api.service.ts
client/src/app/app.component.ts
client/src/app/core/plugins/hooks.service.ts
client/src/app/core/plugins/plugin.service.ts
client/src/types/register-client-option.model.ts
server/lib/plugins/plugin-manager.ts
shared/models/plugins/client-hook.model.ts

index 9809759dbe8a8b927d28b30b6818858fbec57c44..dced14dee9c0a9d3189c46f15d19ec66d43c4169 100644 (file)
@@ -7,6 +7,7 @@ import { ConfirmService, Notifier } from '@app/core'
 import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
 import { ActivatedRoute, Router } from '@angular/router'
 import { compareSemVer } from '@shared/core-utils/miscs/miscs'
+import { PluginService } from '@app/core/plugins/plugin.service'
 
 @Component({
   selector: 'my-plugin-list-installed',
@@ -34,13 +35,14 @@ export class PluginListInstalledComponent implements OnInit {
 
   constructor (
     private i18n: I18n,
-    private pluginService: PluginApiService,
+    private pluginService: PluginService,
+    private pluginApiService: PluginApiService,
     private notifier: Notifier,
     private confirmService: ConfirmService,
     private router: Router,
     private route: ActivatedRoute
   ) {
-    this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
+    this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
   }
 
   ngOnInit () {
@@ -60,7 +62,7 @@ export class PluginListInstalledComponent implements OnInit {
   }
 
   loadMorePlugins () {
-    this.pluginService.getPlugins(this.pluginType, this.pagination, this.sort)
+    this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
         .subscribe(
           res => {
             this.plugins = this.plugins.concat(res.data)
@@ -106,7 +108,7 @@ export class PluginListInstalledComponent implements OnInit {
     )
     if (res === false) return
 
-    this.pluginService.uninstall(plugin.name, plugin.type)
+    this.pluginApiService.uninstall(plugin.name, plugin.type)
       .subscribe(
         () => {
           this.notifier.success(this.i18n('{{pluginName}} uninstalled.', { pluginName: plugin.name }))
@@ -125,7 +127,7 @@ export class PluginListInstalledComponent implements OnInit {
 
     this.updating[updatingKey] = true
 
-    this.pluginService.update(plugin.name, plugin.type)
+    this.pluginApiService.update(plugin.name, plugin.type)
         .pipe()
         .subscribe(
           res => {
index bfcaec011e41ef7bc3028a33ea201934d7061718..343eb57b28d7a5047018326030b55d13a4026fa7 100644 (file)
@@ -12,16 +12,18 @@ import { ManagePlugin } from '@shared/models/plugins/manage-plugin.model'
 import { InstallOrUpdatePlugin } from '@shared/models/plugins/install-plugin.model'
 import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
 import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
+import { PluginService } from '@app/core/plugins/plugin.service'
 
 @Injectable()
 export class PluginApiService {
-  private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/plugins'
+  private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
 
   constructor (
     private authHttp: HttpClient,
     private restExtractor: RestExtractor,
     private restService: RestService,
-    private i18n: I18n
+    private i18n: I18n,
+    private pluginService: PluginService
   ) { }
 
   getPluginTypeOptions () {
@@ -56,7 +58,7 @@ export class PluginApiService {
     params = this.restService.addRestGetParams(params, pagination, sort)
     params = params.append('pluginType', pluginType.toString())
 
-    return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_APPLICATION_URL, { params })
+    return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
                .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
@@ -74,26 +76,28 @@ export class PluginApiService {
 
     if (search) params = params.append('search', search)
 
-    return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_APPLICATION_URL + '/available', { params })
+    return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
                .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
   getPlugin (npmName: string) {
-    const path = PluginApiService.BASE_APPLICATION_URL + '/' + npmName
+    const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
 
     return this.authHttp.get<PeerTubePlugin>(path)
                .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
   getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
-    const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/registered-settings'
+    const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
+    const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
 
     return this.authHttp.get<{ settings: RegisterServerSettingOptions[] }>(path)
                .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
   updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
-    const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/settings'
+    const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
+    const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
 
     return this.authHttp.put(path, { settings })
                .pipe(catchError(res => this.restExtractor.handleError(res)))
@@ -101,19 +105,19 @@ export class PluginApiService {
 
   uninstall (pluginName: string, pluginType: PluginType) {
     const body: ManagePlugin = {
-      npmName: this.nameToNpmName(pluginName, pluginType)
+      npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
     }
 
-    return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/uninstall', body)
+    return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
                .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
   update (pluginName: string, pluginType: PluginType) {
     const body: ManagePlugin = {
-      npmName: this.nameToNpmName(pluginName, pluginType)
+      npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
     }
 
-    return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/update', body)
+    return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
                .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
@@ -122,21 +126,7 @@ export class PluginApiService {
       npmName
     }
 
-    return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/install', body)
+    return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
                .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
-
-  nameToNpmName (name: string, type: PluginType) {
-    const prefix = type === PluginType.PLUGIN
-      ? 'peertube-plugin-'
-      : 'peertube-theme-'
-
-    return prefix + name
-  }
-
-  pluginTypeFromNpmName (npmName: string) {
-    return npmName.startsWith('peertube-plugin-')
-      ? PluginType.PLUGIN
-      : PluginType.THEME
-  }
 }
index 14fd27784e96725e45e59d24ced1686f1110fd7a..db1f91f8caac4eb2e9a3fc7c48dea540da651f46 100644 (file)
@@ -162,6 +162,10 @@ export class AppComponent implements OnInit {
       filter(pathname => !pathname || pathname === '/' || is18nPath(pathname))
     ).subscribe(() => this.redirectService.redirectToHomepage(true))
 
+    navigationEndEvent.subscribe(e => {
+      this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url })
+    })
+
     eventsObs.pipe(
       filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart),
       filter(() => this.screenService.isInSmallView())
index 257e27e6bed7a78400492cc04662ef81c12c91ce..93dac1167cdecb8d6ee009089a8b5ea99c0652a8 100644 (file)
@@ -39,7 +39,7 @@ export class HooksService {
 
   runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) {
     this.pluginService.ensurePluginsAreLoaded(scope)
-        .then(() => this.pluginService.runHook(hookName, params))
+        .then(() => this.pluginService.runHook(hookName, undefined, params))
         .catch((err: any) => console.error('Fatal hook error.', { err }))
   }
 }
index 1294edd7d2375987299f7a63de4e32548795abbc..45d8088a48a90bfefe06de64bae6073a380333a1 100644 (file)
@@ -6,11 +6,15 @@ import { ClientScript } from '@shared/models/plugins/plugin-package-json.model'
 import { ClientScript as ClientScriptModule } from '../../../types/client-script.model'
 import { environment } from '../../../environments/environment'
 import { ReplaySubject } from 'rxjs'
-import { first, shareReplay } from 'rxjs/operators'
+import { catchError, first, map, shareReplay } from 'rxjs/operators'
 import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks'
 import { ClientHook, ClientHookName, clientHookObject } from '@shared/models/plugins/client-hook.model'
 import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type'
 import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model'
+import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
+import { HttpClient } from '@angular/common/http'
+import { RestExtractor } from '@app/shared/rest'
+import { PluginType } from '@shared/models/plugins/plugin.type'
 
 interface HookStructValue extends RegisterClientHookOptions {
   plugin: ServerConfigPlugin
@@ -20,11 +24,14 @@ interface HookStructValue extends RegisterClientHookOptions {
 type PluginInfo = {
   plugin: ServerConfigPlugin
   clientScript: ClientScript
+  pluginType: PluginType
   isTheme: boolean
 }
 
 @Injectable()
 export class PluginService implements ClientHook {
+  private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
+
   pluginsBuilt = new ReplaySubject<boolean>(1)
 
   pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = {
@@ -43,7 +50,9 @@ export class PluginService implements ClientHook {
 
   constructor (
     private router: Router,
-    private server: ServerService
+    private server: ServerService,
+    private authHttp: HttpClient,
+    private restExtractor: RestExtractor
   ) {
   }
 
@@ -87,6 +96,7 @@ export class PluginService implements ClientHook {
             script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
             scopes: clientScript.scopes
           },
+          pluginType: isTheme ? PluginType.THEME : PluginType.PLUGIN,
           isTheme
         })
 
@@ -162,6 +172,20 @@ export class PluginService implements ClientHook {
     return result
   }
 
+  nameToNpmName (name: string, type: PluginType) {
+    const prefix = type === PluginType.PLUGIN
+      ? 'peertube-plugin-'
+      : 'peertube-theme-'
+
+    return prefix + name
+  }
+
+  pluginTypeFromNpmName (npmName: string) {
+    return npmName.startsWith('peertube-plugin-')
+      ? PluginType.PLUGIN
+      : PluginType.THEME
+  }
+
   private loadPlugin (pluginInfo: PluginInfo) {
     const { plugin, clientScript } = pluginInfo
 
@@ -189,6 +213,7 @@ export class PluginService implements ClientHook {
     return import(/* webpackIgnore: true */ clientScript.script)
       .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers }))
       .then(() => this.sortHooksByPriority())
+      .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
   }
 
   private buildScopeStruct () {
@@ -212,6 +237,18 @@ export class PluginService implements ClientHook {
       getBaseStaticRoute: () => {
         const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme)
         return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
+      },
+
+      getSettings: () => {
+        const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
+        const path = PluginService.BASE_PLUGIN_URL + '/' + npmName
+
+        return this.authHttp.get<PeerTubePlugin>(path)
+                   .pipe(
+                     map(p => p.settings),
+                     catchError(res => this.restExtractor.handleError(res))
+                   )
+                   .toPromise()
       }
     }
   }
index 42d689403e5c2eeff3487de35665b9e2db976a41..473c2500f0d82e947b47702d0a97d77d444fa808 100644 (file)
@@ -5,5 +5,7 @@ export type RegisterClientOptions = {
 
   peertubeHelpers: {
     getBaseStaticRoute: () => string
+
+    getSettings: () => Promise<{ [ name: string ]: string }>
   }
 }
index 381a894736795777bd57197354500ead1953d8c6..78e8d758f88218324f287ac964cd583344408e50 100644 (file)
@@ -151,6 +151,7 @@ export class PluginManager implements ServerHook {
     }
 
     delete this.registeredPlugins[plugin.npmName]
+    delete this.settings[plugin.npmName]
 
     if (plugin.type === PluginType.PLUGIN) {
       await plugin.unregister()
index 87e8092c0734f82ebb15dbf51c8d9a83be8e2416..cfa2653c6ea5fd63c294b7bd254b47a50b6f4cdc 100644 (file)
@@ -49,7 +49,10 @@ export const clientActionHookObject = {
   'action:video-watch.video.loaded': true,
 
   // Fired when the search page is being initialized
-  'action:search.init': true
+  'action:search.init': true,
+
+  // Fired every time Angular URL changes
+  'action:router.navigation-end': true
 }
 
 export type ClientActionHookName = keyof typeof clientActionHookObject