X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fplugins%2Fplugin.service.ts;h=dff8ad864754469790b04c1f9a552cfda236de34;hb=584ac47a323d6e57233fce4451d43d4943bfaa10;hp=54dba5e178b5ee8095bb57ec58cd74a65335f0fc;hpb=a786d8a08bf99f339bf16808f46e160404497ae2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts index 54dba5e17..dff8ad864 100644 --- a/client/src/app/core/plugins/plugin.service.ts +++ b/client/src/app/core/plugins/plugin.service.ts @@ -19,10 +19,14 @@ import { PluginTranslation, PluginType, PublicServerSetting, + RegisterClientSettingsScript, ServerConfigPlugin } from '@shared/models' import { environment } from '../../../environments/environment' import { RegisterClientHelpers } from '../../../types/register-client-option.model' +import * as debug from 'debug' + +const logger = debug('peertube:plugins') @Injectable() export class PluginService implements ClientHook { @@ -33,6 +37,7 @@ export class PluginService implements ClientHook { pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject } = { common: new ReplaySubject(1), + 'admin-plugin': new ReplaySubject(1), search: new ReplaySubject(1), 'video-watch': new ReplaySubject(1), signup: new ReplaySubject(1), @@ -46,7 +51,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 } = {} @@ -55,6 +63,7 @@ export class PluginService implements ClientHook { private formFields: FormFields = { video: [] } + private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {} constructor ( private authService: AuthService, @@ -70,6 +79,8 @@ export class PluginService implements ClientHook { } initializePlugins () { + logger('Building plugin configuration') + this.server.getConfig() .subscribe(config => { this.plugins = config.plugin.registered @@ -77,6 +88,8 @@ export class PluginService implements ClientHook { this.buildScopeStruct() this.pluginsBuilt.next(true) + + logger('Plugin configuration built') }) } @@ -140,6 +153,8 @@ export class PluginService implements ClientHook { this.loadingScopes[scope] = true + logger('Loading scope %s', scope) + try { await this.ensurePluginsAreBuilt() @@ -150,6 +165,7 @@ export class PluginService implements ClientHook { this.loadingScopes[scope] = false this.pluginsLoaded[scope].next(true) + logger('Nothing to load for scope %s', scope) return } @@ -168,6 +184,8 @@ export class PluginService implements ClientHook { this.pluginsLoaded[scope].next(true) this.loadingScopes[scope] = false + + logger('Scope %s loaded', scope) } catch (err) { console.error('Cannot load plugins by scope %s.', scope, err) } @@ -197,13 +215,33 @@ export class PluginService implements ClientHook { 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 }) }) } @@ -224,6 +262,11 @@ export class PluginService implements ClientHook { return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static` }, + getBaseRouterRoute: () => { + const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme) + return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/router` + }, + getSettings: () => { const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings' @@ -245,6 +288,13 @@ export class PluginService implements ClientHook { 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),