X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fplugins%2Fhooks.service.ts;h=d9fef838978dd874ba8b246d4166da0772946351;hb=HEAD;hp=a6a444c326a4b3392f1cd651890c07894922b224;hpb=0912f1b4caaa7394516567044f896132ad991131;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/plugins/hooks.service.ts b/client/src/app/core/plugins/hooks.service.ts index a6a444c32..d9fef8389 100644 --- a/client/src/app/core/plugins/hooks.service.ts +++ b/client/src/app/core/plugins/hooks.service.ts @@ -1,10 +1,10 @@ -import { Injectable } from '@angular/core' -import { PluginService } from '@app/core/plugins/plugin.service' -import { ClientActionHookName, ClientFilterHookName } from '@shared/models/plugins/client-hook.model' import { from, Observable } from 'rxjs' import { mergeMap, switchMap } from 'rxjs/operators' -import { ServerService } from '@app/core/server' -import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type' +import { Injectable } from '@angular/core' +import { PluginService } from '@app/core/plugins/plugin.service' +import { logger } from '@root-helpers/logger' +import { ClientActionHookName, ClientFilterHookName, PluginClientScope } from '@shared/models' +import { AuthService, AuthStatus } from '../auth' type RawFunction = (params: U) => T type ObservableFunction = RawFunction> @@ -12,13 +12,24 @@ type ObservableFunction = RawFunction> @Injectable() export class HooksService { constructor ( - private server: ServerService, + private authService: AuthService, private pluginService: PluginService - ) { } + ) { + // Run auth hooks + this.authService.userInformationLoaded + .subscribe(() => this.runAction('action:auth-user.information-loaded', 'common', { user: this.authService.getUser() })) + + this.authService.loginChangedSource.subscribe(obj => { + if (obj === AuthStatus.LoggedIn) { + this.runAction('action:auth-user.logged-in', 'common') + } else if (obj === AuthStatus.LoggedOut) { + this.runAction('action:auth-user.logged-out', 'common') + } + }) + } - wrapObsFun - - (fun: ObservableFunction, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { + wrapObsFun + (fun: ObservableFunction, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { return from(this.pluginService.ensurePluginsAreLoaded(scope)) .pipe( mergeMap(() => this.wrapObjectWithoutScopeLoad(params, hookParamName)), @@ -27,9 +38,8 @@ export class HooksService { ) } - async wrapFun - - (fun: RawFunction, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { + async wrapFun + (fun: RawFunction, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) { await this.pluginService.ensurePluginsAreLoaded(scope) const newParams = await this.wrapObjectWithoutScopeLoad(params, hookParamName) @@ -39,18 +49,21 @@ export class HooksService { } runAction (hookName: U, scope: PluginClientScope, params?: T) { - this.pluginService.ensurePluginsAreLoaded(scope) + // Use setTimeout to give priority to Angular change detector + setTimeout(() => { + this.pluginService.ensurePluginsAreLoaded(scope) .then(() => this.pluginService.runHook(hookName, undefined, params)) - .catch((err: any) => console.error('Fatal hook error.', { err })) + .catch((err: any) => logger.error('Fatal hook error.', err)) + }) } - async wrapObject (result: T, scope: PluginClientScope, hookName: U) { + async wrapObject (result: T, scope: PluginClientScope, hookName: U, context?: any) { await this.pluginService.ensurePluginsAreLoaded(scope) - return this.wrapObjectWithoutScopeLoad(result, hookName) + return this.wrapObjectWithoutScopeLoad(result, hookName, context) } - private wrapObjectWithoutScopeLoad (result: T, hookName: U) { - return this.pluginService.runHook(hookName, result) + private wrapObjectWithoutScopeLoad (result: T, hookName: U, context?: any) { + return this.pluginService.runHook(hookName, result, context) } }