aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/plugins/plugin.service.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-25 19:02:54 +0200
committerChocobozzz <me@florianbigard.com>2019-07-26 15:18:28 +0200
commit23bdacf8ec24ce47a15529830e116911d7478598 (patch)
tree934e6acd8e2715d528a102e55d5ea95d52c1659e /client/src/app/core/plugins/plugin.service.ts
parenta1758df8a3c3f866460edd8f9bbc94e8dd41fd80 (diff)
downloadPeerTube-23bdacf8ec24ce47a15529830e116911d7478598.tar.gz
PeerTube-23bdacf8ec24ce47a15529830e116911d7478598.tar.zst
PeerTube-23bdacf8ec24ce47a15529830e116911d7478598.zip
Add setting helper to client plugins
Diffstat (limited to 'client/src/app/core/plugins/plugin.service.ts')
-rw-r--r--client/src/app/core/plugins/plugin.service.ts41
1 files changed, 39 insertions, 2 deletions
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'
6import { ClientScript as ClientScriptModule } from '../../../types/client-script.model' 6import { ClientScript as ClientScriptModule } from '../../../types/client-script.model'
7import { environment } from '../../../environments/environment' 7import { environment } from '../../../environments/environment'
8import { ReplaySubject } from 'rxjs' 8import { ReplaySubject } from 'rxjs'
9import { first, shareReplay } from 'rxjs/operators' 9import { catchError, first, map, shareReplay } from 'rxjs/operators'
10import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' 10import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks'
11import { ClientHook, ClientHookName, clientHookObject } from '@shared/models/plugins/client-hook.model' 11import { ClientHook, ClientHookName, clientHookObject } from '@shared/models/plugins/client-hook.model'
12import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type' 12import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type'
13import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model' 13import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model'
14import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
15import { HttpClient } from '@angular/common/http'
16import { RestExtractor } from '@app/shared/rest'
17import { PluginType } from '@shared/models/plugins/plugin.type'
14 18
15interface HookStructValue extends RegisterClientHookOptions { 19interface HookStructValue extends RegisterClientHookOptions {
16 plugin: ServerConfigPlugin 20 plugin: ServerConfigPlugin
@@ -20,11 +24,14 @@ interface HookStructValue extends RegisterClientHookOptions {
20type PluginInfo = { 24type 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()
27export class PluginService implements ClientHook { 32export 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 }