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