Skip to content

Replace hardcoded strings with MsgType constants #30179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/utils/exportUtils/Exporter.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
*/

import { Direction, type MatrixEvent, type Relations, type Room } from "matrix-js-sdk/src/matrix";
import { type EventType, type MediaEventContent, type RelationType } from "matrix-js-sdk/src/types";
import { EventType, type MediaEventContent, type RelationType, MsgType } from "matrix-js-sdk/src/types";
import { saveAs } from "file-saver";
import { logger } from "matrix-js-sdk/src/logger";
import sanitizeFilename from "sanitize-filename";
@@ -222,7 +222,7 @@
try {
const isEncrypted = event.isEncrypted();
const content = event.getContent<MediaEventContent>();
const shouldDecrypt = isEncrypted && content.hasOwnProperty("file") && event.getType() !== "m.sticker";
const shouldDecrypt = isEncrypted && content.hasOwnProperty("file") && event.getType() !== EventType.Sticker;
if (shouldDecrypt) {
blob = await decryptFile(content.file);
} else {
@@ -267,22 +267,22 @@
const mediaType = event.getContent().msgtype;
let fileDirectory: string;
switch (mediaType) {
case "m.image":
case MsgType.Image:
fileDirectory = "images";
break;
case "m.video":
case MsgType.Video:
fileDirectory = "videos";
break;
case "m.audio":
case MsgType.Audio:
fileDirectory = "audio";
break;
default:
fileDirectory = event.getType() === "m.sticker" ? "stickers" : "files";
fileDirectory = event.getType() === EventType.Sticker ? "stickers" : "files";
}
const fileDate = formatFullDateNoDay(new Date(event.getTs()));
let [fileName, fileExt] = this.splitFileName(event.getContent().body);

if (event.getType() === "m.sticker") fileExt = ".png";
if (event.getType() === EventType.Sticker) fileExt = ".png";
if (isVoiceMessage(event)) fileExt = ".ogg";

return this.makeUniqueFilePath({
@@ -302,7 +302,7 @@
}

protected isAttachment(mxEv: MatrixEvent): boolean {
const attachmentTypes = ["m.sticker", "m.image", "m.file", "m.video", "m.audio"];
const attachmentTypes = ["m.sticker", MsgType.Image, MsgType.File, MsgType.Video, MsgType.Audio];

Check failure on line 305 in src/utils/exportUtils/Exporter.ts

GitHub Actions / Jest (1)

PlainTextExport › should return text with 12 hr time format

TypeError: Cannot read properties of undefined (reading 'Image') at TestablePlainTextExporter.Image [as isAttachment] (src/utils/exportUtils/Exporter.ts:305:55) at TestablePlainTextExporter.isAttachment [as plainTextForEvent] (src/utils/exportUtils/PlainTextExport.ts:78:18) at TestablePlainTextExporter.plainTextForEvent [as createOutput] (src/utils/exportUtils/PlainTextExport.ts:117:45) at TestablePlainTextExporter.createOutput [as testCreateOutput] (test/unit-tests/utils/exportUtils/PlainTextExport-test.ts:18:21) at testCreateOutput (test/unit-tests/utils/exportUtils/PlainTextExport-test.ts:61:39)

Check failure on line 305 in src/utils/exportUtils/Exporter.ts

GitHub Actions / Jest (1)

PlainTextExport › should return text with 24 hr time format

TypeError: Cannot read properties of undefined (reading 'Image') at TestablePlainTextExporter.Image [as isAttachment] (src/utils/exportUtils/Exporter.ts:305:55) at TestablePlainTextExporter.isAttachment [as plainTextForEvent] (src/utils/exportUtils/PlainTextExport.ts:78:18) at TestablePlainTextExporter.plainTextForEvent [as createOutput] (src/utils/exportUtils/PlainTextExport.ts:117:45) at TestablePlainTextExporter.createOutput [as testCreateOutput] (test/unit-tests/utils/exportUtils/PlainTextExport-test.ts:18:21) at testCreateOutput (test/unit-tests/utils/exportUtils/PlainTextExport-test.ts:61:39)
Copy link
Author

@kenwuuu kenwuuu Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't change "m.sticker" here because it makes getContext on the next line angry, and I want to keep my first PR small and light.

return mxEv.getType() === attachmentTypes[0] || attachmentTypes.includes(mxEv.getContent().msgtype!);
}


Unchanged files with check annotations Beta

expect(exporter.destinationFileName).toMatchSnapshot();
});
it("should export", async () => {

Check failure on line 193 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should export

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:193:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
const events = [...Array(50)].map<IRoomEvent>((_, i) => ({
event_id: `${i}`,
type: EventType.RoomMessage,
expect(await file.text()).toMatchSnapshot();
});
it("should include the room's avatar", async () => {

Check failure on line 224 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should include the room's avatar

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:224:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
mockMessages(EVENT_MESSAGE);
const mxc = "mxc://www.example.com/avatars/nice-room.jpeg";
expect(await files["room.png"]!.text()).toBe(avatar);
});
it("should include the creation event", async () => {

Check failure on line 249 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should include the creation event

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:249:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
const creator = "@bob:example.com";
mockMessages(EVENT_MESSAGE);
room.currentState.setStateEvents([
expect(await getMessageFile(exporter).text()).toContain(`${creator} created this room.`);
});
it("should include the topic", async () => {

Check failure on line 280 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should include the topic

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:280:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
const topic = ":^-) (-^:";
mockMessages(EVENT_MESSAGE);
room.currentState.setStateEvents([
expect(await getMessageFile(exporter).text()).toContain(`Topic: ${topic}`);
});
it("should include avatars", async () => {

Check failure on line 311 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should include avatars

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:311:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
mockMessages(EVENT_MESSAGE);
jest.spyOn(RoomMember.prototype, "getMxcAvatarUrl").mockReturnValue("mxc://example.org/avatar.bmp");
expect(await file.text()).toContain(EVENT_MESSAGE_NO_SENDER.content.body);
});
it("should handle when events sender cannot be found in room state", async () => {

Check failure on line 371 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should handle when events sender cannot be found in room state

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:371:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
mockMessages(EVENT_MESSAGE);
jest.spyOn(RoomState.prototype, "getSentinelMember").mockReturnValue(null);
expect(await file.text()).toContain(EVENT_MESSAGE.content.body);
});
it("should include attachments", async () => {

Check failure on line 393 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should include attachments

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:393:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
mockMessages(EVENT_MESSAGE, EVENT_ATTACHMENT);
const attachmentBody = "Lorem ipsum dolor sit amet";
expect(text).toBe(attachmentBody);
});
it("should handle attachments with identical names and dates", async () => {

Check failure on line 422 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should handle attachments with identical names and dates

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:422:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
mockMessages(EVENT_MESSAGE, EVENT_ATTACHMENT, EVENT_ATTACHMENT);
const exporter = new HTMLExporter(
expect(duplicateFile).not.toBeUndefined();
});
it("should handle when attachment cannot be fetched", async () => {

Check failure on line 452 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should handle when attachment cannot be fetched

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:452:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
mockMessages(EVENT_MESSAGE, EVENT_ATTACHMENT_MALFORMED, EVENT_ATTACHMENT);
const attachmentBody = "Lorem ipsum dolor sit amet";
expect(await messagesFile.text()).toBeTruthy();
});
it("should handle when attachment srcHttp is falsy", async () => {

Check failure on line 485 in test/unit-tests/utils/exportUtils/HTMLExport-test.ts

GitHub Actions / Jest (2)

HTMLExport › should handle when attachment srcHttp is falsy

thrown: "Exceeded timeout of 5000 ms for a test. Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout." at it (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:485:5) at Object.describe (test/unit-tests/utils/exportUtils/HTMLExport-test.ts:75:1)
mockMessages(EVENT_MESSAGE, EVENT_ATTACHMENT);
const attachmentBody = "Lorem ipsum dolor sit amet";
expect(messages.messages[0].To[0].Address).toEqual(email);
const text = await mailpit.renderMessageText(messages.messages[0].ID);
[, code] = text.match(/Your verification code to confirm this email address is: (\d{6})/);
}).toPass();

Check failure on line 39 in playwright/e2e/oidc/index.ts

GitHub Actions / Run Tests [Chrome] 6/6

[Chrome] › playwright/e2e/oidc/oidc-native.spec.ts:109:9 › OIDC Native › can log in to an existing MAS account @no-firefox @no-webkit @screenshot

1) [Chrome] › playwright/e2e/oidc/oidc-native.spec.ts:109:9 › OIDC Native › can log in to an existing MAS account @no-firefox @no-webkit @screenshot Error: Cannot read properties of undefined (reading 'To') Call Log: - Test timeout of 90000ms exceeded at oidc/index.ts:39 37 | const text = await mailpit.renderMessageText(messages.messages[0].ID); 38 | [, code] = text.match(/Your verification code to confirm this email address is: (\d{6})/); > 39 | }).toPass(); | ^ 40 | 41 | await page.getByRole("textbox", { name: "6-digit code" }).fill(code); 42 | await page.getByRole("button", { name: "Continue" }).click(); at registerAccountMas (/home/runner/work/element-web/element-web/playwright/e2e/oidc/index.ts:39:8) at /home/runner/work/element-web/element-web/playwright/e2e/oidc/oidc-native.spec.ts:115:9
await page.getByRole("textbox", { name: "6-digit code" }).fill(code);
await page.getByRole("button", { name: "Continue" }).click();
/*
Copyright 2024 New Vector Ltd.
Copyright 2023 The Matrix.org Foundation C.I.C.
await page.route("**/_matrix/client/v3/sendToDevice/m.secret.request/**", async (route) => {
await route.fulfill({ json: {} });
await new Promise((f) => setTimeout(f, 1000));
await route.fetch();

Check failure on line 97 in playwright/e2e/crypto/device-verification.spec.ts

GitHub Actions / Run Tests [Chrome] 1/6

[Chrome] › playwright/e2e/crypto/device-verification.spec.ts:122:9 › Device verification › Verify device with QR code during login @no-webkit

3) [Chrome] › playwright/e2e/crypto/device-verification.spec.ts:122:9 › Device verification › Verify device with QR code during login @no-webkit Error: "route.fetch: Test ended." while running route callback. Consider awaiting `await page.unrouteAll({ behavior: 'ignoreErrors' })` before the end of the test to ignore remaining routes in flight. 95 | await route.fulfill({ json: {} }); 96 | await new Promise((f) => setTimeout(f, 1000)); > 97 | await route.fetch(); | ^ 98 | }); 99 | 100 | await logIntoElement(page, credentials); at /home/runner/work/element-web/element-web/playwright/e2e/crypto/device-verification.spec.ts:97:25
});
await logIntoElement(page, credentials);
await expect(async () => {
await tile.hover();
await tile.getByRole("button", { name: "Reply", exact: true }).click();
}).toPass();

Check failure on line 21 in playwright/e2e/audio-player/audio-player.spec.ts

GitHub Actions / Run Tests [Chrome] 1/6

[Chrome] › playwright/e2e/audio-player/audio-player.spec.ts:250:9 › Audio player › should support creating a reply chain with multiple audio files @no-firefox @no-webkit @screenshot

2) [Chrome] › playwright/e2e/audio-player/audio-player.spec.ts:250:9 › Audio player › should support creating a reply chain with multiple audio files @no-firefox @no-webkit @screenshot Error: Test timeout of 30000ms exceeded 19 | await tile.hover(); 20 | await tile.getByRole("button", { name: "Reply", exact: true }).click(); > 21 | }).toPass(); | ^ 22 | }; 23 | 24 | test.describe("Audio player", { tag: ["@no-firefox", "@no-webkit"] }, () => { at clickButtonReply (/home/runner/work/element-web/element-web/playwright/e2e/audio-player/audio-player.spec.ts:21:8) at /home/runner/work/element-web/element-web/playwright/e2e/audio-player/audio-player.spec.ts:274:19
};
test.describe("Audio player", { tag: ["@no-firefox", "@no-webkit"] }, () => {
const zip = await extractZipFileToPath(zipPath, dirPath);
await page.goto(`file://${dirPath}/${Object.keys(zip.files)[0]}/messages.html`);
await expect(page).toMatchScreenshot("html-export.png", {

Check failure on line 126 in playwright/e2e/chat-export/html-export.spec.ts

GitHub Actions / Run Tests [Chrome] 1/6

[Chrome] › playwright/e2e/chat-export/html-export.spec.ts:92:9 › HTML Export › should export html successfully and match screenshot @screenshot

1) [Chrome] › playwright/e2e/chat-export/html-export.spec.ts:92:9 › HTML Export › should export html successfully and match screenshot @screenshot Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(page).toHaveScreenshot(expected) 17433 pixels (ratio 0.02 of all image pixels) are different. Expected: /home/runner/work/element-web/element-web/playwright/snapshots/chat-export/html-export.spec.ts/html-export-linux.png Received: /home/runner/work/element-web/element-web/playwright/test-results/chat-export-html-export-HT-b262f-sfully-and-match-screenshot-Chrome-retry2/html-export-actual.png Diff: /home/runner/work/element-web/element-web/playwright/test-results/chat-export-html-export-HT-b262f-sfully-and-match-screenshot-Chrome-retry2/html-export-diff.png Call log: - expect.toHaveScreenshot(html-export.png) with timeout 5000ms - verifying given screenshot expectation - taking page screenshot - disabled all CSS animations - waiting for fonts to load... - fonts loaded - 17433 pixels (ratio 0.02 of all image pixels) are different. - waiting 100ms before taking screenshot - taking page screenshot - disabled all CSS animations - waiting for fonts to load... - fonts loaded - captured a stable screenshot - 17433 pixels (ratio 0.02 of all image pixels) are different. 124 | const zip = await extractZipFileToPath(zipPath, dirPath); 125 | await page.goto(`file://${dirPath}/${Object.keys(zip.files)[0]}/messages.html`); > 126 | await expect(page).toMatchScreenshot("html-export.png", { | ^ 127 | mask: [ 128 | // We need to mask the whole thing because the width of the time part changes 129 | page.locator(".mx_TimelineSeparator"), at /home/runner/work/element-web/element-web/playwright/e2e/chat-export/html-export.spec.ts:126:32

Check failure on line 126 in playwright/e2e/chat-export/html-export.spec.ts

GitHub Actions / Run Tests [Chrome] 1/6

[Chrome] › playwright/e2e/chat-export/html-export.spec.ts:92:9 › HTML Export › should export html successfully and match screenshot @screenshot

1) [Chrome] › playwright/e2e/chat-export/html-export.spec.ts:92:9 › HTML Export › should export html successfully and match screenshot @screenshot Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(page).toHaveScreenshot(expected) 17433 pixels (ratio 0.02 of all image pixels) are different. Expected: /home/runner/work/element-web/element-web/playwright/snapshots/chat-export/html-export.spec.ts/html-export-linux.png Received: /home/runner/work/element-web/element-web/playwright/test-results/chat-export-html-export-HT-b262f-sfully-and-match-screenshot-Chrome-retry1/html-export-actual.png Diff: /home/runner/work/element-web/element-web/playwright/test-results/chat-export-html-export-HT-b262f-sfully-and-match-screenshot-Chrome-retry1/html-export-diff.png Call log: - expect.toHaveScreenshot(html-export.png) with timeout 5000ms - verifying given screenshot expectation - taking page screenshot - disabled all CSS animations - waiting for fonts to load... - fonts loaded - 17433 pixels (ratio 0.02 of all image pixels) are different. - waiting 100ms before taking screenshot - taking page screenshot - disabled all CSS animations - waiting for fonts to load... - fonts loaded - captured a stable screenshot - 17433 pixels (ratio 0.02 of all image pixels) are different. 124 | const zip = await extractZipFileToPath(zipPath, dirPath); 125 | await page.goto(`file://${dirPath}/${Object.keys(zip.files)[0]}/messages.html`); > 126 | await expect(page).toMatchScreenshot("html-export.png", { | ^ 127 | mask: [ 128 | // We need to mask the whole thing because the width of the time part changes 129 | page.locator(".mx_TimelineSeparator"), at /home/runner/work/element-web/element-web/playwright/e2e/chat-export/html-export.spec.ts:126:32

Check failure on line 126 in playwright/e2e/chat-export/html-export.spec.ts

GitHub Actions / Run Tests [Chrome] 1/6

[Chrome] › playwright/e2e/chat-export/html-export.spec.ts:92:9 › HTML Export › should export html successfully and match screenshot @screenshot

1) [Chrome] › playwright/e2e/chat-export/html-export.spec.ts:92:9 › HTML Export › should export html successfully and match screenshot @screenshot Error: expect(page).toHaveScreenshot(expected) 17433 pixels (ratio 0.02 of all image pixels) are different. Expected: /home/runner/work/element-web/element-web/playwright/snapshots/chat-export/html-export.spec.ts/html-export-linux.png Received: /home/runner/work/element-web/element-web/playwright/test-results/chat-export-html-export-HT-b262f-sfully-and-match-screenshot-Chrome/html-export-actual.png Diff: /home/runner/work/element-web/element-web/playwright/test-results/chat-export-html-export-HT-b262f-sfully-and-match-screenshot-Chrome/html-export-diff.png Call log: - expect.toHaveScreenshot(html-export.png) with timeout 5000ms - verifying given screenshot expectation - taking page screenshot - disabled all CSS animations - waiting for fonts to load... - fonts loaded - 17433 pixels (ratio 0.02 of all image pixels) are different. - waiting 100ms before taking screenshot - taking page screenshot - disabled all CSS animations - waiting for fonts to load... - fonts loaded - captured a stable screenshot - 17433 pixels (ratio 0.02 of all image pixels) are different. 124 | const zip = await extractZipFileToPath(zipPath, dirPath); 125 | await page.goto(`file://${dirPath}/${Object.keys(zip.files)[0]}/messages.html`); > 126 | await expect(page).toMatchScreenshot("html-export.png", { | ^ 127 | mask: [ 128 | // We need to mask the whole thing because the width of the time part changes 129 | page.locator(".mx_TimelineSeparator"), at /home/runner/work/element-web/element-web/playwright/e2e/chat-export/html-export.spec.ts:126:32
mask: [
// We need to mask the whole thing because the width of the time part changes
page.locator(".mx_TimelineSeparator"),