Support more plugin helpers in embed

This commit is contained in:
Chocobozzz 2022-03-23 14:06:43 +01:00
parent fa737aa058
commit 5302f77d09
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 26 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import {
HttpStatusCode, HttpStatusCode,
LiveVideo, LiveVideo,
OAuth2ErrorCode, OAuth2ErrorCode,
PublicServerSetting,
ResultList, ResultList,
UserRefreshToken, UserRefreshToken,
Video, Video,
@ -21,7 +22,7 @@ import { P2PMediaLoaderOptions, PeertubePlayerManagerOptions, PlayerMode, VideoJ
import { TranslationsManager } from '../../assets/player/translations-manager' import { TranslationsManager } from '../../assets/player/translations-manager'
import { getBoolOrDefault } from '../../root-helpers/local-storage-utils' import { getBoolOrDefault } from '../../root-helpers/local-storage-utils'
import { peertubeLocalStorage } from '../../root-helpers/peertube-web-storage' import { peertubeLocalStorage } from '../../root-helpers/peertube-web-storage'
import { PluginsManager } from '../../root-helpers/plugins-manager' import { PluginInfo, PluginsManager } from '../../root-helpers/plugins-manager'
import { UserLocalStorageKeys, UserTokens } from '../../root-helpers/users' import { UserLocalStorageKeys, UserTokens } from '../../root-helpers/users'
import { objectToUrlEncoded } from '../../root-helpers/utils' import { objectToUrlEncoded } from '../../root-helpers/utils'
import { isP2PEnabled } from '../../root-helpers/video' import { isP2PEnabled } from '../../root-helpers/video'
@ -98,6 +99,10 @@ export class PeerTubeEmbed {
return window.location.origin + '/api/v1/videos/live/' + videoId return window.location.origin + '/api/v1/videos/live/' + videoId
} }
getPluginUrl () {
return window.location.origin + '/api/v1/plugins'
}
refreshFetch (url: string, options?: RequestInit) { refreshFetch (url: string, options?: RequestInit) {
return fetch(url, options) return fetch(url, options)
.then((res: Response) => { .then((res: Response) => {
@ -760,8 +765,12 @@ export class PeerTubeEmbed {
return document.getElementById('placeholder-preview') return document.getElementById('placeholder-preview')
} }
private getHeaderTokenValue () {
return `${this.userTokens.tokenType} ${this.userTokens.accessToken}`
}
private setHeadersFromTokens () { private setHeadersFromTokens () {
this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`) this.headers.set('Authorization', this.getHeaderTokenValue())
} }
private removeTokensFromHeaders () { private removeTokensFromHeaders () {
@ -779,7 +788,7 @@ export class PeerTubeEmbed {
private loadPlugins (translations?: { [ id: string ]: string }) { private loadPlugins (translations?: { [ id: string ]: string }) {
this.pluginsManager = new PluginsManager({ this.pluginsManager = new PluginsManager({
peertubeHelpersFactory: _ => this.buildPeerTubeHelpers(translations) peertubeHelpersFactory: pluginInfo => this.buildPeerTubeHelpers(pluginInfo, translations)
}) })
this.pluginsManager.loadPluginsList(this.config) this.pluginsManager.loadPluginsList(this.config)
@ -787,7 +796,7 @@ export class PeerTubeEmbed {
return this.pluginsManager.ensurePluginsAreLoaded('embed') return this.pluginsManager.ensurePluginsAreLoaded('embed')
} }
private buildPeerTubeHelpers (translations?: { [ id: string ]: string }): RegisterClientHelpers { private buildPeerTubeHelpers (pluginInfo: PluginInfo, translations?: { [ id: string ]: string }): RegisterClientHelpers {
const unimplemented = () => { const unimplemented = () => {
throw new Error('This helper is not implemented in embed.') throw new Error('This helper is not implemented in embed.')
} }
@ -797,10 +806,20 @@ export class PeerTubeEmbed {
getBaseRouterRoute: unimplemented, getBaseRouterRoute: unimplemented,
getBasePluginClientPath: unimplemented, getBasePluginClientPath: unimplemented,
getSettings: unimplemented, getSettings: () => {
const url = this.getPluginUrl() + '/' + pluginInfo.plugin.npmName + '/public-settings'
isLoggedIn: unimplemented, return this.refreshFetch(url, { headers: this.headers })
getAuthHeader: unimplemented, .then(res => res.json())
.then((obj: PublicServerSetting) => obj.publicSettings)
},
isLoggedIn: () => !!this.userTokens,
getAuthHeader: () => {
if (!this.userTokens) return undefined
return { Authorization: this.getHeaderTokenValue() }
},
notifier: { notifier: {
info: unimplemented, info: unimplemented,