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