PeerTube/client/src/app/core/plugins/plugin.service.ts

314 lines
9.8 KiB
TypeScript
Raw Normal View History

2021-08-17 11:27:47 +02:00
import { firstValueFrom, Observable, of } from 'rxjs'
import { catchError, map, shareReplay } from 'rxjs/operators'
2019-07-25 19:02:54 +02:00
import { HttpClient } from '@angular/common/http'
2020-06-23 14:10:17 +02:00
import { Inject, Injectable, LOCALE_ID, NgZone } from '@angular/core'
import { VideoEditType } from '@app/+videos/+video-edit/shared/video-edit.type'
2020-04-16 16:12:10 +02:00
import { AuthService } from '@app/core/auth'
import { Notifier } from '@app/core/notification'
2020-06-23 14:10:17 +02:00
import { MarkdownService } from '@app/core/renderer'
import { RestExtractor } from '@app/core/rest'
import { ServerService } from '@app/core/server/server.service'
2020-08-20 11:46:25 +02:00
import { getDevLocale, isOnDevLocale } from '@app/helpers'
import { CustomModalComponent } from '@app/modal/custom-modal.component'
import { getCompleteLocale, getKeys, isDefaultLocale, peertubeTranslate } from '@peertube/peertube-core-utils'
2020-06-23 14:10:17 +02:00
import {
ClientHook,
ClientHookName,
PluginClientScope,
PluginTranslation,
PluginType,
PluginType_Type,
2020-06-23 14:10:17 +02:00
PublicServerSetting,
RegisterClientFormFieldOptions,
RegisterClientRouteOptions,
2021-12-29 14:08:07 +01:00
RegisterClientSettingsScriptOptions,
RegisterClientVideoFieldOptions,
2020-06-23 14:10:17 +02:00
ServerConfigPlugin
} from '@peertube/peertube-models'
import { PluginInfo, PluginsManager } from '@root-helpers/plugins-manager'
2020-06-23 14:10:17 +02:00
import { environment } from '../../../environments/environment'
import { RegisterClientHelpers } from '../../../types/register-client-option.model'
2021-06-04 11:14:21 +02:00
type FormFields = {
video: {
2021-12-29 14:08:07 +01:00
pluginInfo: PluginInfo
commonOptions: RegisterClientFormFieldOptions
videoFormOptions: RegisterClientVideoFieldOptions
}[]
}
2019-07-08 15:54:08 +02:00
@Injectable()
2019-07-22 15:40:13 +02:00
export class PluginService implements ClientHook {
2019-07-26 14:44:50 +02:00
private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins'
private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins'
2019-07-25 19:02:54 +02:00
2019-07-26 14:44:50 +02:00
translationsObservable: Observable<PluginTranslation>
customModal: CustomModalComponent
private formFields: FormFields = {
video: []
}
private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScriptOptions } = {}
private clientRoutes: {
[ parentRoute in RegisterClientRouteOptions['parentRoute'] ]?: {
[ route: string ]: RegisterClientRouteOptions
}
} = {}
2019-07-08 15:54:08 +02:00
private pluginsManager: PluginsManager
2019-07-08 15:54:08 +02:00
constructor (
private authService: AuthService,
private notifier: Notifier,
private markdownRenderer: MarkdownService,
2019-07-25 19:02:54 +02:00
private server: ServerService,
private zone: NgZone,
2019-07-25 19:02:54 +02:00
private authHttp: HttpClient,
2019-07-26 14:44:50 +02:00
private restExtractor: RestExtractor,
@Inject(LOCALE_ID) private localeId: string
2019-07-08 15:54:08 +02:00
) {
2019-07-26 14:44:50 +02:00
this.loadTranslations()
this.pluginsManager = new PluginsManager({
peertubeHelpersFactory: this.buildPeerTubeHelpers.bind(this),
onFormFields: this.onFormFields.bind(this),
onSettingsScripts: this.onSettingsScripts.bind(this),
onClientRoute: this.onClientRoute.bind(this)
})
2019-07-08 15:54:08 +02:00
}
initializePlugins () {
this.pluginsManager.loadPluginsList(this.server.getHTMLConfig())
2021-06-04 11:14:21 +02:00
this.pluginsManager.ensurePluginsAreLoaded('common')
2019-07-08 15:54:08 +02:00
}
initializeCustomModal (customModal: CustomModalComponent) {
this.customModal = customModal
}
runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
return this.zone.runOutsideAngular(() => {
return this.pluginsManager.runHook(hookName, result, params)
})
2019-07-08 15:54:08 +02:00
}
ensurePluginsAreLoaded (scope: PluginClientScope) {
return this.pluginsManager.ensurePluginsAreLoaded(scope)
2019-07-10 14:06:19 +02:00
}
reloadLoadedScopes () {
return this.pluginsManager.reloadLoadedScopes()
2019-07-10 14:06:19 +02:00
}
getPluginsManager () {
return this.pluginsManager
2019-07-10 14:06:19 +02:00
}
addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
return this.pluginsManager.addPlugin(plugin, isTheme)
2019-07-08 15:54:08 +02:00
}
removePlugin (plugin: ServerConfigPlugin) {
return this.pluginsManager.removePlugin(plugin)
2019-07-08 15:54:08 +02:00
}
nameToNpmName (name: string, type: PluginType_Type) {
2019-07-25 19:02:54 +02:00
const prefix = type === PluginType.PLUGIN
? 'peertube-plugin-'
: 'peertube-theme-'
return prefix + name
}
getRegisteredVideoFormFields (type: VideoEditType) {
return this.formFields.video.filter(f => f.videoFormOptions.type === type)
}
2021-04-09 13:50:31 +02:00
getRegisteredSettingsScript (npmName: string) {
return this.settingsScripts[npmName]
}
getRegisteredClientRoute (route: string, parentRoute: RegisterClientRouteOptions['parentRoute']) {
if (!this.clientRoutes[parentRoute]) {
return undefined
}
return this.clientRoutes[parentRoute][route]
}
getAllRegisteredClientRoutesForParent (parentRoute: RegisterClientRouteOptions['parentRoute']) {
return this.clientRoutes[parentRoute]
}
getAllRegisteredClientRoutes () {
return Object.keys(this.clientRoutes)
.map((parentRoute: RegisterClientRouteOptions['parentRoute']) => {
return Object.keys(this.clientRoutes[parentRoute])
.map(route => {
if (parentRoute === '/') return route
return parentRoute + route
})
})
.flat()
}
2021-12-29 14:08:07 +01:00
async translateSetting (npmName: string, setting: RegisterClientFormFieldOptions) {
for (const key of getKeys(setting, [ 'label', 'html', 'descriptionHTML' ])) {
2021-12-29 14:08:07 +01:00
if (setting[key]) setting[key] = await this.translateBy(npmName, setting[key])
2021-04-09 13:21:33 +02:00
}
2021-12-29 14:08:07 +01:00
if (Array.isArray(setting.options)) {
const newOptions = []
for (const o of setting.options) {
newOptions.push({
value: o.value,
label: await this.translateBy(npmName, o.label)
})
}
setting.options = newOptions
}
2021-04-09 13:21:33 +02:00
}
2021-12-29 14:08:07 +01:00
translateBy (npmName: string, toTranslate: string) {
const obs = this.translationsObservable
.pipe(
map(allTranslations => allTranslations[npmName]),
map(translations => peertubeTranslate(toTranslate, translations))
)
return firstValueFrom(obs)
}
private onFormFields (
pluginInfo: PluginInfo,
commonOptions: RegisterClientFormFieldOptions,
videoFormOptions: RegisterClientVideoFieldOptions
) {
this.formFields.video.push({
2021-12-29 14:08:07 +01:00
pluginInfo,
commonOptions,
videoFormOptions
})
2019-07-08 15:54:08 +02:00
}
private onSettingsScripts (pluginInfo: PluginInfo, options: RegisterClientSettingsScriptOptions) {
2021-12-29 14:08:07 +01:00
this.settingsScripts[pluginInfo.plugin.npmName] = options
2019-07-08 15:54:08 +02:00
}
private onClientRoute (options: RegisterClientRouteOptions) {
const parentRoute = options.parentRoute || '/'
const route = options.route.startsWith('/')
? options.route
: `/${options.route}`
if (!this.clientRoutes[parentRoute]) {
this.clientRoutes[parentRoute] = {}
}
this.clientRoutes[parentRoute][route] = options
}
2019-07-26 14:44:50 +02:00
private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers {
2019-07-16 16:09:58 +02:00
const { plugin } = pluginInfo
2021-12-29 14:08:07 +01:00
const npmName = pluginInfo.plugin.npmName
2019-07-16 16:09:58 +02:00
return {
getBaseStaticRoute: () => {
const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
2019-07-16 16:09:58 +02:00
return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
2019-07-25 19:02:54 +02:00
},
getBaseRouterRoute: () => {
const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/router`
},
getBaseWebSocketRoute: () => {
const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/ws`
},
getBasePluginClientPath: () => {
return '/p'
},
2019-07-25 19:02:54 +02:00
getSettings: () => {
2019-07-26 14:44:50 +02:00
const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings'
2019-07-25 19:02:54 +02:00
2021-08-17 11:27:47 +02:00
const obs = this.authHttp.get<PublicServerSetting>(path)
2019-07-25 19:02:54 +02:00
.pipe(
2019-07-26 09:35:43 +02:00
map(p => p.publicSettings),
2019-07-25 19:02:54 +02:00
catchError(res => this.restExtractor.handleError(res))
)
2021-08-17 11:27:47 +02:00
return firstValueFrom(obs)
2019-07-26 14:44:50 +02:00
},
2021-03-24 11:34:31 +01:00
getServerConfig: () => {
2021-08-17 11:27:47 +02:00
const obs = this.server.getConfig()
2021-03-24 11:34:31 +01:00
.pipe(catchError(res => this.restExtractor.handleError(res)))
2021-08-17 11:27:47 +02:00
return firstValueFrom(obs)
2021-03-24 11:34:31 +01:00
},
isLoggedIn: () => {
return this.authService.isLoggedIn()
},
2021-04-22 11:29:06 +02:00
getAuthHeader: () => {
if (!this.authService.isLoggedIn()) return undefined
const value = this.authService.getRequestHeaderValue()
2021-08-17 14:42:53 +02:00
return { Authorization: value }
2021-04-22 11:29:06 +02:00
},
2020-04-15 09:21:06 +02:00
notifier: {
2021-06-14 13:44:17 +02:00
info: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.info(text, title, timeout)),
error: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.error(text, title, timeout)),
success: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.success(text, title, timeout))
2020-04-15 09:21:06 +02:00
},
showModal: (input: {
2021-08-17 14:42:53 +02:00
title: string
content: string
close?: boolean
cancel?: { value: string, action?: () => void }
confirm?: { value: string, action?: () => void }
}) => {
2021-06-14 13:44:17 +02:00
this.zone.run(() => this.customModal.show(input))
},
markdownRenderer: {
textMarkdownToHTML: (textMarkdown: string) => {
return this.markdownRenderer.textMarkdownToHTML({ markdown: textMarkdown })
},
enhancedMarkdownToHTML: (enhancedMarkdown: string) => {
return this.markdownRenderer.enhancedMarkdownToHTML({ markdown: enhancedMarkdown })
}
},
2019-07-26 14:44:50 +02:00
translate: (value: string) => {
2021-12-29 14:08:07 +01:00
return this.translateBy(npmName, value)
2019-07-16 16:09:58 +02:00
}
}
}
2019-07-26 14:44:50 +02:00
private loadTranslations () {
const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
// Default locale, nothing to translate
if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay())
this.translationsObservable = this.authHttp
.get<PluginTranslation>(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json')
.pipe(shareReplay())
}
2019-07-08 15:54:08 +02:00
}