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