diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-25 19:02:54 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-07-26 15:18:28 +0200 |
commit | 23bdacf8ec24ce47a15529830e116911d7478598 (patch) | |
tree | 934e6acd8e2715d528a102e55d5ea95d52c1659e /client/src/app/core | |
parent | a1758df8a3c3f866460edd8f9bbc94e8dd41fd80 (diff) | |
download | PeerTube-23bdacf8ec24ce47a15529830e116911d7478598.tar.gz PeerTube-23bdacf8ec24ce47a15529830e116911d7478598.tar.zst PeerTube-23bdacf8ec24ce47a15529830e116911d7478598.zip |
Add setting helper to client plugins
Diffstat (limited to 'client/src/app/core')
-rw-r--r-- | client/src/app/core/plugins/hooks.service.ts | 2 | ||||
-rw-r--r-- | client/src/app/core/plugins/plugin.service.ts | 41 |
2 files changed, 40 insertions, 3 deletions
diff --git a/client/src/app/core/plugins/hooks.service.ts b/client/src/app/core/plugins/hooks.service.ts index 257e27e6b..93dac1167 100644 --- a/client/src/app/core/plugins/hooks.service.ts +++ b/client/src/app/core/plugins/hooks.service.ts | |||
@@ -39,7 +39,7 @@ export class HooksService { | |||
39 | 39 | ||
40 | runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) { | 40 | runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) { |
41 | this.pluginService.ensurePluginsAreLoaded(scope) | 41 | this.pluginService.ensurePluginsAreLoaded(scope) |
42 | .then(() => this.pluginService.runHook(hookName, params)) | 42 | .then(() => this.pluginService.runHook(hookName, undefined, params)) |
43 | .catch((err: any) => console.error('Fatal hook error.', { err })) | 43 | .catch((err: any) => console.error('Fatal hook error.', { err })) |
44 | } | 44 | } |
45 | } | 45 | } |
diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts index 1294edd7d..45d8088a4 100644 --- a/client/src/app/core/plugins/plugin.service.ts +++ b/client/src/app/core/plugins/plugin.service.ts | |||
@@ -6,11 +6,15 @@ import { ClientScript } from '@shared/models/plugins/plugin-package-json.model' | |||
6 | import { ClientScript as ClientScriptModule } from '../../../types/client-script.model' | 6 | import { ClientScript as ClientScriptModule } from '../../../types/client-script.model' |
7 | import { environment } from '../../../environments/environment' | 7 | import { environment } from '../../../environments/environment' |
8 | import { ReplaySubject } from 'rxjs' | 8 | import { ReplaySubject } from 'rxjs' |
9 | import { first, shareReplay } from 'rxjs/operators' | 9 | import { catchError, first, map, shareReplay } from 'rxjs/operators' |
10 | import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' | 10 | import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' |
11 | import { ClientHook, ClientHookName, clientHookObject } from '@shared/models/plugins/client-hook.model' | 11 | import { ClientHook, ClientHookName, clientHookObject } from '@shared/models/plugins/client-hook.model' |
12 | import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type' | 12 | import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type' |
13 | import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model' | 13 | import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model' |
14 | import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model' | ||
15 | import { HttpClient } from '@angular/common/http' | ||
16 | import { RestExtractor } from '@app/shared/rest' | ||
17 | import { PluginType } from '@shared/models/plugins/plugin.type' | ||
14 | 18 | ||
15 | interface HookStructValue extends RegisterClientHookOptions { | 19 | interface HookStructValue extends RegisterClientHookOptions { |
16 | plugin: ServerConfigPlugin | 20 | plugin: ServerConfigPlugin |
@@ -20,11 +24,14 @@ interface HookStructValue extends RegisterClientHookOptions { | |||
20 | type PluginInfo = { | 24 | type PluginInfo = { |
21 | plugin: ServerConfigPlugin | 25 | plugin: ServerConfigPlugin |
22 | clientScript: ClientScript | 26 | clientScript: ClientScript |
27 | pluginType: PluginType | ||
23 | isTheme: boolean | 28 | isTheme: boolean |
24 | } | 29 | } |
25 | 30 | ||
26 | @Injectable() | 31 | @Injectable() |
27 | export class PluginService implements ClientHook { | 32 | export class PluginService implements ClientHook { |
33 | private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins' | ||
34 | |||
28 | pluginsBuilt = new ReplaySubject<boolean>(1) | 35 | pluginsBuilt = new ReplaySubject<boolean>(1) |
29 | 36 | ||
30 | pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = { | 37 | pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = { |
@@ -43,7 +50,9 @@ export class PluginService implements ClientHook { | |||
43 | 50 | ||
44 | constructor ( | 51 | constructor ( |
45 | private router: Router, | 52 | private router: Router, |
46 | private server: ServerService | 53 | private server: ServerService, |
54 | private authHttp: HttpClient, | ||
55 | private restExtractor: RestExtractor | ||
47 | ) { | 56 | ) { |
48 | } | 57 | } |
49 | 58 | ||
@@ -87,6 +96,7 @@ export class PluginService implements ClientHook { | |||
87 | script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`, | 96 | script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`, |
88 | scopes: clientScript.scopes | 97 | scopes: clientScript.scopes |
89 | }, | 98 | }, |
99 | pluginType: isTheme ? PluginType.THEME : PluginType.PLUGIN, | ||
90 | isTheme | 100 | isTheme |
91 | }) | 101 | }) |
92 | 102 | ||
@@ -162,6 +172,20 @@ export class PluginService implements ClientHook { | |||
162 | return result | 172 | return result |
163 | } | 173 | } |
164 | 174 | ||
175 | nameToNpmName (name: string, type: PluginType) { | ||
176 | const prefix = type === PluginType.PLUGIN | ||
177 | ? 'peertube-plugin-' | ||
178 | : 'peertube-theme-' | ||
179 | |||
180 | return prefix + name | ||
181 | } | ||
182 | |||
183 | pluginTypeFromNpmName (npmName: string) { | ||
184 | return npmName.startsWith('peertube-plugin-') | ||
185 | ? PluginType.PLUGIN | ||
186 | : PluginType.THEME | ||
187 | } | ||
188 | |||
165 | private loadPlugin (pluginInfo: PluginInfo) { | 189 | private loadPlugin (pluginInfo: PluginInfo) { |
166 | const { plugin, clientScript } = pluginInfo | 190 | const { plugin, clientScript } = pluginInfo |
167 | 191 | ||
@@ -189,6 +213,7 @@ export class PluginService implements ClientHook { | |||
189 | return import(/* webpackIgnore: true */ clientScript.script) | 213 | return import(/* webpackIgnore: true */ clientScript.script) |
190 | .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers })) | 214 | .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers })) |
191 | .then(() => this.sortHooksByPriority()) | 215 | .then(() => this.sortHooksByPriority()) |
216 | .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) | ||
192 | } | 217 | } |
193 | 218 | ||
194 | private buildScopeStruct () { | 219 | private buildScopeStruct () { |
@@ -212,6 +237,18 @@ export class PluginService implements ClientHook { | |||
212 | getBaseStaticRoute: () => { | 237 | getBaseStaticRoute: () => { |
213 | const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme) | 238 | const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme) |
214 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static` | 239 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static` |
240 | }, | ||
241 | |||
242 | getSettings: () => { | ||
243 | const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType) | ||
244 | const path = PluginService.BASE_PLUGIN_URL + '/' + npmName | ||
245 | |||
246 | return this.authHttp.get<PeerTubePlugin>(path) | ||
247 | .pipe( | ||
248 | map(p => p.settings), | ||
249 | catchError(res => this.restExtractor.handleError(res)) | ||
250 | ) | ||
251 | .toPromise() | ||
215 | } | 252 | } |
216 | } | 253 | } |
217 | } | 254 | } |