1 import { peertubeTranslate } from '../../../../../shared/core-utils/i18n'
2 import { HTMLServerConfig, PublicServerSetting } from '../../../../../shared/models'
3 import { PluginInfo, PluginsManager } from '../../../root-helpers'
4 import { RegisterClientHelpers } from '../../../types'
5 import { AuthHTTP } from './auth-http'
6 import { Translations } from './translations'
8 export class PeerTubePlugin {
10 private pluginsManager: PluginsManager
12 constructor (private readonly http: AuthHTTP) {
16 loadPlugins (config: HTMLServerConfig, translations?: Translations) {
17 this.pluginsManager = new PluginsManager({
18 peertubeHelpersFactory: pluginInfo => this.buildPeerTubeHelpers({
24 this.pluginsManager.loadPluginsList(config)
26 return this.pluginsManager.ensurePluginsAreLoaded('embed')
29 getPluginsManager () {
30 return this.pluginsManager
33 private buildPeerTubeHelpers (options: {
34 pluginInfo: PluginInfo
35 translations?: Translations
36 }): RegisterClientHelpers {
37 const { pluginInfo, translations } = options
39 const unimplemented = () => {
40 throw new Error('This helper is not implemented in embed.')
44 getBaseStaticRoute: unimplemented,
45 getBaseRouterRoute: unimplemented,
46 getBaseWebSocketRoute: unimplemented,
47 getBasePluginClientPath: unimplemented,
50 const url = this.getPluginUrl() + '/' + pluginInfo.plugin.npmName + '/public-settings'
52 return this.http.fetch(url, { optionalAuth: true })
53 .then(res => res.json())
54 .then((obj: PublicServerSetting) => obj.publicSettings)
57 isLoggedIn: () => this.http.isLoggedIn(),
58 getAuthHeader: () => {
59 if (!this.http.isLoggedIn()) return undefined
61 return { Authorization: this.http.getHeaderTokenValue() }
67 success: unimplemented
70 showModal: unimplemented,
72 getServerConfig: unimplemented,
75 textMarkdownToHTML: unimplemented,
76 enhancedMarkdownToHTML: unimplemented
79 translate: (value: string) => Promise.resolve(peertubeTranslate(value, translations))
83 private getPluginUrl () {
84 return window.location.origin + '/api/v1/plugins'