]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/plugins/hooks.service.ts
Add ability for plugins to add metadata
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / hooks.service.ts
1 import { from, Observable } from 'rxjs'
2 import { mergeMap, switchMap } from 'rxjs/operators'
3 import { Injectable } from '@angular/core'
4 import { PluginService } from '@app/core/plugins/plugin.service'
5 import { logger } from '@root-helpers/logger'
6 import { ClientActionHookName, ClientFilterHookName, PluginClientScope } from '@shared/models'
7 import { AuthService, AuthStatus } from '../auth'
8
9 type RawFunction<U, T> = (params: U) => T
10 type ObservableFunction<U, T> = RawFunction<U, Observable<T>>
11
12 @Injectable()
13 export class HooksService {
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 }
30
31 wrapObsFun <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
32 (fun: ObservableFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) {
33 return from(this.pluginService.ensurePluginsAreLoaded(scope))
34 .pipe(
35 mergeMap(() => this.wrapObjectWithoutScopeLoad(params, hookParamName)),
36 switchMap(params => fun(params)),
37 mergeMap(result => this.pluginService.runHook(hookResultName, result, params))
38 )
39 }
40
41 async wrapFun <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
42 (fun: RawFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) {
43 await this.pluginService.ensurePluginsAreLoaded(scope)
44
45 const newParams = await this.wrapObjectWithoutScopeLoad(params, hookParamName)
46 const result = fun(newParams)
47
48 return this.pluginService.runHook(hookResultName, result, params)
49 }
50
51 async wrapFunResult <P, R, H extends ClientFilterHookName>
52 (fun: RawFunction<P, R>, params: P, scope: PluginClientScope, hookResultName: H) {
53 await this.pluginService.ensurePluginsAreLoaded(scope)
54
55 const result = fun(params)
56
57 return this.pluginService.runHook(hookResultName, result, params)
58 }
59
60 runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) {
61 // Use setTimeout to give priority to Angular change detector
62 setTimeout(() => {
63 this.pluginService.ensurePluginsAreLoaded(scope)
64 .then(() => this.pluginService.runHook(hookName, undefined, params))
65 .catch((err: any) => logger.error('Fatal hook error.', err))
66 })
67 }
68
69 async wrapObject<T, U extends ClientFilterHookName> (result: T, scope: PluginClientScope, hookName: U, context?: any) {
70 await this.pluginService.ensurePluginsAreLoaded(scope)
71
72 return this.wrapObjectWithoutScopeLoad(result, hookName, context)
73 }
74
75 private wrapObjectWithoutScopeLoad<T, U extends ClientFilterHookName> (result: T, hookName: U, context?: any) {
76 return this.pluginService.runHook(hookName, result, context)
77 }
78 }