]>
Commit | Line | Data |
---|---|---|
1 | import { firstValueFrom, Observable, of } from 'rxjs' | |
2 | import { catchError, 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 { PluginInfo, PluginsManager } from '@root-helpers/plugins-manager' | |
14 | import { getKeys } from '@shared/core-utils' | |
15 | import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n' | |
16 | import { | |
17 | ClientHook, | |
18 | ClientHookName, | |
19 | PluginClientScope, | |
20 | PluginTranslation, | |
21 | PluginType, | |
22 | PublicServerSetting, | |
23 | RegisterClientFormFieldOptions, | |
24 | RegisterClientRouteOptions, | |
25 | RegisterClientSettingsScriptOptions, | |
26 | RegisterClientVideoFieldOptions, | |
27 | ServerConfigPlugin | |
28 | } from '@shared/models' | |
29 | import { environment } from '../../../environments/environment' | |
30 | import { RegisterClientHelpers } from '../../../types/register-client-option.model' | |
31 | ||
32 | type FormFields = { | |
33 | video: { | |
34 | pluginInfo: PluginInfo | |
35 | commonOptions: RegisterClientFormFieldOptions | |
36 | videoFormOptions: RegisterClientVideoFieldOptions | |
37 | }[] | |
38 | } | |
39 | ||
40 | @Injectable() | |
41 | export class PluginService implements ClientHook { | |
42 | private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins' | |
43 | private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins' | |
44 | ||
45 | translationsObservable: Observable<PluginTranslation> | |
46 | ||
47 | customModal: CustomModalComponent | |
48 | ||
49 | private formFields: FormFields = { | |
50 | video: [] | |
51 | } | |
52 | private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScriptOptions } = {} | |
53 | private clientRoutes: { [ route: string ]: RegisterClientRouteOptions } = {} | |
54 | ||
55 | private pluginsManager: PluginsManager | |
56 | ||
57 | constructor ( | |
58 | private authService: AuthService, | |
59 | private notifier: Notifier, | |
60 | private markdownRenderer: MarkdownService, | |
61 | private server: ServerService, | |
62 | private zone: NgZone, | |
63 | private authHttp: HttpClient, | |
64 | private restExtractor: RestExtractor, | |
65 | @Inject(LOCALE_ID) private localeId: string | |
66 | ) { | |
67 | this.loadTranslations() | |
68 | ||
69 | this.pluginsManager = new PluginsManager({ | |
70 | peertubeHelpersFactory: this.buildPeerTubeHelpers.bind(this), | |
71 | onFormFields: this.onFormFields.bind(this), | |
72 | onSettingsScripts: this.onSettingsScripts.bind(this), | |
73 | onClientRoute: this.onClientRoute.bind(this) | |
74 | }) | |
75 | } | |
76 | ||
77 | initializePlugins () { | |
78 | this.pluginsManager.loadPluginsList(this.server.getHTMLConfig()) | |
79 | ||
80 | this.pluginsManager.ensurePluginsAreLoaded('common') | |
81 | } | |
82 | ||
83 | initializeCustomModal (customModal: CustomModalComponent) { | |
84 | this.customModal = customModal | |
85 | } | |
86 | ||
87 | runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> { | |
88 | return this.zone.runOutsideAngular(() => { | |
89 | return this.pluginsManager.runHook(hookName, result, params) | |
90 | }) | |
91 | } | |
92 | ||
93 | ensurePluginsAreLoaded (scope: PluginClientScope) { | |
94 | return this.pluginsManager.ensurePluginsAreLoaded(scope) | |
95 | } | |
96 | ||
97 | reloadLoadedScopes () { | |
98 | return this.pluginsManager.reloadLoadedScopes() | |
99 | } | |
100 | ||
101 | getPluginsManager () { | |
102 | return this.pluginsManager | |
103 | } | |
104 | ||
105 | addPlugin (plugin: ServerConfigPlugin, isTheme = false) { | |
106 | return this.pluginsManager.addPlugin(plugin, isTheme) | |
107 | } | |
108 | ||
109 | removePlugin (plugin: ServerConfigPlugin) { | |
110 | return this.pluginsManager.removePlugin(plugin) | |
111 | } | |
112 | ||
113 | nameToNpmName (name: string, type: PluginType) { | |
114 | const prefix = type === PluginType.PLUGIN | |
115 | ? 'peertube-plugin-' | |
116 | : 'peertube-theme-' | |
117 | ||
118 | return prefix + name | |
119 | } | |
120 | ||
121 | getRegisteredVideoFormFields (type: VideoEditType) { | |
122 | return this.formFields.video.filter(f => f.videoFormOptions.type === type) | |
123 | } | |
124 | ||
125 | getRegisteredSettingsScript (npmName: string) { | |
126 | return this.settingsScripts[npmName] | |
127 | } | |
128 | ||
129 | getRegisteredClientRoute (route: string) { | |
130 | return this.clientRoutes[route] | |
131 | } | |
132 | ||
133 | getAllRegisteredClientRoutes () { | |
134 | return Object.keys(this.clientRoutes) | |
135 | } | |
136 | ||
137 | async translateSetting (npmName: string, setting: RegisterClientFormFieldOptions) { | |
138 | for (const key of getKeys(setting, [ 'label', 'html', 'descriptionHTML' ])) { | |
139 | if (setting[key]) setting[key] = await this.translateBy(npmName, setting[key]) | |
140 | } | |
141 | ||
142 | if (Array.isArray(setting.options)) { | |
143 | const newOptions = [] | |
144 | ||
145 | for (const o of setting.options) { | |
146 | newOptions.push({ | |
147 | value: o.value, | |
148 | label: await this.translateBy(npmName, o.label) | |
149 | }) | |
150 | } | |
151 | ||
152 | setting.options = newOptions | |
153 | } | |
154 | } | |
155 | ||
156 | translateBy (npmName: string, toTranslate: string) { | |
157 | const obs = this.translationsObservable | |
158 | .pipe( | |
159 | map(allTranslations => allTranslations[npmName]), | |
160 | map(translations => peertubeTranslate(toTranslate, translations)) | |
161 | ) | |
162 | ||
163 | return firstValueFrom(obs) | |
164 | } | |
165 | ||
166 | private onFormFields ( | |
167 | pluginInfo: PluginInfo, | |
168 | commonOptions: RegisterClientFormFieldOptions, | |
169 | videoFormOptions: RegisterClientVideoFieldOptions | |
170 | ) { | |
171 | this.formFields.video.push({ | |
172 | pluginInfo, | |
173 | commonOptions, | |
174 | videoFormOptions | |
175 | }) | |
176 | } | |
177 | ||
178 | private onSettingsScripts (pluginInfo: PluginInfo, options: RegisterClientSettingsScriptOptions) { | |
179 | this.settingsScripts[pluginInfo.plugin.npmName] = options | |
180 | } | |
181 | ||
182 | private onClientRoute (options: RegisterClientRouteOptions) { | |
183 | const route = options.route.startsWith('/') | |
184 | ? options.route | |
185 | : `/${options.route}` | |
186 | ||
187 | this.clientRoutes[route] = options | |
188 | } | |
189 | ||
190 | private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers { | |
191 | const { plugin } = pluginInfo | |
192 | const npmName = pluginInfo.plugin.npmName | |
193 | ||
194 | return { | |
195 | getBaseStaticRoute: () => { | |
196 | const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme) | |
197 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static` | |
198 | }, | |
199 | ||
200 | getBaseRouterRoute: () => { | |
201 | const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme) | |
202 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/router` | |
203 | }, | |
204 | ||
205 | getBaseWebSocketRoute: () => { | |
206 | const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme) | |
207 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/ws` | |
208 | }, | |
209 | ||
210 | getBasePluginClientPath: () => { | |
211 | return '/p' | |
212 | }, | |
213 | ||
214 | getSettings: () => { | |
215 | const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings' | |
216 | ||
217 | const obs = this.authHttp.get<PublicServerSetting>(path) | |
218 | .pipe( | |
219 | map(p => p.publicSettings), | |
220 | catchError(res => this.restExtractor.handleError(res)) | |
221 | ) | |
222 | ||
223 | return firstValueFrom(obs) | |
224 | }, | |
225 | ||
226 | getServerConfig: () => { | |
227 | const obs = this.server.getConfig() | |
228 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
229 | ||
230 | return firstValueFrom(obs) | |
231 | }, | |
232 | ||
233 | isLoggedIn: () => { | |
234 | return this.authService.isLoggedIn() | |
235 | }, | |
236 | ||
237 | getAuthHeader: () => { | |
238 | if (!this.authService.isLoggedIn()) return undefined | |
239 | ||
240 | const value = this.authService.getRequestHeaderValue() | |
241 | return { Authorization: value } | |
242 | }, | |
243 | ||
244 | notifier: { | |
245 | info: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.info(text, title, timeout)), | |
246 | error: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.error(text, title, timeout)), | |
247 | success: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.success(text, title, timeout)) | |
248 | }, | |
249 | ||
250 | showModal: (input: { | |
251 | title: string | |
252 | content: string | |
253 | close?: boolean | |
254 | cancel?: { value: string, action?: () => void } | |
255 | confirm?: { value: string, action?: () => void } | |
256 | }) => { | |
257 | this.zone.run(() => this.customModal.show(input)) | |
258 | }, | |
259 | ||
260 | markdownRenderer: { | |
261 | textMarkdownToHTML: (textMarkdown: string) => { | |
262 | return this.markdownRenderer.textMarkdownToHTML({ markdown: textMarkdown }) | |
263 | }, | |
264 | ||
265 | enhancedMarkdownToHTML: (enhancedMarkdown: string) => { | |
266 | return this.markdownRenderer.enhancedMarkdownToHTML({ markdown: enhancedMarkdown }) | |
267 | } | |
268 | }, | |
269 | ||
270 | translate: (value: string) => { | |
271 | return this.translateBy(npmName, value) | |
272 | } | |
273 | } | |
274 | } | |
275 | ||
276 | private loadTranslations () { | |
277 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
278 | ||
279 | // Default locale, nothing to translate | |
280 | if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay()) | |
281 | ||
282 | this.translationsObservable = this.authHttp | |
283 | .get<PluginTranslation>(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json') | |
284 | .pipe(shareReplay()) | |
285 | } | |
286 | } |