diff options
Diffstat (limited to 'client/src/standalone/videos/shared/peertube-plugin.ts')
-rw-r--r-- | client/src/standalone/videos/shared/peertube-plugin.ts | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/client/src/standalone/videos/shared/peertube-plugin.ts b/client/src/standalone/videos/shared/peertube-plugin.ts new file mode 100644 index 000000000..968854ce8 --- /dev/null +++ b/client/src/standalone/videos/shared/peertube-plugin.ts | |||
@@ -0,0 +1,85 @@ | |||
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' | ||
7 | |||
8 | export 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, | ||
46 | getBasePluginClientPath: unimplemented, | ||
47 | |||
48 | getSettings: () => { | ||
49 | const url = this.getPluginUrl() + '/' + pluginInfo.plugin.npmName + '/public-settings' | ||
50 | |||
51 | return this.http.fetch(url, { optionalAuth: true }) | ||
52 | .then(res => res.json()) | ||
53 | .then((obj: PublicServerSetting) => obj.publicSettings) | ||
54 | }, | ||
55 | |||
56 | isLoggedIn: () => this.http.isLoggedIn(), | ||
57 | getAuthHeader: () => { | ||
58 | if (!this.http.isLoggedIn()) return undefined | ||
59 | |||
60 | return { Authorization: this.http.getHeaderTokenValue() } | ||
61 | }, | ||
62 | |||
63 | notifier: { | ||
64 | info: unimplemented, | ||
65 | error: unimplemented, | ||
66 | success: unimplemented | ||
67 | }, | ||
68 | |||
69 | showModal: unimplemented, | ||
70 | |||
71 | getServerConfig: unimplemented, | ||
72 | |||
73 | markdownRenderer: { | ||
74 | textMarkdownToHTML: unimplemented, | ||
75 | enhancedMarkdownToHTML: unimplemented | ||
76 | }, | ||
77 | |||
78 | translate: (value: string) => Promise.resolve(peertubeTranslate(value, translations)) | ||
79 | } | ||
80 | } | ||
81 | |||
82 | private getPluginUrl () { | ||
83 | return window.location.origin + '/api/v1/plugins' | ||
84 | } | ||
85 | } | ||