aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/standalone/videos/shared/peertube-plugin.ts
blob: 968854ce842d713c4ec34a38fb93575a377b8847 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { peertubeTranslate } from '../../../../../shared/core-utils/i18n'
import { HTMLServerConfig, PublicServerSetting } from '../../../../../shared/models'
import { PluginInfo, PluginsManager } from '../../../root-helpers'
import { RegisterClientHelpers } from '../../../types'
import { AuthHTTP } from './auth-http'
import { Translations } from './translations'

export class PeerTubePlugin {

  private pluginsManager: PluginsManager

  constructor (private readonly http: AuthHTTP) {

  }

  loadPlugins (config: HTMLServerConfig, translations?: Translations) {
    this.pluginsManager = new PluginsManager({
      peertubeHelpersFactory: pluginInfo => this.buildPeerTubeHelpers({
        pluginInfo,
        translations
      })
    })

    this.pluginsManager.loadPluginsList(config)

    return this.pluginsManager.ensurePluginsAreLoaded('embed')
  }

  getPluginsManager () {
    return this.pluginsManager
  }

  private buildPeerTubeHelpers (options: {
    pluginInfo: PluginInfo
    translations?: Translations
  }): RegisterClientHelpers {
    const { pluginInfo, translations } = options

    const unimplemented = () => {
      throw new Error('This helper is not implemented in embed.')
    }

    return {
      getBaseStaticRoute: unimplemented,
      getBaseRouterRoute: unimplemented,
      getBasePluginClientPath: unimplemented,

      getSettings: () => {
        const url = this.getPluginUrl() + '/' + pluginInfo.plugin.npmName + '/public-settings'

        return this.http.fetch(url, { optionalAuth: true })
          .then(res => res.json())
          .then((obj: PublicServerSetting) => obj.publicSettings)
      },

      isLoggedIn: () => this.http.isLoggedIn(),
      getAuthHeader: () => {
        if (!this.http.isLoggedIn()) return undefined

        return { Authorization: this.http.getHeaderTokenValue() }
      },

      notifier: {
        info: unimplemented,
        error: unimplemented,
        success: unimplemented
      },

      showModal: unimplemented,

      getServerConfig: unimplemented,

      markdownRenderer: {
        textMarkdownToHTML: unimplemented,
        enhancedMarkdownToHTML: unimplemented
      },

      translate: (value: string) => Promise.resolve(peertubeTranslate(value, translations))
    }
  }

  private getPluginUrl () {
    return window.location.origin + '/api/v1/plugins'
  }
}