]>
Commit | Line | Data |
---|---|---|
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 { environment } from '../../../environments/environment' | |
7 | import { RegisterHookOptions } from '@shared/models/plugins/register-hook.model' | |
8 | import { ReplaySubject } from 'rxjs' | |
9 | import { first, shareReplay } from 'rxjs/operators' | |
10 | import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' | |
11 | import { ClientHook, ClientHookName } from '@shared/models/plugins/client-hook.model' | |
12 | import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type' | |
13 | ||
14 | interface HookStructValue extends RegisterHookOptions { | |
15 | plugin: ServerConfigPlugin | |
16 | clientScript: ClientScript | |
17 | } | |
18 | ||
19 | type PluginInfo = { | |
20 | plugin: ServerConfigPlugin | |
21 | clientScript: ClientScript | |
22 | isTheme: boolean | |
23 | } | |
24 | ||
25 | @Injectable() | |
26 | export class PluginService implements ClientHook { | |
27 | pluginsBuilt = new ReplaySubject<boolean>(1) | |
28 | ||
29 | pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = { | |
30 | common: new ReplaySubject<boolean>(1), | |
31 | 'video-watch': new ReplaySubject<boolean>(1) | |
32 | } | |
33 | ||
34 | private plugins: ServerConfigPlugin[] = [] | |
35 | private scopes: { [ scopeName: string ]: PluginInfo[] } = {} | |
36 | private loadedScripts: { [ script: string ]: boolean } = {} | |
37 | private loadedScopes: PluginClientScope[] = [] | |
38 | ||
39 | private hooks: { [ name: string ]: HookStructValue[] } = {} | |
40 | ||
41 | constructor ( | |
42 | private router: Router, | |
43 | private server: ServerService | |
44 | ) { | |
45 | } | |
46 | ||
47 | initializePlugins () { | |
48 | this.server.configLoaded | |
49 | .subscribe(() => { | |
50 | this.plugins = this.server.getConfig().plugin.registered | |
51 | ||
52 | this.buildScopeStruct() | |
53 | ||
54 | this.pluginsBuilt.next(true) | |
55 | }) | |
56 | } | |
57 | ||
58 | ensurePluginsAreBuilt () { | |
59 | return this.pluginsBuilt.asObservable() | |
60 | .pipe(first(), shareReplay()) | |
61 | .toPromise() | |
62 | } | |
63 | ||
64 | ensurePluginsAreLoaded (scope: PluginClientScope) { | |
65 | return this.pluginsLoaded[scope].asObservable() | |
66 | .pipe(first(), shareReplay()) | |
67 | .toPromise() | |
68 | } | |
69 | ||
70 | addPlugin (plugin: ServerConfigPlugin, isTheme = false) { | |
71 | const pathPrefix = this.getPluginPathPrefix(isTheme) | |
72 | ||
73 | for (const key of Object.keys(plugin.clientScripts)) { | |
74 | const clientScript = plugin.clientScripts[key] | |
75 | ||
76 | for (const scope of clientScript.scopes) { | |
77 | if (!this.scopes[scope]) this.scopes[scope] = [] | |
78 | ||
79 | this.scopes[scope].push({ | |
80 | plugin, | |
81 | clientScript: { | |
82 | script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`, | |
83 | scopes: clientScript.scopes | |
84 | }, | |
85 | isTheme | |
86 | }) | |
87 | ||
88 | this.loadedScripts[clientScript.script] = false | |
89 | } | |
90 | } | |
91 | } | |
92 | ||
93 | removePlugin (plugin: ServerConfigPlugin) { | |
94 | for (const key of Object.keys(this.scopes)) { | |
95 | this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name) | |
96 | } | |
97 | } | |
98 | ||
99 | async reloadLoadedScopes () { | |
100 | for (const scope of this.loadedScopes) { | |
101 | await this.loadPluginsByScope(scope, true) | |
102 | } | |
103 | } | |
104 | ||
105 | async loadPluginsByScope (scope: PluginClientScope, isReload = false) { | |
106 | try { | |
107 | await this.ensurePluginsAreBuilt() | |
108 | ||
109 | if (!isReload) this.loadedScopes.push(scope) | |
110 | ||
111 | const toLoad = this.scopes[ scope ] | |
112 | if (!Array.isArray(toLoad)) return | |
113 | ||
114 | const promises: Promise<any>[] = [] | |
115 | for (const pluginInfo of toLoad) { | |
116 | const clientScript = pluginInfo.clientScript | |
117 | ||
118 | if (this.loadedScripts[ clientScript.script ]) continue | |
119 | ||
120 | promises.push(this.loadPlugin(pluginInfo)) | |
121 | ||
122 | this.loadedScripts[ clientScript.script ] = true | |
123 | } | |
124 | ||
125 | await Promise.all(promises) | |
126 | ||
127 | this.pluginsLoaded[scope].next(true) | |
128 | } catch (err) { | |
129 | console.error('Cannot load plugins by scope %s.', scope, err) | |
130 | } | |
131 | } | |
132 | ||
133 | async runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> { | |
134 | if (!this.hooks[hookName]) return Promise.resolve(result) | |
135 | ||
136 | const hookType = getHookType(hookName) | |
137 | ||
138 | for (const hook of this.hooks[hookName]) { | |
139 | console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name) | |
140 | ||
141 | result = await internalRunHook(hook.handler, hookType, result, params, err => { | |
142 | console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err) | |
143 | }) | |
144 | } | |
145 | ||
146 | return result | |
147 | } | |
148 | ||
149 | private loadPlugin (pluginInfo: PluginInfo) { | |
150 | const { plugin, clientScript } = pluginInfo | |
151 | ||
152 | const registerHook = (options: RegisterHookOptions) => { | |
153 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | |
154 | ||
155 | this.hooks[options.target].push({ | |
156 | plugin, | |
157 | clientScript, | |
158 | target: options.target, | |
159 | handler: options.handler, | |
160 | priority: options.priority || 0 | |
161 | }) | |
162 | } | |
163 | ||
164 | const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo) | |
165 | ||
166 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) | |
167 | ||
168 | return import(/* webpackIgnore: true */ clientScript.script) | |
169 | .then(script => script.register({ registerHook, peertubeHelpers })) | |
170 | .then(() => this.sortHooksByPriority()) | |
171 | } | |
172 | ||
173 | private buildScopeStruct () { | |
174 | for (const plugin of this.plugins) { | |
175 | this.addPlugin(plugin) | |
176 | } | |
177 | } | |
178 | ||
179 | private sortHooksByPriority () { | |
180 | for (const hookName of Object.keys(this.hooks)) { | |
181 | this.hooks[hookName].sort((a, b) => { | |
182 | return b.priority - a.priority | |
183 | }) | |
184 | } | |
185 | } | |
186 | ||
187 | private buildPeerTubeHelpers (pluginInfo: PluginInfo) { | |
188 | const { plugin } = pluginInfo | |
189 | ||
190 | return { | |
191 | getBaseStaticRoute: () => { | |
192 | const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme) | |
193 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static` | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
198 | private getPluginPathPrefix (isTheme: boolean) { | |
199 | return isTheme ? '/themes' : '/plugins' | |
200 | } | |
201 | } |