]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/standalone/videos/shared/peertube-plugin.ts
Add ability for plugins to register ws routes
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / shared / peertube-plugin.ts
CommitLineData
f1a0f3b7
C
1import { peertubeTranslate } from '../../../../../shared/core-utils/i18n'
2import { HTMLServerConfig, PublicServerSetting } from '../../../../../shared/models'
3import { PluginInfo, PluginsManager } from '../../../root-helpers'
4import { RegisterClientHelpers } from '../../../types'
5import { AuthHTTP } from './auth-http'
6import { Translations } from './translations'
7
8export class PeerTubePlugin {
9
10 private pluginsManager: PluginsManager
11
12 constructor (private readonly http: AuthHTTP) {
13
14 }
15
16 loadPlugins (config: HTMLServerConfig, translations?: Translations) {
17 this.pluginsManager = new PluginsManager({
18 peertubeHelpersFactory: pluginInfo => this.buildPeerTubeHelpers({
19 pluginInfo,
20 translations
21 })
22 })
23
24 this.pluginsManager.loadPluginsList(config)
25
26 return this.pluginsManager.ensurePluginsAreLoaded('embed')
27 }
28
29 getPluginsManager () {
30 return this.pluginsManager
31 }
32
33 private buildPeerTubeHelpers (options: {
34 pluginInfo: PluginInfo
35 translations?: Translations
36 }): RegisterClientHelpers {
37 const { pluginInfo, translations } = options
38
39 const unimplemented = () => {
40 throw new Error('This helper is not implemented in embed.')
41 }
42
43 return {
44 getBaseStaticRoute: unimplemented,
45 getBaseRouterRoute: unimplemented,
9d4c60dc 46 getBaseWebSocketRoute: unimplemented,
f1a0f3b7
C
47 getBasePluginClientPath: unimplemented,
48
49 getSettings: () => {
50 const url = this.getPluginUrl() + '/' + pluginInfo.plugin.npmName + '/public-settings'
51
52 return this.http.fetch(url, { optionalAuth: true })
53 .then(res => res.json())
54 .then((obj: PublicServerSetting) => obj.publicSettings)
55 },
56
57 isLoggedIn: () => this.http.isLoggedIn(),
58 getAuthHeader: () => {
59 if (!this.http.isLoggedIn()) return undefined
60
61 return { Authorization: this.http.getHeaderTokenValue() }
62 },
63
64 notifier: {
65 info: unimplemented,
66 error: unimplemented,
67 success: unimplemented
68 },
69
70 showModal: unimplemented,
71
72 getServerConfig: unimplemented,
73
74 markdownRenderer: {
75 textMarkdownToHTML: unimplemented,
76 enhancedMarkdownToHTML: unimplemented
77 },
78
79 translate: (value: string) => Promise.resolve(peertubeTranslate(value, translations))
80 }
81 }
82
83 private getPluginUrl () {
84 return window.location.origin + '/api/v1/plugins'
85 }
86}