]>
Commit | Line | Data |
---|---|---|
d75db01f | 1 | import { Inject, Injectable, LOCALE_ID, NgZone } from '@angular/core' |
18a6f04c | 2 | import { Router } from '@angular/router' |
d75db01f | 3 | import { getCompleteLocale, isDefaultLocale, peertubeTranslate, ServerConfigPlugin } from '@shared/models' |
18a6f04c C |
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' |
d75db01f | 8 | import { Observable, of, ReplaySubject } from 'rxjs' |
23bdacf8 | 9 | import { catchError, first, map, 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' |
23bdacf8 | 14 | import { HttpClient } from '@angular/common/http' |
891bc2ff C |
15 | import { AuthService } from '@app/core/auth' |
16 | import { Notifier } from '@app/core/notification' | |
23bdacf8 | 17 | import { RestExtractor } from '@app/shared/rest' |
8c7725dc | 18 | import { MarkdownService } from '@app/shared/renderer' |
23bdacf8 | 19 | import { PluginType } from '@shared/models/plugins/plugin.type' |
ba211e73 | 20 | import { PublicServerSetting } from '@shared/models/plugins/public-server.setting' |
d75db01f C |
21 | import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' |
22 | import { RegisterClientHelpers } from '../../../types/register-client-option.model' | |
23 | import { PluginTranslation } from '@shared/models/plugins/plugin-translation.model' | |
0c503f5c | 24 | import { importModule } from '@app/shared/misc/utils' |
437e8e06 | 25 | import { CustomModalComponent } from '@app/modal/custom-modal.component' |
18a6f04c | 26 | |
9ae88819 | 27 | interface HookStructValue extends RegisterClientHookOptions { |
18a6f04c C |
28 | plugin: ServerConfigPlugin |
29 | clientScript: ClientScript | |
30 | } | |
31 | ||
f0c5e8b6 C |
32 | type PluginInfo = { |
33 | plugin: ServerConfigPlugin | |
34 | clientScript: ClientScript | |
23bdacf8 | 35 | pluginType: PluginType |
f0c5e8b6 C |
36 | isTheme: boolean |
37 | } | |
38 | ||
18a6f04c | 39 | @Injectable() |
93cae479 | 40 | export class PluginService implements ClientHook { |
d75db01f C |
41 | private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins' |
42 | private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins' | |
23bdacf8 | 43 | |
93cae479 C |
44 | pluginsBuilt = new ReplaySubject<boolean>(1) |
45 | ||
46 | pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = { | |
47 | common: new ReplaySubject<boolean>(1), | |
e8f902c0 | 48 | search: new ReplaySubject<boolean>(1), |
ba7b7e57 | 49 | 'video-watch': new ReplaySubject<boolean>(1), |
f375bb3d C |
50 | signup: new ReplaySubject<boolean>(1), |
51 | login: new ReplaySubject<boolean>(1) | |
93cae479 | 52 | } |
18a6f04c | 53 | |
d75db01f C |
54 | translationsObservable: Observable<PluginTranslation> |
55 | ||
437e8e06 K |
56 | customModal: CustomModalComponent |
57 | ||
18a6f04c | 58 | private plugins: ServerConfigPlugin[] = [] |
f0c5e8b6 | 59 | private scopes: { [ scopeName: string ]: PluginInfo[] } = {} |
18a6f04c | 60 | private loadedScripts: { [ script: string ]: boolean } = {} |
93cae479 | 61 | private loadedScopes: PluginClientScope[] = [] |
c9e3eeed | 62 | private loadingScopes: { [id in PluginClientScope]?: boolean } = {} |
18a6f04c C |
63 | |
64 | private hooks: { [ name: string ]: HookStructValue[] } = {} | |
65 | ||
66 | constructor ( | |
67 | private router: Router, | |
eb8f702c | 68 | private authService: AuthService, |
74c2dece | 69 | private notifier: Notifier, |
8c7725dc | 70 | private markdownRenderer: MarkdownService, |
23bdacf8 | 71 | private server: ServerService, |
16d54696 | 72 | private zone: NgZone, |
23bdacf8 | 73 | private authHttp: HttpClient, |
d75db01f C |
74 | private restExtractor: RestExtractor, |
75 | @Inject(LOCALE_ID) private localeId: string | |
18a6f04c | 76 | ) { |
d75db01f | 77 | this.loadTranslations() |
18a6f04c C |
78 | } |
79 | ||
80 | initializePlugins () { | |
ba430d75 C |
81 | this.server.getConfig() |
82 | .subscribe(config => { | |
83 | this.plugins = config.plugin.registered | |
18a6f04c C |
84 | |
85 | this.buildScopeStruct() | |
86 | ||
93cae479 | 87 | this.pluginsBuilt.next(true) |
18a6f04c C |
88 | }) |
89 | } | |
90 | ||
437e8e06 K |
91 | initializeCustomModal (customModal: CustomModalComponent) { |
92 | this.customModal = customModal | |
93 | } | |
94 | ||
93cae479 C |
95 | ensurePluginsAreBuilt () { |
96 | return this.pluginsBuilt.asObservable() | |
97 | .pipe(first(), shareReplay()) | |
98 | .toPromise() | |
99 | } | |
100 | ||
101 | ensurePluginsAreLoaded (scope: PluginClientScope) { | |
c9e3eeed C |
102 | this.loadPluginsByScope(scope) |
103 | ||
93cae479 | 104 | return this.pluginsLoaded[scope].asObservable() |
ffb321be | 105 | .pipe(first(), shareReplay()) |
18a6f04c C |
106 | .toPromise() |
107 | } | |
108 | ||
b5f919ac | 109 | addPlugin (plugin: ServerConfigPlugin, isTheme = false) { |
f0c5e8b6 | 110 | const pathPrefix = this.getPluginPathPrefix(isTheme) |
b5f919ac | 111 | |
ffb321be C |
112 | for (const key of Object.keys(plugin.clientScripts)) { |
113 | const clientScript = plugin.clientScripts[key] | |
114 | ||
115 | for (const scope of clientScript.scopes) { | |
116 | if (!this.scopes[scope]) this.scopes[scope] = [] | |
117 | ||
118 | this.scopes[scope].push({ | |
119 | plugin, | |
120 | clientScript: { | |
b5f919ac | 121 | script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`, |
ffb321be | 122 | scopes: clientScript.scopes |
f0c5e8b6 | 123 | }, |
23bdacf8 | 124 | pluginType: isTheme ? PluginType.THEME : PluginType.PLUGIN, |
f0c5e8b6 | 125 | isTheme |
ffb321be C |
126 | }) |
127 | ||
128 | this.loadedScripts[clientScript.script] = false | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | removePlugin (plugin: ServerConfigPlugin) { | |
134 | for (const key of Object.keys(this.scopes)) { | |
135 | this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name) | |
136 | } | |
137 | } | |
138 | ||
139 | async reloadLoadedScopes () { | |
140 | for (const scope of this.loadedScopes) { | |
f0c5e8b6 | 141 | await this.loadPluginsByScope(scope, true) |
ffb321be C |
142 | } |
143 | } | |
144 | ||
93cae479 | 145 | async loadPluginsByScope (scope: PluginClientScope, isReload = false) { |
c9e3eeed C |
146 | if (this.loadingScopes[scope]) return |
147 | if (!isReload && this.loadedScopes.includes(scope)) return | |
148 | ||
149 | this.loadingScopes[scope] = true | |
150 | ||
18a6f04c | 151 | try { |
93cae479 | 152 | await this.ensurePluginsAreBuilt() |
18a6f04c | 153 | |
f0c5e8b6 | 154 | if (!isReload) this.loadedScopes.push(scope) |
ffb321be | 155 | |
18a6f04c | 156 | const toLoad = this.scopes[ scope ] |
e8f902c0 | 157 | if (!Array.isArray(toLoad)) { |
c9e3eeed | 158 | this.loadingScopes[scope] = false |
e8f902c0 C |
159 | this.pluginsLoaded[scope].next(true) |
160 | ||
161 | return | |
162 | } | |
18a6f04c C |
163 | |
164 | const promises: Promise<any>[] = [] | |
f0c5e8b6 C |
165 | for (const pluginInfo of toLoad) { |
166 | const clientScript = pluginInfo.clientScript | |
167 | ||
18a6f04c C |
168 | if (this.loadedScripts[ clientScript.script ]) continue |
169 | ||
f0c5e8b6 | 170 | promises.push(this.loadPlugin(pluginInfo)) |
18a6f04c C |
171 | |
172 | this.loadedScripts[ clientScript.script ] = true | |
173 | } | |
174 | ||
ffb321be | 175 | await Promise.all(promises) |
93cae479 C |
176 | |
177 | this.pluginsLoaded[scope].next(true) | |
c9e3eeed | 178 | this.loadingScopes[scope] = false |
18a6f04c C |
179 | } catch (err) { |
180 | console.error('Cannot load plugins by scope %s.', scope, err) | |
181 | } | |
182 | } | |
183 | ||
16d54696 C |
184 | runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> { |
185 | return this.zone.runOutsideAngular(async () => { | |
186 | if (!this.hooks[ hookName ]) return result | |
f0c5e8b6 | 187 | |
16d54696 | 188 | const hookType = getHookType(hookName) |
18a6f04c | 189 | |
16d54696 C |
190 | for (const hook of this.hooks[ hookName ]) { |
191 | console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name) | |
93cae479 | 192 | |
16d54696 C |
193 | result = await internalRunHook(hook.handler, hookType, result, params, err => { |
194 | console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err) | |
195 | }) | |
196 | } | |
18a6f04c | 197 | |
16d54696 C |
198 | return result |
199 | }) | |
18a6f04c C |
200 | } |
201 | ||
23bdacf8 C |
202 | nameToNpmName (name: string, type: PluginType) { |
203 | const prefix = type === PluginType.PLUGIN | |
204 | ? 'peertube-plugin-' | |
205 | : 'peertube-theme-' | |
206 | ||
207 | return prefix + name | |
208 | } | |
209 | ||
210 | pluginTypeFromNpmName (npmName: string) { | |
211 | return npmName.startsWith('peertube-plugin-') | |
212 | ? PluginType.PLUGIN | |
213 | : PluginType.THEME | |
214 | } | |
215 | ||
f0c5e8b6 C |
216 | private loadPlugin (pluginInfo: PluginInfo) { |
217 | const { plugin, clientScript } = pluginInfo | |
218 | ||
9ae88819 | 219 | const registerHook = (options: RegisterClientHookOptions) => { |
7663e55a C |
220 | if (clientHookObject[options.target] !== true) { |
221 | console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name) | |
222 | return | |
223 | } | |
224 | ||
18a6f04c C |
225 | if (!this.hooks[options.target]) this.hooks[options.target] = [] |
226 | ||
227 | this.hooks[options.target].push({ | |
228 | plugin, | |
229 | clientScript, | |
230 | target: options.target, | |
231 | handler: options.handler, | |
232 | priority: options.priority || 0 | |
233 | }) | |
234 | } | |
235 | ||
f0c5e8b6 C |
236 | const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo) |
237 | ||
18a6f04c C |
238 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) |
239 | ||
16d54696 | 240 | return this.zone.runOutsideAngular(() => { |
0c503f5c | 241 | return importModule(clientScript.script) |
16d54696 C |
242 | .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers })) |
243 | .then(() => this.sortHooksByPriority()) | |
244 | .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) | |
245 | }) | |
18a6f04c C |
246 | } |
247 | ||
248 | private buildScopeStruct () { | |
249 | for (const plugin of this.plugins) { | |
ffb321be | 250 | this.addPlugin(plugin) |
18a6f04c C |
251 | } |
252 | } | |
253 | ||
254 | private sortHooksByPriority () { | |
255 | for (const hookName of Object.keys(this.hooks)) { | |
256 | this.hooks[hookName].sort((a, b) => { | |
257 | return b.priority - a.priority | |
258 | }) | |
259 | } | |
260 | } | |
f0c5e8b6 | 261 | |
d75db01f | 262 | private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers { |
f0c5e8b6 | 263 | const { plugin } = pluginInfo |
d75db01f | 264 | const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType) |
f0c5e8b6 C |
265 | |
266 | return { | |
267 | getBaseStaticRoute: () => { | |
268 | const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme) | |
269 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static` | |
23bdacf8 C |
270 | }, |
271 | ||
272 | getSettings: () => { | |
d75db01f | 273 | const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings' |
23bdacf8 | 274 | |
ba211e73 | 275 | return this.authHttp.get<PublicServerSetting>(path) |
23bdacf8 | 276 | .pipe( |
ba211e73 | 277 | map(p => p.publicSettings), |
23bdacf8 C |
278 | catchError(res => this.restExtractor.handleError(res)) |
279 | ) | |
280 | .toPromise() | |
d75db01f C |
281 | }, |
282 | ||
eb8f702c RK |
283 | isLoggedIn: () => { |
284 | return this.authService.isLoggedIn() | |
285 | }, | |
286 | ||
f757be65 C |
287 | notifier: { |
288 | info: (text: string, title?: string, timeout?: number) => this.notifier.info(text, title, timeout), | |
289 | error: (text: string, title?: string, timeout?: number) => this.notifier.error(text, title, timeout), | |
290 | success: (text: string, title?: string, timeout?: number) => this.notifier.success(text, title, timeout) | |
291 | }, | |
74c2dece | 292 | |
437e8e06 K |
293 | showModal: (input: { |
294 | title: string, | |
295 | content: string, | |
296 | close?: boolean, | |
297 | cancel?: { value: string, action?: () => void }, | |
298 | confirm?: { value: string, action?: () => void } | |
299 | }) => { | |
300 | this.customModal.show(input) | |
301 | }, | |
302 | ||
8c7725dc K |
303 | markdownRenderer: { |
304 | textMarkdownToHTML: (textMarkdown: string) => { | |
305 | return this.markdownRenderer.textMarkdownToHTML(textMarkdown) | |
306 | }, | |
307 | ||
308 | enhancedMarkdownToHTML: (enhancedMarkdown: string) => { | |
309 | return this.markdownRenderer.enhancedMarkdownToHTML(enhancedMarkdown) | |
310 | } | |
311 | }, | |
312 | ||
d75db01f C |
313 | translate: (value: string) => { |
314 | return this.translationsObservable | |
315 | .pipe(map(allTranslations => allTranslations[npmName])) | |
316 | .pipe(map(translations => peertubeTranslate(value, translations))) | |
317 | .toPromise() | |
f0c5e8b6 C |
318 | } |
319 | } | |
320 | } | |
321 | ||
d75db01f C |
322 | private loadTranslations () { |
323 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
324 | ||
325 | // Default locale, nothing to translate | |
326 | if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay()) | |
327 | ||
328 | this.translationsObservable = this.authHttp | |
329 | .get<PluginTranslation>(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json') | |
330 | .pipe(shareReplay()) | |
331 | } | |
332 | ||
f0c5e8b6 C |
333 | private getPluginPathPrefix (isTheme: boolean) { |
334 | return isTheme ? '/themes' : '/plugins' | |
335 | } | |
18a6f04c | 336 | } |