From 0a1d45c0f91e9567211affe45670ac5ae1a6d502 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 1 May 2024 10:14:20 -0600 Subject: [PATCH] Use global state to track authenticated media support Requires https://github.com/matrix-org/matrix-js-sdk/pull/4185 --- src/Lifecycle.ts | 8 ++++++++ src/customisations/Media.ts | 14 ++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Lifecycle.ts b/src/Lifecycle.ts index 61097c13c21..04cf9350bba 100644 --- a/src/Lifecycle.ts +++ b/src/Lifecycle.ts @@ -82,6 +82,7 @@ import { tryDecryptToken, } from "./utils/tokens/tokens"; import { TokenRefresher } from "./utils/oidc/TokenRefresher"; +import { setUseAuthenticatedMedia } from "./customisations/Media"; const HOMESERVER_URL_KEY = "mx_hs_url"; const ID_SERVER_URL_KEY = "mx_is_url"; @@ -788,6 +789,13 @@ async function doSetLoggedIn(credentials: IMatrixClientCreds, clearStorageEnable MatrixClientPeg.replaceUsingCreds(credentials, tokenRefresher?.doRefreshAccessToken.bind(tokenRefresher)); const client = MatrixClientPeg.safeGet(); + try { + setUseAuthenticatedMedia(await client.doesServerSupportUnstableFeature("org.matrix.msc3916")); + } catch (e) { + logger.error("Error checking for authenticated media support:", e); + // we otherwise ignore the error to avoid breaking offline mode + } + setSentryUser(credentials.userId); if (PosthogAnalytics.instance.isEnabled()) { diff --git a/src/customisations/Media.ts b/src/customisations/Media.ts index 6d253348b6d..9c1dd0fbec4 100644 --- a/src/customisations/Media.ts +++ b/src/customisations/Media.ts @@ -30,6 +30,12 @@ import {getMediaByUrl} from "../utils/media"; // functions below create an instance of the Media class and are used throughout the // project. +let USE_AUTHENTICATED_MEDIA = false; + +export function setUseAuthenticatedMedia(use: boolean): void { + USE_AUTHENTICATED_MEDIA = use; +} + /** * A media object is a representation of a "source media" and an optional * "thumbnail media", derived from event contents or external sources. @@ -82,7 +88,7 @@ export class Media { */ public get srcHttp(): string | null { // eslint-disable-next-line no-restricted-properties - return this.client.mxcUrlToHttp(this.srcMxc, undefined, undefined, undefined, false, true) || null; + return this.client.mxcUrlToHttp(this.srcMxc, undefined, undefined, undefined, false, true, USE_AUTHENTICATED_MEDIA) || null; } /** @@ -92,7 +98,7 @@ export class Media { public get thumbnailHttp(): string | null { if (!this.hasThumbnail) return null; // eslint-disable-next-line no-restricted-properties - return this.client.mxcUrlToHttp(this.thumbnailMxc!, undefined, undefined, undefined, false, true); + return this.client.mxcUrlToHttp(this.thumbnailMxc!, undefined, undefined, undefined, false, true, USE_AUTHENTICATED_MEDIA); } /** @@ -109,7 +115,7 @@ export class Media { width = Math.floor(width * window.devicePixelRatio); height = Math.floor(height * window.devicePixelRatio); // eslint-disable-next-line no-restricted-properties - return this.client.mxcUrlToHttp(this.thumbnailMxc!, width, height, mode, false, true); + return this.client.mxcUrlToHttp(this.thumbnailMxc!, width, height, mode, false, true, USE_AUTHENTICATED_MEDIA); } /** @@ -124,7 +130,7 @@ export class Media { width = Math.floor(width * window.devicePixelRatio); height = Math.floor(height * window.devicePixelRatio); // eslint-disable-next-line no-restricted-properties - return this.client.mxcUrlToHttp(this.srcMxc, width, height, mode, false, true); + return this.client.mxcUrlToHttp(this.srcMxc, width, height, mode, false, true, USE_AUTHENTICATED_MEDIA); } /**