diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-08 15:54:08 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 18a6f04c071f7a0735eb39b8c67fd51a082d1a31 (patch) | |
tree | b9fb0637878390d32b5c73d02b8eee2ef48cbfa5 /client/src/app/core | |
parent | 2c0539420d77339e6afe8d7920b44af4c0dcb1e6 (diff) | |
download | PeerTube-18a6f04c071f7a0735eb39b8c67fd51a082d1a31.tar.gz PeerTube-18a6f04c071f7a0735eb39b8c67fd51a082d1a31.tar.zst PeerTube-18a6f04c071f7a0735eb39b8c67fd51a082d1a31.zip |
WIP plugins: hook on client side
Diffstat (limited to 'client/src/app/core')
-rw-r--r-- | client/src/app/core/core.module.ts | 3 | ||||
-rw-r--r-- | client/src/app/core/plugins/plugin.service.ts | 137 | ||||
-rw-r--r-- | client/src/app/core/server/server.service.ts | 1 |
3 files changed, 141 insertions, 0 deletions
diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.ts index 06fa8fcf1..436c0dfb8 100644 --- a/client/src/app/core/core.module.ts +++ b/client/src/app/core/core.module.ts | |||
@@ -21,6 +21,7 @@ import { MessageService } from 'primeng/api' | |||
21 | import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service' | 21 | import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service' |
22 | import { ServerConfigResolver } from './routing/server-config-resolver.service' | 22 | import { ServerConfigResolver } from './routing/server-config-resolver.service' |
23 | import { UnloggedGuard } from '@app/core/routing/unlogged-guard.service' | 23 | import { UnloggedGuard } from '@app/core/routing/unlogged-guard.service' |
24 | import { PluginService } from '@app/core/plugins/plugin.service' | ||
24 | 25 | ||
25 | @NgModule({ | 26 | @NgModule({ |
26 | imports: [ | 27 | imports: [ |
@@ -61,6 +62,8 @@ import { UnloggedGuard } from '@app/core/routing/unlogged-guard.service' | |||
61 | UserRightGuard, | 62 | UserRightGuard, |
62 | UnloggedGuard, | 63 | UnloggedGuard, |
63 | 64 | ||
65 | PluginService, | ||
66 | |||
64 | RedirectService, | 67 | RedirectService, |
65 | Notifier, | 68 | Notifier, |
66 | MessageService, | 69 | MessageService, |
diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts new file mode 100644 index 000000000..6c567d3ca --- /dev/null +++ b/client/src/app/core/plugins/plugin.service.ts | |||
@@ -0,0 +1,137 @@ | |||
1 | import { Injectable } from '@angular/core' | ||
2 | import { Router } from '@angular/router' | ||
3 | import { ServerConfigPlugin } from '@shared/models' | ||
4 | import { ServerService } from '@app/core/server/server.service' | ||
5 | import { ClientScript } from '@shared/models/plugins/plugin-package-json.model' | ||
6 | import { PluginScope } from '@shared/models/plugins/plugin-scope.type' | ||
7 | import { environment } from '../../../environments/environment' | ||
8 | import { RegisterHookOptions } from '@shared/models/plugins/register.model' | ||
9 | import { ReplaySubject } from 'rxjs' | ||
10 | import { first } from 'rxjs/operators' | ||
11 | |||
12 | interface HookStructValue extends RegisterHookOptions { | ||
13 | plugin: ServerConfigPlugin | ||
14 | clientScript: ClientScript | ||
15 | } | ||
16 | |||
17 | @Injectable() | ||
18 | export class PluginService { | ||
19 | pluginsLoaded = new ReplaySubject<boolean>(1) | ||
20 | |||
21 | private plugins: ServerConfigPlugin[] = [] | ||
22 | private scopes: { [ scopeName: string ]: { plugin: ServerConfigPlugin, clientScript: ClientScript }[] } = {} | ||
23 | private loadedScripts: { [ script: string ]: boolean } = {} | ||
24 | |||
25 | private hooks: { [ name: string ]: HookStructValue[] } = {} | ||
26 | |||
27 | constructor ( | ||
28 | private router: Router, | ||
29 | private server: ServerService | ||
30 | ) { | ||
31 | } | ||
32 | |||
33 | initializePlugins () { | ||
34 | this.server.configLoaded | ||
35 | .subscribe(() => { | ||
36 | this.plugins = this.server.getConfig().plugins | ||
37 | |||
38 | this.buildScopeStruct() | ||
39 | |||
40 | this.pluginsLoaded.next(true) | ||
41 | }) | ||
42 | } | ||
43 | |||
44 | ensurePluginsAreLoaded () { | ||
45 | return this.pluginsLoaded.asObservable() | ||
46 | .pipe(first()) | ||
47 | .toPromise() | ||
48 | } | ||
49 | |||
50 | async loadPluginsByScope (scope: PluginScope) { | ||
51 | try { | ||
52 | await this.ensurePluginsAreLoaded() | ||
53 | |||
54 | const toLoad = this.scopes[ scope ] | ||
55 | if (!Array.isArray(toLoad)) return | ||
56 | |||
57 | const promises: Promise<any>[] = [] | ||
58 | for (const { plugin, clientScript } of toLoad) { | ||
59 | if (this.loadedScripts[ clientScript.script ]) continue | ||
60 | |||
61 | promises.push(this.loadPlugin(plugin, clientScript)) | ||
62 | |||
63 | this.loadedScripts[ clientScript.script ] = true | ||
64 | } | ||
65 | |||
66 | return Promise.all(promises) | ||
67 | } catch (err) { | ||
68 | console.error('Cannot load plugins by scope %s.', scope, err) | ||
69 | } | ||
70 | } | ||
71 | |||
72 | async runHook (hookName: string, param?: any) { | ||
73 | let result = param | ||
74 | |||
75 | const wait = hookName.startsWith('static:') | ||
76 | |||
77 | for (const hook of this.hooks[hookName]) { | ||
78 | try { | ||
79 | if (wait) result = await hook.handler(param) | ||
80 | else result = hook.handler() | ||
81 | } catch (err) { | ||
82 | console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err) | ||
83 | } | ||
84 | } | ||
85 | |||
86 | return result | ||
87 | } | ||
88 | |||
89 | private loadPlugin (plugin: ServerConfigPlugin, clientScript: ClientScript) { | ||
90 | const registerHook = (options: RegisterHookOptions) => { | ||
91 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | ||
92 | |||
93 | this.hooks[options.target].push({ | ||
94 | plugin, | ||
95 | clientScript, | ||
96 | target: options.target, | ||
97 | handler: options.handler, | ||
98 | priority: options.priority || 0 | ||
99 | }) | ||
100 | } | ||
101 | |||
102 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) | ||
103 | |||
104 | const url = environment.apiUrl + `/plugins/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}` | ||
105 | |||
106 | return import(/* webpackIgnore: true */ url) | ||
107 | .then(script => script.register({ registerHook })) | ||
108 | .then(() => this.sortHooksByPriority()) | ||
109 | } | ||
110 | |||
111 | private buildScopeStruct () { | ||
112 | for (const plugin of this.plugins) { | ||
113 | for (const key of Object.keys(plugin.clientScripts)) { | ||
114 | const clientScript = plugin.clientScripts[key] | ||
115 | |||
116 | for (const scope of clientScript.scopes) { | ||
117 | if (!this.scopes[scope]) this.scopes[scope] = [] | ||
118 | |||
119 | this.scopes[scope].push({ | ||
120 | plugin, | ||
121 | clientScript | ||
122 | }) | ||
123 | |||
124 | this.loadedScripts[clientScript.script] = false | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | |||
130 | private sortHooksByPriority () { | ||
131 | for (const hookName of Object.keys(this.hooks)) { | ||
132 | this.hooks[hookName].sort((a, b) => { | ||
133 | return b.priority - a.priority | ||
134 | }) | ||
135 | } | ||
136 | } | ||
137 | } | ||
diff --git a/client/src/app/core/server/server.service.ts b/client/src/app/core/server/server.service.ts index 689f25a40..80c52164d 100644 --- a/client/src/app/core/server/server.service.ts +++ b/client/src/app/core/server/server.service.ts | |||
@@ -42,6 +42,7 @@ export class ServerService { | |||
42 | css: '' | 42 | css: '' |
43 | } | 43 | } |
44 | }, | 44 | }, |
45 | plugins: [], | ||
45 | email: { | 46 | email: { |
46 | enabled: false | 47 | enabled: false |
47 | }, | 48 | }, |