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