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 getBasePluginClientPath: unimplemented,
49 const url = this.getPluginUrl() + '/' + pluginInfo.plugin.npmName + '/public-settings'
51 return this.http.fetch(url, { optionalAuth: true })
52 .then(res => res.json())
53 .then((obj: PublicServerSetting) => obj.publicSettings)
56 isLoggedIn: () => this.http.isLoggedIn(),
57 getAuthHeader: () => {
58 if (!this.http.isLoggedIn()) return undefined
60 return { Authorization: this.http.getHeaderTokenValue() }
66 success: unimplemented
69 showModal: unimplemented,
71 getServerConfig: unimplemented,
74 textMarkdownToHTML: unimplemented,
75 enhancedMarkdownToHTML: unimplemented
78 translate: (value: string) => Promise.resolve(peertubeTranslate(value, translations))
82 private getPluginUrl () {
83 return window.location.origin + '/api/v1/plugins'