]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/plugins/hooks.service.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / hooks.service.ts
CommitLineData
f375bb3d
C
1import { from, Observable } from 'rxjs'
2import { mergeMap, switchMap } from 'rxjs/operators'
93cae479
C
3import { Injectable } from '@angular/core'
4import { PluginService } from '@app/core/plugins/plugin.service'
42b40636 5import { logger } from '@root-helpers/logger'
67ed6552 6import { ClientActionHookName, ClientFilterHookName, PluginClientScope } from '@shared/models'
faeec106 7import { AuthService, AuthStatus } from '../auth'
93cae479
C
8
9type RawFunction<U, T> = (params: U) => T
10type ObservableFunction<U, T> = RawFunction<U, Observable<T>>
11
12@Injectable()
13export class HooksService {
faeec106
C
14 constructor (
15 private authService: AuthService,
16 private pluginService: PluginService
17 ) {
18 // Run auth hooks
19 this.authService.userInformationLoaded
20 .subscribe(() => this.runAction('action:auth-user.information-loaded', 'common', { user: this.authService.getUser() }))
21
22 this.authService.loginChangedSource.subscribe(obj => {
23 if (obj === AuthStatus.LoggedIn) {
24 this.runAction('action:auth-user.logged-in', 'common')
25 } else if (obj === AuthStatus.LoggedOut) {
26 this.runAction('action:auth-user.logged-out', 'common')
27 }
28 })
29 }
93cae479 30
9df52d66
C
31 wrapObsFun <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
32 (fun: ObservableFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) {
93cae479
C
33 return from(this.pluginService.ensurePluginsAreLoaded(scope))
34 .pipe(
0912f1b4 35 mergeMap(() => this.wrapObjectWithoutScopeLoad(params, hookParamName)),
93cae479
C
36 switchMap(params => fun(params)),
37 mergeMap(result => this.pluginService.runHook(hookResultName, result, params))
38 )
39 }
40
9df52d66
C
41 async wrapFun <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
42 (fun: RawFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) {
c2023a9f
C
43 await this.pluginService.ensurePluginsAreLoaded(scope)
44
0912f1b4 45 const newParams = await this.wrapObjectWithoutScopeLoad(params, hookParamName)
c2023a9f 46 const result = fun(newParams)
93cae479 47
c2023a9f 48 return this.pluginService.runHook(hookResultName, result, params)
93cae479
C
49 }
50
c9e3eeed 51 runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) {
9ca0f688
C
52 // Use setTimeout to give priority to Angular change detector
53 setTimeout(() => {
54 this.pluginService.ensurePluginsAreLoaded(scope)
23bdacf8 55 .then(() => this.pluginService.runHook(hookName, undefined, params))
42b40636 56 .catch((err: any) => logger.error('Fatal hook error.', err))
9ca0f688 57 })
93cae479 58 }
c2023a9f 59
3b504f6e 60 async wrapObject<T, U extends ClientFilterHookName> (result: T, scope: PluginClientScope, hookName: U, context?: any) {
0912f1b4
C
61 await this.pluginService.ensurePluginsAreLoaded(scope)
62
3b504f6e 63 return this.wrapObjectWithoutScopeLoad(result, hookName, context)
0912f1b4
C
64 }
65
3b504f6e
C
66 private wrapObjectWithoutScopeLoad<T, U extends ClientFilterHookName> (result: T, hookName: U, context?: any) {
67 return this.pluginService.runHook(hookName, result, context)
c2023a9f 68 }
93cae479 69}