]>
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, | |
3c47fa3b | 22 | RegisterClientSettingsScript, |
67ed6552 C |
23 | ServerConfigPlugin |
24 | } from '@shared/models' | |
25 | import { environment } from '../../../environments/environment' | |
67ed6552 | 26 | import { RegisterClientHelpers } from '../../../types/register-client-option.model' |
18a6f04c | 27 | |
18a6f04c | 28 | @Injectable() |
93cae479 | 29 | export class PluginService implements ClientHook { |
d75db01f C |
30 | private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins' |
31 | private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins' | |
23bdacf8 | 32 | |
93cae479 C |
33 | pluginsBuilt = new ReplaySubject<boolean>(1) |
34 | ||
35 | pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = { | |
36 | common: new ReplaySubject<boolean>(1), | |
e8f902c0 | 37 | search: new ReplaySubject<boolean>(1), |
ba7b7e57 | 38 | 'video-watch': new ReplaySubject<boolean>(1), |
f375bb3d | 39 | signup: new ReplaySubject<boolean>(1), |
f9562863 | 40 | login: new ReplaySubject<boolean>(1), |
7294aab0 | 41 | 'video-edit': new ReplaySubject<boolean>(1), |
f9562863 | 42 | embed: new ReplaySubject<boolean>(1) |
93cae479 | 43 | } |
18a6f04c | 44 | |
d75db01f C |
45 | translationsObservable: Observable<PluginTranslation> |
46 | ||
437e8e06 K |
47 | customModal: CustomModalComponent |
48 | ||
18a6f04c | 49 | private plugins: ServerConfigPlugin[] = [] |
c713017f C |
50 | private helpers: { [ npmName: string ]: RegisterClientHelpers } = {} |
51 | ||
f0c5e8b6 | 52 | private scopes: { [ scopeName: string ]: PluginInfo[] } = {} |
c713017f | 53 | |
18a6f04c | 54 | private loadedScripts: { [ script: string ]: boolean } = {} |
93cae479 | 55 | private loadedScopes: PluginClientScope[] = [] |
c9e3eeed | 56 | private loadingScopes: { [id in PluginClientScope]?: boolean } = {} |
18a6f04c | 57 | |
f9562863 | 58 | private hooks: Hooks = {} |
7294aab0 C |
59 | private formFields: FormFields = { |
60 | video: [] | |
61 | } | |
3c47fa3b | 62 | private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {} |
18a6f04c C |
63 | |
64 | constructor ( | |
eb8f702c | 65 | private authService: AuthService, |
74c2dece | 66 | private notifier: Notifier, |
8c7725dc | 67 | private markdownRenderer: MarkdownService, |
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: { | |
f9562863 | 118 | script: `${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 | 181 | runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> { |
f9562863 C |
182 | return this.zone.runOutsideAngular(() => { |
183 | return runHook(this.hooks, hookName, result, params) | |
16d54696 | 184 | }) |
18a6f04c C |
185 | } |
186 | ||
23bdacf8 C |
187 | nameToNpmName (name: string, type: PluginType) { |
188 | const prefix = type === PluginType.PLUGIN | |
189 | ? 'peertube-plugin-' | |
190 | : 'peertube-theme-' | |
191 | ||
192 | return prefix + name | |
193 | } | |
194 | ||
195 | pluginTypeFromNpmName (npmName: string) { | |
196 | return npmName.startsWith('peertube-plugin-') | |
197 | ? PluginType.PLUGIN | |
198 | : PluginType.THEME | |
199 | } | |
200 | ||
c6c0fa6c | 201 | getRegisteredVideoFormFields (type: VideoEditType) { |
7294aab0 C |
202 | return this.formFields.video.filter(f => f.videoFormOptions.type === type) |
203 | } | |
204 | ||
3c47fa3b C |
205 | getRegisteredSettingsScript (npmName: string) { |
206 | return this.settingsScripts[npmName] | |
207 | } | |
208 | ||
c713017f C |
209 | translateBy (npmName: string, toTranslate: string) { |
210 | const helpers = this.helpers[npmName] | |
211 | if (!helpers) { | |
212 | console.error('Unknown helpers to translate %s from %s.', toTranslate, npmName) | |
213 | return toTranslate | |
214 | } | |
215 | ||
216 | return helpers.translate(toTranslate) | |
217 | } | |
218 | ||
f0c5e8b6 | 219 | private loadPlugin (pluginInfo: PluginInfo) { |
16d54696 | 220 | return this.zone.runOutsideAngular(() => { |
c713017f C |
221 | const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType) |
222 | ||
223 | const helpers = this.buildPeerTubeHelpers(pluginInfo) | |
224 | this.helpers[npmName] = helpers | |
225 | ||
7294aab0 C |
226 | return loadPlugin({ |
227 | hooks: this.hooks, | |
228 | formFields: this.formFields, | |
3c47fa3b | 229 | onSettingsScripts: options => this.settingsScripts[npmName] = options, |
7294aab0 | 230 | pluginInfo, |
c713017f | 231 | peertubeHelpersFactory: () => helpers |
7294aab0 | 232 | }) |
16d54696 | 233 | }) |
18a6f04c C |
234 | } |
235 | ||
236 | private buildScopeStruct () { | |
237 | for (const plugin of this.plugins) { | |
ffb321be | 238 | this.addPlugin(plugin) |
18a6f04c C |
239 | } |
240 | } | |
241 | ||
d75db01f | 242 | private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers { |
f0c5e8b6 | 243 | const { plugin } = pluginInfo |
d75db01f | 244 | const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType) |
f0c5e8b6 C |
245 | |
246 | return { | |
247 | getBaseStaticRoute: () => { | |
248 | const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme) | |
249 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static` | |
23bdacf8 C |
250 | }, |
251 | ||
252 | getSettings: () => { | |
d75db01f | 253 | const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings' |
23bdacf8 | 254 | |
ba211e73 | 255 | return this.authHttp.get<PublicServerSetting>(path) |
23bdacf8 | 256 | .pipe( |
ba211e73 | 257 | map(p => p.publicSettings), |
23bdacf8 C |
258 | catchError(res => this.restExtractor.handleError(res)) |
259 | ) | |
260 | .toPromise() | |
d75db01f C |
261 | }, |
262 | ||
faeec106 C |
263 | getServerConfig: () => { |
264 | return this.server.getConfig() | |
265 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
266 | .toPromise() | |
267 | }, | |
268 | ||
eb8f702c RK |
269 | isLoggedIn: () => { |
270 | return this.authService.isLoggedIn() | |
271 | }, | |
272 | ||
f757be65 C |
273 | notifier: { |
274 | info: (text: string, title?: string, timeout?: number) => this.notifier.info(text, title, timeout), | |
275 | error: (text: string, title?: string, timeout?: number) => this.notifier.error(text, title, timeout), | |
276 | success: (text: string, title?: string, timeout?: number) => this.notifier.success(text, title, timeout) | |
277 | }, | |
74c2dece | 278 | |
437e8e06 K |
279 | showModal: (input: { |
280 | title: string, | |
281 | content: string, | |
282 | close?: boolean, | |
283 | cancel?: { value: string, action?: () => void }, | |
284 | confirm?: { value: string, action?: () => void } | |
285 | }) => { | |
286 | this.customModal.show(input) | |
287 | }, | |
288 | ||
8c7725dc K |
289 | markdownRenderer: { |
290 | textMarkdownToHTML: (textMarkdown: string) => { | |
291 | return this.markdownRenderer.textMarkdownToHTML(textMarkdown) | |
292 | }, | |
293 | ||
294 | enhancedMarkdownToHTML: (enhancedMarkdown: string) => { | |
295 | return this.markdownRenderer.enhancedMarkdownToHTML(enhancedMarkdown) | |
296 | } | |
297 | }, | |
298 | ||
d75db01f C |
299 | translate: (value: string) => { |
300 | return this.translationsObservable | |
301 | .pipe(map(allTranslations => allTranslations[npmName])) | |
302 | .pipe(map(translations => peertubeTranslate(value, translations))) | |
303 | .toPromise() | |
f0c5e8b6 C |
304 | } |
305 | } | |
306 | } | |
307 | ||
d75db01f C |
308 | private loadTranslations () { |
309 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
310 | ||
311 | // Default locale, nothing to translate | |
312 | if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay()) | |
313 | ||
314 | this.translationsObservable = this.authHttp | |
315 | .get<PluginTranslation>(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json') | |
316 | .pipe(shareReplay()) | |
317 | } | |
318 | ||
f0c5e8b6 C |
319 | private getPluginPathPrefix (isTheme: boolean) { |
320 | return isTheme ? '/themes' : '/plugins' | |
321 | } | |
18a6f04c | 322 | } |