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