Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 0a1d45c

Browse files
committed
Use global state to track authenticated media support
Requires matrix-org/matrix-js-sdk#4185
1 parent 5cdd706 commit 0a1d45c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Lifecycle.ts

+8
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import {
8282
tryDecryptToken,
8383
} from "./utils/tokens/tokens";
8484
import { TokenRefresher } from "./utils/oidc/TokenRefresher";
85+
import { setUseAuthenticatedMedia } from "./customisations/Media";
8586

8687
const HOMESERVER_URL_KEY = "mx_hs_url";
8788
const ID_SERVER_URL_KEY = "mx_is_url";
@@ -788,6 +789,13 @@ async function doSetLoggedIn(credentials: IMatrixClientCreds, clearStorageEnable
788789
MatrixClientPeg.replaceUsingCreds(credentials, tokenRefresher?.doRefreshAccessToken.bind(tokenRefresher));
789790
const client = MatrixClientPeg.safeGet();
790791

792+
try {
793+
setUseAuthenticatedMedia(await client.doesServerSupportUnstableFeature("org.matrix.msc3916"));
794+
} catch (e) {
795+
logger.error("Error checking for authenticated media support:", e);
796+
// we otherwise ignore the error to avoid breaking offline mode
797+
}
798+
791799
setSentryUser(credentials.userId);
792800

793801
if (PosthogAnalytics.instance.isEnabled()) {

src/customisations/Media.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ import {getMediaByUrl} from "../utils/media";
3030
// functions below create an instance of the Media class and are used throughout the
3131
// project.
3232

33+
let USE_AUTHENTICATED_MEDIA = false;
34+
35+
export function setUseAuthenticatedMedia(use: boolean): void {
36+
USE_AUTHENTICATED_MEDIA = use;
37+
}
38+
3339
/**
3440
* A media object is a representation of a "source media" and an optional
3541
* "thumbnail media", derived from event contents or external sources.
@@ -82,7 +88,7 @@ export class Media {
8288
*/
8389
public get srcHttp(): string | null {
8490
// eslint-disable-next-line no-restricted-properties
85-
return this.client.mxcUrlToHttp(this.srcMxc, undefined, undefined, undefined, false, true) || null;
91+
return this.client.mxcUrlToHttp(this.srcMxc, undefined, undefined, undefined, false, true, USE_AUTHENTICATED_MEDIA) || null;
8692
}
8793

8894
/**
@@ -92,7 +98,7 @@ export class Media {
9298
public get thumbnailHttp(): string | null {
9399
if (!this.hasThumbnail) return null;
94100
// eslint-disable-next-line no-restricted-properties
95-
return this.client.mxcUrlToHttp(this.thumbnailMxc!, undefined, undefined, undefined, false, true);
101+
return this.client.mxcUrlToHttp(this.thumbnailMxc!, undefined, undefined, undefined, false, true, USE_AUTHENTICATED_MEDIA);
96102
}
97103

98104
/**
@@ -109,7 +115,7 @@ export class Media {
109115
width = Math.floor(width * window.devicePixelRatio);
110116
height = Math.floor(height * window.devicePixelRatio);
111117
// eslint-disable-next-line no-restricted-properties
112-
return this.client.mxcUrlToHttp(this.thumbnailMxc!, width, height, mode, false, true);
118+
return this.client.mxcUrlToHttp(this.thumbnailMxc!, width, height, mode, false, true, USE_AUTHENTICATED_MEDIA);
113119
}
114120

115121
/**
@@ -124,7 +130,7 @@ export class Media {
124130
width = Math.floor(width * window.devicePixelRatio);
125131
height = Math.floor(height * window.devicePixelRatio);
126132
// eslint-disable-next-line no-restricted-properties
127-
return this.client.mxcUrlToHttp(this.srcMxc, width, height, mode, false, true);
133+
return this.client.mxcUrlToHttp(this.srcMxc, width, height, mode, false, true, USE_AUTHENTICATED_MEDIA);
128134
}
129135

130136
/**

0 commit comments

Comments
 (0)