Skip to content

Commit e7b682a

Browse files
committed
Fix stringified promises in HTML exports
Fixes element-hq/element-web#24272 HtmlExporter was coercing Promises into strings. This removes the unecessary async labels on the offending methods. Signed-off-by: Clark Fischer <[email protected]>
1 parent b69b64a commit e7b682a

File tree

5 files changed

+147
-15
lines changed

5 files changed

+147
-15
lines changed

src/utils/exportUtils/HtmlExport.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export default class HTMLExporter extends Exporter {
238238
}
239239
}
240240

241-
protected async getDateSeparator(event: MatrixEvent): Promise<string> {
241+
protected getDateSeparator(event: MatrixEvent): string {
242242
const ts = event.getTs();
243243
const dateSeparator = (
244244
<li key={ts}>
@@ -248,7 +248,7 @@ export default class HTMLExporter extends Exporter {
248248
return renderToStaticMarkup(dateSeparator);
249249
}
250250

251-
protected async needsDateSeparator(event: MatrixEvent, prevEvent: MatrixEvent | null): Promise<boolean> {
251+
protected needsDateSeparator(event: MatrixEvent, prevEvent: MatrixEvent | null): boolean {
252252
if (!prevEvent) return true;
253253
return wantsDateSeparator(prevEvent.getDate() || undefined, event.getDate() || undefined);
254254
}
@@ -400,7 +400,7 @@ export default class HTMLExporter extends Exporter {
400400
if (this.cancelled) return this.cleanUp();
401401
if (!haveRendererForEvent(event, false)) continue;
402402

403-
content += (await this.needsDateSeparator(event, prevEvent)) ? this.getDateSeparator(event) : "";
403+
content += this.needsDateSeparator(event, prevEvent) ? this.getDateSeparator(event) : "";
404404
const shouldBeJoined =
405405
!this.needsDateSeparator(event, prevEvent) &&
406406
shouldFormContinuation(prevEvent, event, false, this.threadsEnabled);

src/utils/exportUtils/exportCSS.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ const getExportCSS = async (usedClasses: Set<string>): Promise<string> => {
5858

5959
// If the light theme isn't loaded we will have to fetch & parse it manually
6060
if (!stylesheets.some(isLightTheme)) {
61-
const href = document.querySelector<HTMLLinkElement>('link[rel="stylesheet"][href$="theme-light.css"]').href;
62-
stylesheets.push(await getRulesFromCssFile(href));
61+
const href = document.querySelector<HTMLLinkElement>('link[rel="stylesheet"][href$="theme-light.css"]')?.href;
62+
if (href) stylesheets.push(await getRulesFromCssFile(href));
6363
}
6464

6565
let css = "";

test/test-utils/test-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ export function createTestClient(): MatrixClient {
207207
setPassword: jest.fn().mockRejectedValue({}),
208208
groupCallEventHandler: { groupCalls: new Map<string, GroupCall>() },
209209
redactEvent: jest.fn(),
210+
211+
createMessagesRequest: jest.fn().mockResolvedValue({
212+
chunk: [],
213+
}),
210214
} as unknown as MatrixClient;
211215

212216
client.reEmitter = new ReEmitter(client);
Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Matrix.org Foundation C.I.C.
2+
Copyright 2022 - 2023 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,26 +14,36 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { mocked } from "jest-mock";
17+
import { EventType, IRoomEvent, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
1818

19-
import { createTestClient, mkStubRoom, REPEATABLE_DATE } from "../../test-utils";
19+
import { mkStubRoom, REPEATABLE_DATE, stubClient } from "../../test-utils";
2020
import { ExportType, IExportOptions } from "../../../src/utils/exportUtils/exportUtils";
2121
import SdkConfig from "../../../src/SdkConfig";
2222
import HTMLExporter from "../../../src/utils/exportUtils/HtmlExport";
23+
import DMRoomMap from "../../../src/utils/DMRoomMap";
24+
25+
jest.mock("jszip");
2326

2427
describe("HTMLExport", () => {
28+
let client: jest.Mocked<MatrixClient>;
29+
2530
beforeEach(() => {
2631
jest.useFakeTimers();
2732
jest.setSystemTime(REPEATABLE_DATE);
28-
});
2933

30-
afterEach(() => {
31-
mocked(SdkConfig.get).mockRestore();
34+
client = stubClient() as jest.Mocked<MatrixClient>;
35+
DMRoomMap.makeShared();
3236
});
3337

38+
function getMessageFile(exporter: HTMLExporter): Blob {
39+
//@ts-ignore private access
40+
const files = exporter.files;
41+
const file = files.find((f) => f.name == "messages.html")!;
42+
return file.blob;
43+
}
44+
3445
it("should have an SDK-branded destination file name", () => {
3546
const roomName = "My / Test / Room: Welcome";
36-
const client = createTestClient();
3747
const stubOptions: IExportOptions = {
3848
attachmentsIncluded: false,
3949
maxSize: 50000000,
@@ -43,10 +53,42 @@ describe("HTMLExport", () => {
4353

4454
expect(exporter.destinationFileName).toMatchSnapshot();
4555

46-
jest.spyOn(SdkConfig, "get").mockImplementation(() => {
47-
return { brand: "BrandedChat/WithSlashes/ForFun" };
48-
});
56+
SdkConfig.put({ brand: "BrandedChat/WithSlashes/ForFun" });
4957

5058
expect(exporter.destinationFileName).toMatchSnapshot();
5159
});
60+
61+
it("should export", async () => {
62+
const room = new Room("!myroom:example.org", client, "@me:example.org");
63+
64+
const events = [...Array(50)].map<IRoomEvent>((_, i) => ({
65+
event_id: "$1",
66+
type: EventType.RoomMessage,
67+
sender: `@user${i}:example.com`,
68+
origin_server_ts: 5_000 + i * 1000,
69+
content: {
70+
msgtype: "m.text",
71+
body: `Message #${i}`,
72+
},
73+
}));
74+
75+
client.getRoom.mockReturnValue(room);
76+
client.createMessagesRequest.mockResolvedValue({ chunk: events });
77+
78+
const exporter = new HTMLExporter(
79+
room,
80+
ExportType.LastNMessages,
81+
{
82+
attachmentsIncluded: false,
83+
maxSize: 1_024 * 1_024,
84+
numberOfMessages: events.length,
85+
},
86+
() => {},
87+
);
88+
89+
await exporter.export();
90+
91+
const file = getMessageFile(exporter);
92+
expect(await file.text()).toMatchSnapshot();
93+
});
5294
});

test/utils/exportUtils/__snapshots__/HTMLExport-test.ts.snap

Lines changed: 86 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)