X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fplugins%2Fplugin.service.ts;h=871613b89f6712f86a04f4e84eba6c9902106f74;hb=f95628636b6ccdf3eae2449ca718e075b072f678;hp=039fd6ff1ee8108f6a955a223afad790b7b0c8c6;hpb=891bc2ffadd5dedae316fcc80856ff859e6f8336;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 039fd6ff1..871613b89 100644 --- a/client/src/app/core/plugins/plugin.service.ts +++ b/client/src/app/core/plugins/plugin.service.ts @@ -1,39 +1,27 @@ -import { Inject, Injectable, LOCALE_ID, NgZone } from '@angular/core' -import { Router } from '@angular/router' -import { getCompleteLocale, isDefaultLocale, peertubeTranslate, ServerConfigPlugin } from '@shared/models' -import { ServerService } from '@app/core/server/server.service' -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 { Observable, of, ReplaySubject } from 'rxjs' 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 { HttpClient } from '@angular/common/http' +import { Inject, Injectable, LOCALE_ID, NgZone } from '@angular/core' import { AuthService } from '@app/core/auth' import { Notifier } from '@app/core/notification' -import { RestExtractor } from '@app/shared/rest' -import { PluginType } from '@shared/models/plugins/plugin.type' -import { PublicServerSetting } from '@shared/models/plugins/public-server.setting' -import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' -import { RegisterClientHelpers } from '../../../types/register-client-option.model' -import { PluginTranslation } from '@shared/models/plugins/plugin-translation.model' -import { importModule } from '@app/shared/misc/utils' +import { MarkdownService } from '@app/core/renderer' +import { RestExtractor } from '@app/core/rest' +import { ServerService } from '@app/core/server/server.service' +import { getDevLocale, isOnDevLocale } from '@app/helpers' import { CustomModalComponent } from '@app/modal/custom-modal.component' - -interface HookStructValue extends RegisterClientHookOptions { - plugin: ServerConfigPlugin - clientScript: ClientScript -} - -type PluginInfo = { - plugin: ServerConfigPlugin - clientScript: ClientScript - pluginType: PluginType - isTheme: boolean -} +import { Hooks, loadPlugin, PluginInfo, runHook } from '@root-helpers/plugins' +import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n' +import { + ClientHook, + ClientHookName, + PluginClientScope, + PluginTranslation, + PluginType, + PublicServerSetting, + ServerConfigPlugin +} from '@shared/models' +import { environment } from '../../../environments/environment' +import { RegisterClientHelpers } from '../../../types/register-client-option.model' @Injectable() export class PluginService implements ClientHook { @@ -46,7 +34,9 @@ export class PluginService implements ClientHook { common: new ReplaySubject(1), search: new ReplaySubject(1), 'video-watch': new ReplaySubject(1), - signup: new ReplaySubject(1) + signup: new ReplaySubject(1), + login: new ReplaySubject(1), + embed: new ReplaySubject(1) } translationsObservable: Observable @@ -59,12 +49,12 @@ export class PluginService implements ClientHook { private loadedScopes: PluginClientScope[] = [] private loadingScopes: { [id in PluginClientScope]?: boolean } = {} - private hooks: { [ name: string ]: HookStructValue[] } = {} + private hooks: Hooks = {} constructor ( - private router: Router, private authService: AuthService, private notifier: Notifier, + private markdownRenderer: MarkdownService, private server: ServerService, private zone: NgZone, private authHttp: HttpClient, @@ -115,7 +105,7 @@ export class PluginService implements ClientHook { this.scopes[scope].push({ plugin, clientScript: { - script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`, + script: `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`, scopes: clientScript.scopes }, pluginType: isTheme ? PluginType.THEME : PluginType.PLUGIN, @@ -179,20 +169,8 @@ export class PluginService implements ClientHook { } runHook (hookName: ClientHookName, result?: T, params?: any): Promise { - return this.zone.runOutsideAngular(async () => { - if (!this.hooks[ hookName ]) return result - - const hookType = getHookType(hookName) - - for (const hook of this.hooks[ hookName ]) { - console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name) - - result = await internalRunHook(hook.handler, hookType, result, params, err => { - console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err) - }) - } - - return result + return this.zone.runOutsideAngular(() => { + return runHook(this.hooks, hookName, result, params) }) } @@ -211,34 +189,8 @@ export class PluginService implements ClientHook { } private loadPlugin (pluginInfo: PluginInfo) { - const { plugin, clientScript } = pluginInfo - - const registerHook = (options: RegisterClientHookOptions) => { - if (clientHookObject[options.target] !== true) { - console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name) - return - } - - if (!this.hooks[options.target]) this.hooks[options.target] = [] - - this.hooks[options.target].push({ - plugin, - clientScript, - target: options.target, - handler: options.handler, - priority: options.priority || 0 - }) - } - - const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo) - - console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) - return this.zone.runOutsideAngular(() => { - return importModule(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)) + return loadPlugin(this.hooks, pluginInfo, pluginInfo => this.buildPeerTubeHelpers(pluginInfo)) }) } @@ -248,14 +200,6 @@ export class PluginService implements ClientHook { } } - private sortHooksByPriority () { - for (const hookName of Object.keys(this.hooks)) { - this.hooks[hookName].sort((a, b) => { - return b.priority - a.priority - }) - } - } - private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers { const { plugin } = pluginInfo const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType) @@ -297,6 +241,16 @@ export class PluginService implements ClientHook { this.customModal.show(input) }, + markdownRenderer: { + textMarkdownToHTML: (textMarkdown: string) => { + return this.markdownRenderer.textMarkdownToHTML(textMarkdown) + }, + + enhancedMarkdownToHTML: (enhancedMarkdown: string) => { + return this.markdownRenderer.enhancedMarkdownToHTML(enhancedMarkdown) + } + }, + translate: (value: string) => { return this.translationsObservable .pipe(map(allTranslations => allTranslations[npmName]))