1 import { 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 { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
22 RegisterClientFormFieldOptions,
23 RegisterClientSettingsScript,
24 RegisterClientVideoFieldOptions,
26 } from '@shared/models'
27 import { environment } from '../../../environments/environment'
28 import { RegisterClientHelpers } from '../../../types/register-client-option.model'
32 commonOptions: RegisterClientFormFieldOptions
33 videoFormOptions: RegisterClientVideoFieldOptions
38 export class PluginService implements ClientHook {
39 private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins'
40 private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins'
42 translationsObservable: Observable<PluginTranslation>
44 customModal: CustomModalComponent
46 private helpers: { [ npmName: string ]: RegisterClientHelpers } = {}
48 private formFields: FormFields = {
51 private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {}
53 private pluginsManager: PluginsManager
56 private authService: AuthService,
57 private notifier: Notifier,
58 private markdownRenderer: MarkdownService,
59 private server: ServerService,
61 private authHttp: HttpClient,
62 private restExtractor: RestExtractor,
63 @Inject(LOCALE_ID) private localeId: string
65 this.loadTranslations()
67 this.pluginsManager = new PluginsManager({
68 peertubeHelpersFactory: this.buildPeerTubeHelpers.bind(this),
69 onFormFields: this.onFormFields.bind(this),
70 onSettingsScripts: this.onSettingsScripts.bind(this)
74 initializePlugins () {
75 this.pluginsManager.loadPluginsList(this.server.getHTMLConfig())
77 this.pluginsManager.ensurePluginsAreLoaded('common')
80 initializeCustomModal (customModal: CustomModalComponent) {
81 this.customModal = customModal
84 runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
85 return this.zone.runOutsideAngular(() => {
86 return this.pluginsManager.runHook(hookName, result, params)
90 ensurePluginsAreLoaded (scope: PluginClientScope) {
91 return this.pluginsManager.ensurePluginsAreLoaded(scope)
94 reloadLoadedScopes () {
95 return this.pluginsManager.reloadLoadedScopes()
98 getPluginsManager () {
99 return this.pluginsManager
102 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
103 return this.pluginsManager.addPlugin(plugin, isTheme)
106 removePlugin (plugin: ServerConfigPlugin) {
107 return this.pluginsManager.removePlugin(plugin)
110 nameToNpmName (name: string, type: PluginType) {
111 const prefix = type === PluginType.PLUGIN
118 getRegisteredVideoFormFields (type: VideoEditType) {
119 return this.formFields.video.filter(f => f.videoFormOptions.type === type)
122 getRegisteredSettingsScript (npmName: string) {
123 return this.settingsScripts[npmName]
126 translateBy (npmName: string, toTranslate: string) {
127 const helpers = this.helpers[npmName]
129 console.error('Unknown helpers to translate %s from %s.', toTranslate, npmName)
133 return helpers.translate(toTranslate)
136 private onFormFields (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) {
137 this.formFields.video.push({
143 private onSettingsScripts (pluginInfo: PluginInfo, options: RegisterClientSettingsScript) {
144 const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
146 this.settingsScripts[npmName] = options
149 private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers {
150 const { plugin } = pluginInfo
151 const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
154 getBaseStaticRoute: () => {
155 const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
156 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
159 getBaseRouterRoute: () => {
160 const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
161 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/router`
165 const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings'
167 return this.authHttp.get<PublicServerSetting>(path)
169 map(p => p.publicSettings),
170 catchError(res => this.restExtractor.handleError(res))
175 getServerConfig: () => {
176 return this.server.getConfig()
177 .pipe(catchError(res => this.restExtractor.handleError(res)))
182 return this.authService.isLoggedIn()
185 getAuthHeader: () => {
186 if (!this.authService.isLoggedIn()) return undefined
188 const value = this.authService.getRequestHeaderValue()
189 return { 'Authorization': value }
193 info: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.info(text, title, timeout)),
194 error: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.error(text, title, timeout)),
195 success: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.success(text, title, timeout))
202 cancel?: { value: string, action?: () => void },
203 confirm?: { value: string, action?: () => void }
205 this.zone.run(() => this.customModal.show(input))
209 textMarkdownToHTML: (textMarkdown: string) => {
210 return this.markdownRenderer.textMarkdownToHTML(textMarkdown)
213 enhancedMarkdownToHTML: (enhancedMarkdown: string) => {
214 return this.markdownRenderer.enhancedMarkdownToHTML(enhancedMarkdown)
218 translate: (value: string) => {
219 return this.translationsObservable
220 .pipe(map(allTranslations => allTranslations[npmName]))
221 .pipe(map(translations => peertubeTranslate(value, translations)))
227 private loadTranslations () {
228 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
230 // Default locale, nothing to translate
231 if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay())
233 this.translationsObservable = this.authHttp
234 .get<PluginTranslation>(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json')