]>
Commit | Line | Data |
---|---|---|
72f611ca | 1 | import * as debug from 'debug' |
1378c0d3 | 2 | import { firstValueFrom, ReplaySubject } from 'rxjs' |
72f611ca | 3 | import { first, shareReplay } from 'rxjs/operators' |
4 | import { RegisterClientHelpers } from 'src/types/register-client-option.model' | |
5 | import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' | |
6 | import { | |
7 | ClientHookName, | |
8 | clientHookObject, | |
9 | ClientScript, | |
10 | HTMLServerConfig, | |
11 | PluginClientScope, | |
12 | PluginType, | |
13 | RegisterClientFormFieldOptions, | |
14 | RegisterClientHookOptions, | |
15 | RegisterClientSettingsScript, | |
16 | RegisterClientVideoFieldOptions, | |
17 | ServerConfigPlugin | |
18 | } from '../../../shared/models' | |
19 | import { environment } from '../environments/environment' | |
20 | import { ClientScript as ClientScriptModule } from '../types/client-script.model' | |
21 | ||
22 | interface HookStructValue extends RegisterClientHookOptions { | |
23 | plugin: ServerConfigPlugin | |
24 | clientScript: ClientScript | |
25 | } | |
26 | ||
27 | type Hooks = { [ name: string ]: HookStructValue[] } | |
28 | ||
29 | type PluginInfo = { | |
30 | plugin: ServerConfigPlugin | |
31 | clientScript: ClientScript | |
32 | pluginType: PluginType | |
33 | isTheme: boolean | |
34 | } | |
35 | ||
36 | type PeertubeHelpersFactory = (pluginInfo: PluginInfo) => RegisterClientHelpers | |
37 | type OnFormFields = (options: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void | |
38 | type OnSettingsScripts = (pluginInfo: PluginInfo, options: RegisterClientSettingsScript) => void | |
39 | ||
40 | const logger = debug('peertube:plugins') | |
41 | ||
42 | class PluginsManager { | |
43 | private hooks: Hooks = {} | |
44 | ||
45 | private scopes: { [ scopeName: string ]: PluginInfo[] } = {} | |
46 | ||
47 | private loadedScripts: { [ script: string ]: boolean } = {} | |
48 | private loadedScopes: PluginClientScope[] = [] | |
49 | private loadingScopes: { [id in PluginClientScope]?: boolean } = {} | |
50 | ||
51 | private pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = { | |
52 | common: new ReplaySubject<boolean>(1), | |
53 | 'admin-plugin': new ReplaySubject<boolean>(1), | |
54 | search: new ReplaySubject<boolean>(1), | |
55 | 'video-watch': new ReplaySubject<boolean>(1), | |
56 | signup: new ReplaySubject<boolean>(1), | |
57 | login: new ReplaySubject<boolean>(1), | |
58 | 'video-edit': new ReplaySubject<boolean>(1), | |
59 | embed: new ReplaySubject<boolean>(1) | |
60 | } | |
61 | ||
62 | private readonly peertubeHelpersFactory: PeertubeHelpersFactory | |
63 | private readonly onFormFields: OnFormFields | |
64 | private readonly onSettingsScripts: OnSettingsScripts | |
65 | ||
66 | constructor (options: { | |
67 | peertubeHelpersFactory: PeertubeHelpersFactory | |
68 | onFormFields?: OnFormFields | |
69 | onSettingsScripts?: OnSettingsScripts | |
70 | }) { | |
71 | this.peertubeHelpersFactory = options.peertubeHelpersFactory | |
72 | this.onFormFields = options.onFormFields | |
73 | this.onSettingsScripts = options.onSettingsScripts | |
74 | } | |
75 | ||
76 | static getPluginPathPrefix (isTheme: boolean) { | |
77 | return isTheme ? '/themes' : '/plugins' | |
78 | } | |
79 | ||
80 | loadPluginsList (config: HTMLServerConfig) { | |
81 | for (const plugin of config.plugin.registered) { | |
82 | this.addPlugin(plugin) | |
83 | } | |
84 | } | |
85 | ||
86 | async runHook<T> (hookName: ClientHookName, result?: T, params?: any) { | |
87 | if (!this.hooks[hookName]) return result | |
88 | ||
89 | const hookType = getHookType(hookName) | |
90 | ||
91 | for (const hook of this.hooks[hookName]) { | |
92 | console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name) | |
93 | ||
94 | result = await internalRunHook(hook.handler, hookType, result, params, err => { | |
95 | console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err) | |
96 | }) | |
97 | } | |
98 | ||
99 | return result | |
100 | } | |
101 | ||
102 | ensurePluginsAreLoaded (scope: PluginClientScope) { | |
103 | this.loadPluginsByScope(scope) | |
104 | ||
1378c0d3 | 105 | const obs = this.pluginsLoaded[scope].asObservable() |
72f611ca | 106 | .pipe(first(), shareReplay()) |
1378c0d3 C |
107 | |
108 | return firstValueFrom(obs) | |
72f611ca | 109 | } |
110 | ||
111 | async reloadLoadedScopes () { | |
112 | for (const scope of this.loadedScopes) { | |
113 | await this.loadPluginsByScope(scope, true) | |
114 | } | |
115 | } | |
116 | ||
117 | addPlugin (plugin: ServerConfigPlugin, isTheme = false) { | |
118 | const pathPrefix = PluginsManager.getPluginPathPrefix(isTheme) | |
119 | ||
120 | for (const key of Object.keys(plugin.clientScripts)) { | |
121 | const clientScript = plugin.clientScripts[key] | |
122 | ||
123 | for (const scope of clientScript.scopes) { | |
124 | if (!this.scopes[scope]) this.scopes[scope] = [] | |
125 | ||
126 | this.scopes[scope].push({ | |
127 | plugin, | |
128 | clientScript: { | |
129 | script: `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`, | |
130 | scopes: clientScript.scopes | |
131 | }, | |
132 | pluginType: isTheme ? PluginType.THEME : PluginType.PLUGIN, | |
133 | isTheme | |
134 | }) | |
135 | ||
136 | this.loadedScripts[clientScript.script] = false | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | removePlugin (plugin: ServerConfigPlugin) { | |
142 | for (const key of Object.keys(this.scopes)) { | |
143 | this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name) | |
144 | } | |
145 | } | |
146 | ||
147 | async loadPluginsByScope (scope: PluginClientScope, isReload = false) { | |
148 | if (this.loadingScopes[scope]) return | |
149 | if (!isReload && this.loadedScopes.includes(scope)) return | |
150 | ||
151 | this.loadingScopes[scope] = true | |
152 | ||
153 | logger('Loading scope %s', scope) | |
154 | ||
155 | try { | |
156 | if (!isReload) this.loadedScopes.push(scope) | |
157 | ||
9df52d66 | 158 | const toLoad = this.scopes[scope] |
72f611ca | 159 | if (!Array.isArray(toLoad)) { |
160 | this.loadingScopes[scope] = false | |
161 | this.pluginsLoaded[scope].next(true) | |
162 | ||
163 | logger('Nothing to load for scope %s', scope) | |
164 | return | |
165 | } | |
166 | ||
167 | const promises: Promise<any>[] = [] | |
168 | for (const pluginInfo of toLoad) { | |
169 | const clientScript = pluginInfo.clientScript | |
170 | ||
9df52d66 | 171 | if (this.loadedScripts[clientScript.script]) continue |
72f611ca | 172 | |
173 | promises.push(this.loadPlugin(pluginInfo)) | |
174 | ||
9df52d66 | 175 | this.loadedScripts[clientScript.script] = true |
72f611ca | 176 | } |
177 | ||
178 | await Promise.all(promises) | |
179 | ||
180 | this.pluginsLoaded[scope].next(true) | |
181 | this.loadingScopes[scope] = false | |
182 | ||
183 | logger('Scope %s loaded', scope) | |
184 | } catch (err) { | |
185 | console.error('Cannot load plugins by scope %s.', scope, err) | |
186 | } | |
187 | } | |
188 | ||
189 | private loadPlugin (pluginInfo: PluginInfo) { | |
190 | const { plugin, clientScript } = pluginInfo | |
191 | ||
192 | const registerHook = (options: RegisterClientHookOptions) => { | |
193 | if (clientHookObject[options.target] !== true) { | |
194 | console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name) | |
195 | return | |
196 | } | |
197 | ||
198 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | |
199 | ||
200 | this.hooks[options.target].push({ | |
201 | plugin, | |
202 | clientScript, | |
203 | target: options.target, | |
204 | handler: options.handler, | |
205 | priority: options.priority || 0 | |
206 | }) | |
207 | } | |
208 | ||
209 | const registerVideoField = (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => { | |
210 | if (!this.onFormFields) { | |
211 | throw new Error('Video field registration is not supported') | |
212 | } | |
213 | ||
214 | return this.onFormFields(commonOptions, videoFormOptions) | |
215 | } | |
216 | ||
217 | const registerSettingsScript = (options: RegisterClientSettingsScript) => { | |
218 | if (!this.onSettingsScripts) { | |
219 | throw new Error('Registering settings script is not supported') | |
220 | } | |
221 | ||
222 | return this.onSettingsScripts(pluginInfo, options) | |
223 | } | |
224 | ||
225 | const peertubeHelpers = this.peertubeHelpersFactory(pluginInfo) | |
226 | ||
227 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) | |
228 | ||
229 | const absURL = (environment.apiUrl || window.location.origin) + clientScript.script | |
230 | return import(/* webpackIgnore: true */ absURL) | |
231 | .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers })) | |
232 | .then(() => this.sortHooksByPriority()) | |
233 | .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) | |
234 | } | |
235 | ||
236 | private sortHooksByPriority () { | |
237 | for (const hookName of Object.keys(this.hooks)) { | |
238 | this.hooks[hookName].sort((a, b) => { | |
239 | return b.priority - a.priority | |
240 | }) | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | export { | |
246 | PluginsManager, | |
247 | ||
248 | PluginInfo, | |
249 | PeertubeHelpersFactory, | |
250 | OnFormFields, | |
251 | OnSettingsScripts | |
252 | } |