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