Fix html exports by adding SDKContext (#30987)

* Fix html exports by adding SDKContext

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* delint

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Add test

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2025-10-14 14:43:34 +01:00
committed by GitHub
parent b4396f5943
commit 56f6c1ef46
2 changed files with 61 additions and 23 deletions

View File

@@ -30,6 +30,7 @@ import MatrixClientContext from "../../contexts/MatrixClientContext";
import getExportCSS from "./exportCSS";
import { textForEvent } from "../../TextForEvent";
import { haveRendererForEvent } from "../../events/EventTileFactory";
import { SDKContext, SdkContextClass } from "../../contexts/SDKContext.ts";
import exportJS from "!!raw-loader!./exportJS";
@@ -267,29 +268,31 @@ export default class HTMLExporter extends Exporter {
return (
<div className="mx_Export_EventWrapper" id={mxEv.getId()}>
<MatrixClientContext.Provider value={this.room.client}>
<TooltipProvider>
<EventTile
mxEvent={mxEv}
continuation={continuation}
isRedacted={mxEv.isRedacted()}
replacingEventId={mxEv.replacingEventId()}
forExport={true}
alwaysShowTimestamps={true}
showUrlPreview={false}
checkUnmounting={() => false}
isTwelveHour={false}
last={false}
lastInSection={false}
permalinkCreator={this.permalinkCreator}
lastSuccessful={false}
isSelectedEvent={false}
showReactions={true}
layout={Layout.Group}
showReadReceipts={false}
getRelationsForEvent={this.getRelationsForEvent}
ref={ref}
/>
</TooltipProvider>
<SDKContext.Provider value={SdkContextClass.instance}>
<TooltipProvider>
<EventTile
mxEvent={mxEv}
continuation={continuation}
isRedacted={mxEv.isRedacted()}
replacingEventId={mxEv.replacingEventId()}
forExport={true}
alwaysShowTimestamps={true}
showUrlPreview={false}
checkUnmounting={() => false}
isTwelveHour={false}
last={false}
lastInSection={false}
permalinkCreator={this.permalinkCreator}
lastSuccessful={false}
isSelectedEvent={false}
showReactions={true}
layout={Layout.Group}
showReadReceipts={false}
getRelationsForEvent={this.getRelationsForEvent}
ref={ref}
/>
</TooltipProvider>
</SDKContext.Provider>
</MatrixClientContext.Provider>
</div>
);

View File

@@ -32,6 +32,7 @@ import HTMLExporter from "../../../../src/utils/exportUtils/HtmlExport";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { mediaFromMxc } from "../../../../src/customisations/Media";
import SettingsStore from "../../../../src/settings/SettingsStore";
import { SdkContextClass } from "../../../../src/contexts/SDKContext.ts";
jest.mock("jszip");
jest.mock("../../../../src/settings/SettingsStore");
@@ -75,6 +76,20 @@ const EVENT_ATTACHMENT_MALFORMED: IRoomEvent = {
},
};
const EVENT_MENTION: IRoomEvent = {
event_id: "$4",
type: EventType.RoomMessage,
sender: "@bob:example.com",
origin_server_ts: 0,
content: {
"msgtype": "m.text",
"body": "Message Alex",
"format": "org.matrix.custom.html",
"formatted_body": 'Message <a href="https://matrix.to/#/@alex:example.org">@alex:example.org</a>',
"m.mentions": { user_ids: ["@alex:example.org"] },
},
};
describe("HTMLExport", () => {
let client: jest.Mocked<MatrixClient>;
let room: Room;
@@ -96,6 +111,7 @@ describe("HTMLExport", () => {
jest.setSystemTime(REPEATABLE_DATE);
client = stubClient() as jest.Mocked<MatrixClient>;
SdkContextClass.instance.client = client;
DMRoomMap.makeShared(client);
room = new Room("!myroom:example.org", client, "@me:example.org");
@@ -716,4 +732,23 @@ describe("HTMLExport", () => {
const file = getMessageFile(exporter);
expect(file).not.toBeUndefined();
});
it("should not crash when exporting mentions", async () => {
mockMessages(EVENT_MESSAGE, EVENT_MENTION);
const exporter = new HTMLExporter(
room,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
await exporter.export();
const file = getMessageFile(exporter);
expect(file).not.toBeUndefined();
});
});