Skip to content

Commit

Permalink
Use global state to track authenticated media support
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed May 1, 2024
1 parent 5cdd706 commit 0a1d45c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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()) {
Expand Down
14 changes: 10 additions & 4 deletions src/customisations/Media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 0a1d45c

Please sign in to comment.