From 5302f77d095c2188859ee463128aa59eec20ea88 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 23 Mar 2022 14:06:43 +0100 Subject: Support more plugin helpers in embed --- client/src/standalone/videos/embed.ts | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'client/src/standalone/videos/embed.ts') diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index 55c26ec3b..c0af1dfb3 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts @@ -8,6 +8,7 @@ import { HttpStatusCode, LiveVideo, OAuth2ErrorCode, + PublicServerSetting, ResultList, UserRefreshToken, Video, @@ -21,7 +22,7 @@ import { P2PMediaLoaderOptions, PeertubePlayerManagerOptions, PlayerMode, VideoJ import { TranslationsManager } from '../../assets/player/translations-manager' import { getBoolOrDefault } from '../../root-helpers/local-storage-utils' 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 { objectToUrlEncoded } from '../../root-helpers/utils' import { isP2PEnabled } from '../../root-helpers/video' @@ -98,6 +99,10 @@ export class PeerTubeEmbed { return window.location.origin + '/api/v1/videos/live/' + videoId } + getPluginUrl () { + return window.location.origin + '/api/v1/plugins' + } + refreshFetch (url: string, options?: RequestInit) { return fetch(url, options) .then((res: Response) => { @@ -760,8 +765,12 @@ export class PeerTubeEmbed { return document.getElementById('placeholder-preview') } + private getHeaderTokenValue () { + return `${this.userTokens.tokenType} ${this.userTokens.accessToken}` + } + private setHeadersFromTokens () { - this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`) + this.headers.set('Authorization', this.getHeaderTokenValue()) } private removeTokensFromHeaders () { @@ -779,7 +788,7 @@ export class PeerTubeEmbed { private loadPlugins (translations?: { [ id: string ]: string }) { this.pluginsManager = new PluginsManager({ - peertubeHelpersFactory: _ => this.buildPeerTubeHelpers(translations) + peertubeHelpersFactory: pluginInfo => this.buildPeerTubeHelpers(pluginInfo, translations) }) this.pluginsManager.loadPluginsList(this.config) @@ -787,7 +796,7 @@ export class PeerTubeEmbed { return this.pluginsManager.ensurePluginsAreLoaded('embed') } - private buildPeerTubeHelpers (translations?: { [ id: string ]: string }): RegisterClientHelpers { + private buildPeerTubeHelpers (pluginInfo: PluginInfo, translations?: { [ id: string ]: string }): RegisterClientHelpers { const unimplemented = () => { throw new Error('This helper is not implemented in embed.') } @@ -797,10 +806,20 @@ export class PeerTubeEmbed { getBaseRouterRoute: unimplemented, getBasePluginClientPath: unimplemented, - getSettings: unimplemented, + getSettings: () => { + const url = this.getPluginUrl() + '/' + pluginInfo.plugin.npmName + '/public-settings' - isLoggedIn: unimplemented, - getAuthHeader: unimplemented, + return this.refreshFetch(url, { headers: this.headers }) + .then(res => res.json()) + .then((obj: PublicServerSetting) => obj.publicSettings) + }, + + isLoggedIn: () => !!this.userTokens, + getAuthHeader: () => { + if (!this.userTokens) return undefined + + return { Authorization: this.getHeaderTokenValue() } + }, notifier: { info: unimplemented, -- cgit v1.2.3