]>
Commit | Line | Data |
---|---|---|
72f611ca | 1 | import { Observable, of } from 'rxjs' |
2 | import { catchError, 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' |
72f611ca | 13 | import { PluginInfo, PluginsManager } from '@root-helpers/plugins-manager' |
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, | |
72f611ca | 22 | RegisterClientFormFieldOptions, |
3c47fa3b | 23 | RegisterClientSettingsScript, |
72f611ca | 24 | RegisterClientVideoFieldOptions, |
67ed6552 C |
25 | ServerConfigPlugin |
26 | } from '@shared/models' | |
27 | import { environment } from '../../../environments/environment' | |
67ed6552 | 28 | import { RegisterClientHelpers } from '../../../types/register-client-option.model' |
584ac47a | 29 | |
72f611ca | 30 | type FormFields = { |
31 | video: { | |
32 | commonOptions: RegisterClientFormFieldOptions | |
33 | videoFormOptions: RegisterClientVideoFieldOptions | |
34 | }[] | |
35 | } | |
18a6f04c | 36 | |
18a6f04c | 37 | @Injectable() |
93cae479 | 38 | export class PluginService implements ClientHook { |
d75db01f C |
39 | private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins' |
40 | private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins' | |
23bdacf8 | 41 | |
d75db01f C |
42 | translationsObservable: Observable<PluginTranslation> |
43 | ||
437e8e06 K |
44 | customModal: CustomModalComponent |
45 | ||
c713017f C |
46 | private helpers: { [ npmName: string ]: RegisterClientHelpers } = {} |
47 | ||
7294aab0 C |
48 | private formFields: FormFields = { |
49 | video: [] | |
50 | } | |
3c47fa3b | 51 | private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {} |
18a6f04c | 52 | |
72f611ca | 53 | private pluginsManager: PluginsManager |
54 | ||
18a6f04c | 55 | constructor ( |
eb8f702c | 56 | private authService: AuthService, |
74c2dece | 57 | private notifier: Notifier, |
8c7725dc | 58 | private markdownRenderer: MarkdownService, |
23bdacf8 | 59 | private server: ServerService, |
16d54696 | 60 | private zone: NgZone, |
23bdacf8 | 61 | private authHttp: HttpClient, |
d75db01f C |
62 | private restExtractor: RestExtractor, |
63 | @Inject(LOCALE_ID) private localeId: string | |
18a6f04c | 64 | ) { |
d75db01f | 65 | this.loadTranslations() |
72f611ca | 66 | |
67 | this.pluginsManager = new PluginsManager({ | |
68 | peertubeHelpersFactory: this.buildPeerTubeHelpers.bind(this), | |
69 | onFormFields: this.onFormFields.bind(this), | |
70 | onSettingsScripts: this.onSettingsScripts.bind(this) | |
71 | }) | |
18a6f04c C |
72 | } |
73 | ||
74 | initializePlugins () { | |
72f611ca | 75 | this.pluginsManager.loadPluginsList(this.server.getHTMLConfig()) |
584ac47a | 76 | |
72f611ca | 77 | this.pluginsManager.ensurePluginsAreLoaded('common') |
18a6f04c C |
78 | } |
79 | ||
437e8e06 K |
80 | initializeCustomModal (customModal: CustomModalComponent) { |
81 | this.customModal = customModal | |
82 | } | |
83 | ||
72f611ca | 84 | runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> { |
85 | return this.zone.runOutsideAngular(() => { | |
86 | return this.pluginsManager.runHook(hookName, result, params) | |
87 | }) | |
18a6f04c C |
88 | } |
89 | ||
72f611ca | 90 | ensurePluginsAreLoaded (scope: PluginClientScope) { |
91 | return this.pluginsManager.ensurePluginsAreLoaded(scope) | |
ffb321be C |
92 | } |
93 | ||
72f611ca | 94 | reloadLoadedScopes () { |
95 | return this.pluginsManager.reloadLoadedScopes() | |
ffb321be C |
96 | } |
97 | ||
72f611ca | 98 | getPluginsManager () { |
99 | return this.pluginsManager | |
ffb321be C |
100 | } |
101 | ||
72f611ca | 102 | addPlugin (plugin: ServerConfigPlugin, isTheme = false) { |
103 | return this.pluginsManager.addPlugin(plugin, isTheme) | |
18a6f04c C |
104 | } |
105 | ||
72f611ca | 106 | removePlugin (plugin: ServerConfigPlugin) { |
107 | return this.pluginsManager.removePlugin(plugin) | |
18a6f04c C |
108 | } |
109 | ||
23bdacf8 C |
110 | nameToNpmName (name: string, type: PluginType) { |
111 | const prefix = type === PluginType.PLUGIN | |
112 | ? 'peertube-plugin-' | |
113 | : 'peertube-theme-' | |
114 | ||
115 | return prefix + name | |
116 | } | |
117 | ||
c6c0fa6c | 118 | getRegisteredVideoFormFields (type: VideoEditType) { |
7294aab0 C |
119 | return this.formFields.video.filter(f => f.videoFormOptions.type === type) |
120 | } | |
121 | ||
3c47fa3b C |
122 | getRegisteredSettingsScript (npmName: string) { |
123 | return this.settingsScripts[npmName] | |
124 | } | |
125 | ||
c713017f C |
126 | translateBy (npmName: string, toTranslate: string) { |
127 | const helpers = this.helpers[npmName] | |
128 | if (!helpers) { | |
129 | console.error('Unknown helpers to translate %s from %s.', toTranslate, npmName) | |
130 | return toTranslate | |
131 | } | |
132 | ||
133 | return helpers.translate(toTranslate) | |
134 | } | |
135 | ||
72f611ca | 136 | private onFormFields (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) { |
137 | this.formFields.video.push({ | |
138 | commonOptions, | |
139 | videoFormOptions | |
16d54696 | 140 | }) |
18a6f04c C |
141 | } |
142 | ||
72f611ca | 143 | private onSettingsScripts (pluginInfo: PluginInfo, options: RegisterClientSettingsScript) { |
144 | const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType) | |
145 | ||
146 | this.settingsScripts[npmName] = options | |
18a6f04c C |
147 | } |
148 | ||
d75db01f | 149 | private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers { |
f0c5e8b6 | 150 | const { plugin } = pluginInfo |
d75db01f | 151 | const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType) |
f0c5e8b6 C |
152 | |
153 | return { | |
154 | getBaseStaticRoute: () => { | |
72f611ca | 155 | const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme) |
f0c5e8b6 | 156 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static` |
23bdacf8 C |
157 | }, |
158 | ||
9777fe9e | 159 | getBaseRouterRoute: () => { |
72f611ca | 160 | const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme) |
9777fe9e JL |
161 | return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/router` |
162 | }, | |
163 | ||
23bdacf8 | 164 | getSettings: () => { |
d75db01f | 165 | const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings' |
23bdacf8 | 166 | |
ba211e73 | 167 | return this.authHttp.get<PublicServerSetting>(path) |
23bdacf8 | 168 | .pipe( |
ba211e73 | 169 | map(p => p.publicSettings), |
23bdacf8 C |
170 | catchError(res => this.restExtractor.handleError(res)) |
171 | ) | |
172 | .toPromise() | |
d75db01f C |
173 | }, |
174 | ||
faeec106 C |
175 | getServerConfig: () => { |
176 | return this.server.getConfig() | |
177 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
178 | .toPromise() | |
179 | }, | |
180 | ||
eb8f702c RK |
181 | isLoggedIn: () => { |
182 | return this.authService.isLoggedIn() | |
183 | }, | |
184 | ||
4eca42ff C |
185 | getAuthHeader: () => { |
186 | if (!this.authService.isLoggedIn()) return undefined | |
187 | ||
188 | const value = this.authService.getRequestHeaderValue() | |
189 | return { 'Authorization': value } | |
190 | }, | |
191 | ||
f757be65 C |
192 | notifier: { |
193 | info: (text: string, title?: string, timeout?: number) => this.notifier.info(text, title, timeout), | |
194 | error: (text: string, title?: string, timeout?: number) => this.notifier.error(text, title, timeout), | |
195 | success: (text: string, title?: string, timeout?: number) => this.notifier.success(text, title, timeout) | |
196 | }, | |
74c2dece | 197 | |
437e8e06 K |
198 | showModal: (input: { |
199 | title: string, | |
200 | content: string, | |
201 | close?: boolean, | |
202 | cancel?: { value: string, action?: () => void }, | |
203 | confirm?: { value: string, action?: () => void } | |
204 | }) => { | |
205 | this.customModal.show(input) | |
206 | }, | |
207 | ||
8c7725dc K |
208 | markdownRenderer: { |
209 | textMarkdownToHTML: (textMarkdown: string) => { | |
210 | return this.markdownRenderer.textMarkdownToHTML(textMarkdown) | |
211 | }, | |
212 | ||
213 | enhancedMarkdownToHTML: (enhancedMarkdown: string) => { | |
214 | return this.markdownRenderer.enhancedMarkdownToHTML(enhancedMarkdown) | |
215 | } | |
216 | }, | |
217 | ||
d75db01f C |
218 | translate: (value: string) => { |
219 | return this.translationsObservable | |
220 | .pipe(map(allTranslations => allTranslations[npmName])) | |
221 | .pipe(map(translations => peertubeTranslate(value, translations))) | |
222 | .toPromise() | |
f0c5e8b6 C |
223 | } |
224 | } | |
225 | } | |
226 | ||
d75db01f C |
227 | private loadTranslations () { |
228 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
229 | ||
230 | // Default locale, nothing to translate | |
231 | if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay()) | |
232 | ||
233 | this.translationsObservable = this.authHttp | |
234 | .get<PluginTranslation>(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json') | |
235 | .pipe(shareReplay()) | |
236 | } | |
18a6f04c | 237 | } |