From ab217bdc3552d9711c51e0e711d2f0ba86565e48 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 24 Jan 2024 03:30:51 -0700 Subject: [PATCH] Support optional MSC3860 redirects (#4007) * Support optional MSC3860 redirects See `allow_redirect` across the media endpoints: https://spec.matrix.org/v1.9/client-server-api/#client-behaviour-7 * Update the tests * Appease the linter * Add test to appease SonarCloud * Only add `allow_redirect` if the parameter is specified rather than defaulting to `false` Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- spec/unit/content-repo.spec.ts | 15 +++++++++++++++ spec/unit/matrix-client.spec.ts | 16 ++++++++++++++++ src/client.ts | 6 +++++- src/content-repo.ts | 9 +++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/spec/unit/content-repo.spec.ts b/spec/unit/content-repo.spec.ts index 91953945ac7..33eeab12d5e 100644 --- a/spec/unit/content-repo.spec.ts +++ b/spec/unit/content-repo.spec.ts @@ -37,6 +37,21 @@ describe("ContentRepo", function () { ); }); + it("should allow redirects when requested on download URLs", function () { + const mxcUri = "mxc://server.name/resourceid"; + expect(getHttpUriForMxc(baseUrl, mxcUri, undefined, undefined, undefined, false, true)).toEqual( + baseUrl + "/_matrix/media/v3/download/server.name/resourceid?allow_redirect=true", + ); + }); + + it("should allow redirects when requested on thumbnail URLs", function () { + const mxcUri = "mxc://server.name/resourceid"; + expect(getHttpUriForMxc(baseUrl, mxcUri, 32, 32, "scale", false, true)).toEqual( + baseUrl + + "/_matrix/media/v3/thumbnail/server.name/resourceid?width=32&height=32&method=scale&allow_redirect=true", + ); + }); + it("should return the empty string for null input", function () { expect(getHttpUriForMxc(null as any, "")).toEqual(""); }); diff --git a/spec/unit/matrix-client.spec.ts b/spec/unit/matrix-client.spec.ts index 920bec55804..8f87d6f8cc6 100644 --- a/spec/unit/matrix-client.spec.ts +++ b/spec/unit/matrix-client.spec.ts @@ -55,6 +55,7 @@ import { RuleId, IPushRule, ConditionKind, + getHttpUriForMxc, } from "../../src"; import { supportsMatrixCall } from "../../src/webrtc/call"; import { makeBeaconEvent } from "../test-utils/beacon"; @@ -369,6 +370,21 @@ describe("MatrixClient", function () { client.stopClient(); }); + describe("mxcUrlToHttp", () => { + it("should call getHttpUriForMxc", () => { + const mxc = "mxc://server/example"; + expect(client.mxcUrlToHttp(mxc)).toBe(getHttpUriForMxc(client.baseUrl, mxc)); + expect(client.mxcUrlToHttp(mxc, 32)).toBe(getHttpUriForMxc(client.baseUrl, mxc, 32)); + expect(client.mxcUrlToHttp(mxc, 32, 46)).toBe(getHttpUriForMxc(client.baseUrl, mxc, 32, 46)); + expect(client.mxcUrlToHttp(mxc, 32, 46, "scale")).toBe( + getHttpUriForMxc(client.baseUrl, mxc, 32, 46, "scale"), + ); + expect(client.mxcUrlToHttp(mxc, 32, 46, "scale", false, true)).toBe( + getHttpUriForMxc(client.baseUrl, mxc, 32, 46, "scale", false, true), + ); + }); + }); + describe("timestampToEvent", () => { const roomId = "!room:server.org"; const eventId = "$eventId:example.org"; diff --git a/src/client.ts b/src/client.ts index 3acc0135209..bb294f87e38 100644 --- a/src/client.ts +++ b/src/client.ts @@ -5805,6 +5805,9 @@ export class MatrixClient extends TypedEventEmitter= 0) {