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