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