From ec829435bfe4ab8010740fc6884571e23f6e652e Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Mon, 26 Aug 2024 09:11:20 +0200 Subject: [PATCH 1/5] Implement download_file in widget driver Signed-off-by: Michael Weimann --- src/stores/widgets/StopGapWidgetDriver.ts | 15 ++++++++++ .../widgets/StopGapWidgetDriver-test.ts | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/stores/widgets/StopGapWidgetDriver.ts b/src/stores/widgets/StopGapWidgetDriver.ts index 6bb3e887da8..94ac9e5f8d9 100644 --- a/src/stores/widgets/StopGapWidgetDriver.ts +++ b/src/stores/widgets/StopGapWidgetDriver.ts @@ -73,6 +73,7 @@ import { navigateToPermalink } from "../../utils/permalinks/navigator"; import { SdkContextClass } from "../../contexts/SDKContext"; import { ModuleRunner } from "../../modules/ModuleRunner"; import SettingsStore from "../../settings/SettingsStore"; +import { Media } from "../../customisations/Media"; // TODO: Purge this from the universe @@ -679,4 +680,18 @@ export class StopGapWidgetDriver extends WidgetDriver { return { contentUri: uploadResult.content_uri }; } + + /** + * Download a file from the media repository on the homeserver. + * + * @param contentUri - the MXC URI of the file to download + * @returns an object with: file - response contents as Blob + */ + public async downloadFile(contentUri: string): Promise<{ file: XMLHttpRequestBodyInit }> { + const client = MatrixClientPeg.safeGet(); + const media = new Media({mxc: contentUri}, client); + const response = await media.downloadSource(); + const blob = await response.blob(); + return { file: blob }; + } } diff --git a/test/stores/widgets/StopGapWidgetDriver-test.ts b/test/stores/widgets/StopGapWidgetDriver-test.ts index 282ce23d6fa..d3751b7b799 100644 --- a/test/stores/widgets/StopGapWidgetDriver-test.ts +++ b/test/stores/widgets/StopGapWidgetDriver-test.ts @@ -15,6 +15,7 @@ limitations under the License. */ import { mocked, MockedObject } from "jest-mock"; +import fetchMockJest from "fetch-mock-jest"; import { MatrixClient, ClientEvent, @@ -616,4 +617,32 @@ describe("StopGapWidgetDriver", () => { expect(client.uploadContent).toHaveBeenCalledWith("data"); }); }); + + describe("downloadFile", () => { + let driver: WidgetDriver; + + beforeEach(() => { + driver = mkDefaultDriver(); + }); + + it("should download a file and return the blob", async () => { + // eslint-disable-next-line no-restricted-properties + client.mxcUrlToHttp.mockImplementation((mxcUrl) => { + if (mxcUrl === "mxc://example.com/test_file") { + return "https://example.com/_matrix/media/v3/download/test_file"; + } + + return null; + }); + + fetchMockJest.get("https://example.com/_matrix/media/v3/download/test_file", "test contents"); + + const result = await driver.downloadFile("mxc://example.com/test_file"); + // A type test is impossible here because of + // https://github.com/jefflau/jest-fetch-mock/issues/209 + // Tell TypeScript that file is a blob. + const file = result.file as Blob; + await expect(file.text()).resolves.toEqual("test contents"); + }); + }); }); From 5ca0745f37b4ded551a28d908123248106dea05f Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Tue, 27 Aug 2024 15:49:51 +0200 Subject: [PATCH 2/5] Fix test URIs Signed-off-by: Michael Weimann --- test/stores/widgets/StopGapWidgetDriver-test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/stores/widgets/StopGapWidgetDriver-test.ts b/test/stores/widgets/StopGapWidgetDriver-test.ts index d3751b7b799..960852e6065 100644 --- a/test/stores/widgets/StopGapWidgetDriver-test.ts +++ b/test/stores/widgets/StopGapWidgetDriver-test.ts @@ -629,13 +629,13 @@ describe("StopGapWidgetDriver", () => { // eslint-disable-next-line no-restricted-properties client.mxcUrlToHttp.mockImplementation((mxcUrl) => { if (mxcUrl === "mxc://example.com/test_file") { - return "https://example.com/_matrix/media/v3/download/test_file"; + return "https://example.com/_matrix/media/v3/download/example.com/test_file"; } return null; }); - fetchMockJest.get("https://example.com/_matrix/media/v3/download/test_file", "test contents"); + fetchMockJest.get("https://example.com/_matrix/media/v3/download/example.com/test_file", "test contents"); const result = await driver.downloadFile("mxc://example.com/test_file"); // A type test is impossible here because of From e2b30adb6a8a3a8f27d0a4b4a8f7255db1fd7124 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Wed, 28 Aug 2024 15:57:42 +0200 Subject: [PATCH 3/5] Use download-file branch as widget-api source Signed-off-by: Michael Weimann --- package.json | 2 +- yarn.lock | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index a37866dbbe5..3d9c666631e 100644 --- a/package.json +++ b/package.json @@ -117,7 +117,7 @@ "matrix-encrypt-attachment": "^1.0.3", "matrix-events-sdk": "0.0.1", "matrix-js-sdk": "github:matrix-org/matrix-js-sdk#develop", - "matrix-widget-api": "^1.8.2", + "matrix-widget-api": "github:nordeck/matrix-widget-api#download-file", "memoize-one": "^6.0.0", "minimist": "^1.2.5", "oidc-client-ts": "^3.0.1", diff --git a/yarn.lock b/yarn.lock index ccc28991ddb..9f07ac9f4a6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6952,6 +6952,13 @@ matrix-widget-api@^1.8.2: "@types/events" "^3.0.0" events "^3.2.0" +"matrix-widget-api@github:nordeck/matrix-widget-api#download-file": + version "1.8.2" + resolved "https://codeload.github.com/nordeck/matrix-widget-api/tar.gz/7c06a33680cdef6873e5e70070db11e79ed39979" + dependencies: + "@types/events" "^3.0.0" + events "^3.2.0" + md5@^2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f" From 50b599242b09bb510a1827ee4c09bfebf138b478 Mon Sep 17 00:00:00 2001 From: Kim Brose Date: Fri, 30 Aug 2024 16:18:08 +0200 Subject: [PATCH 4/5] bump matrix-widget-api to 1.9.0 Signed-off-by: Kim Brose --- package.json | 2 +- yarn.lock | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 3d9c666631e..7f380bbca8e 100644 --- a/package.json +++ b/package.json @@ -117,7 +117,7 @@ "matrix-encrypt-attachment": "^1.0.3", "matrix-events-sdk": "0.0.1", "matrix-js-sdk": "github:matrix-org/matrix-js-sdk#develop", - "matrix-widget-api": "github:nordeck/matrix-widget-api#download-file", + "matrix-widget-api": "^1.9.0", "memoize-one": "^6.0.0", "minimist": "^1.2.5", "oidc-client-ts": "^3.0.1", diff --git a/yarn.lock b/yarn.lock index 9f07ac9f4a6..ecf0c747609 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6952,9 +6952,10 @@ matrix-widget-api@^1.8.2: "@types/events" "^3.0.0" events "^3.2.0" -"matrix-widget-api@github:nordeck/matrix-widget-api#download-file": - version "1.8.2" - resolved "https://codeload.github.com/nordeck/matrix-widget-api/tar.gz/7c06a33680cdef6873e5e70070db11e79ed39979" +matrix-widget-api@^1.9.0: + version "1.9.0" + resolved "https://registry.yarnpkg.com/matrix-widget-api/-/matrix-widget-api-1.9.0.tgz#884136b405bd3c56e4ea285095c9e01ec52b6b1f" + integrity sha512-au8mqralNDqrEvaVAkU37bXOb8I9SCe+ACdPk11QWw58FKstVq31q2wRz+qWA6J+42KJ6s1DggWbG/S3fEs3jw== dependencies: "@types/events" "^3.0.0" events "^3.2.0" From 363270850b373445653e19327ff06544c0759c72 Mon Sep 17 00:00:00 2001 From: Kim Brose Date: Fri, 30 Aug 2024 16:23:35 +0200 Subject: [PATCH 5/5] prettier Signed-off-by: Kim Brose --- src/stores/widgets/StopGapWidgetDriver.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stores/widgets/StopGapWidgetDriver.ts b/src/stores/widgets/StopGapWidgetDriver.ts index 94ac9e5f8d9..b61fafbd520 100644 --- a/src/stores/widgets/StopGapWidgetDriver.ts +++ b/src/stores/widgets/StopGapWidgetDriver.ts @@ -689,9 +689,9 @@ export class StopGapWidgetDriver extends WidgetDriver { */ public async downloadFile(contentUri: string): Promise<{ file: XMLHttpRequestBodyInit }> { const client = MatrixClientPeg.safeGet(); - const media = new Media({mxc: contentUri}, client); + const media = new Media({ mxc: contentUri }, client); const response = await media.downloadSource(); const blob = await response.blob(); - return { file: blob }; + return { file: blob }; } }