Room list: move MessagePreviewStore and previews into its own directory (#32710)

* refactor: move `MessagePreviewStore` and previews into its own directory

The `MessagePreviewStore` is used widly and not only by the room list.
Moving to its own folder to be able to remove old room list later with
less friction

* test: add more tests
This commit is contained in:
Florian Duros
2026-03-04 17:40:12 +00:00
committed by GitHub
parent 1c66f0ba01
commit 1963f268aa
26 changed files with 412 additions and 27 deletions
@@ -42,7 +42,7 @@ import { TestSdkContext } from "../../../TestSdkContext";
import { SDKContext } from "../../../../../src/contexts/SDKContext";
import { shouldShowComponent } from "../../../../../src/customisations/helpers/UIComponents";
import { UIComponent } from "../../../../../src/settings/UIFeature";
import { MessagePreviewStore } from "../../../../../src/stores/room-list/MessagePreviewStore";
import { MessagePreviewStore } from "../../../../../src/stores/message-preview";
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
import SettingsStore from "../../../../../src/settings/SettingsStore";
import { ConnectionState } from "../../../../../src/models/Call";
@@ -18,7 +18,7 @@ import {
Room,
} from "matrix-js-sdk/src/matrix";
import { MessagePreviewStore } from "../../../../src/stores/room-list/MessagePreviewStore";
import { MessagePreviewStore } from "../../../../src/stores/message-preview";
import { mkEvent, mkMessage, mkReaction, setupAsyncStoreWithClient, stubClient } from "../../../test-utils";
import { DefaultTagID } from "../../../../src/stores/room-list-v3/skip-list/tag";
import { mkThread } from "../../../test-utils/threads";
@@ -0,0 +1,79 @@
/*
* Copyright 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import { Room } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { LegacyCallAnswerEventPreview } from "../../../../../src/stores/message-preview/previews/LegacyCallAnswerEventPreview";
import { DefaultTagID } from "../../../../../src/stores/room-list-v3/skip-list/tag";
import { mkEvent, stubClient } from "../../../../test-utils";
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
describe("LegacyCallAnswerEventPreview", () => {
const preview = new LegacyCallAnswerEventPreview();
const roomId = "!room:example.com";
beforeAll(() => {
stubClient();
});
describe("getTextFor", () => {
describe("in a room that should be prefixed (non-DM)", () => {
// Default stub: getRoom returns null → shouldPrefixMessagesIn returns true
it("returns 'You joined the call' when the event is from self", () => {
const selfUserId = MatrixClientPeg.safeGet().getSafeUserId();
const event = mkEvent({
event: true,
type: "m.call.answer",
content: {},
user: selfUserId,
room: roomId,
});
expect(preview.getTextFor(event)).toBe("You joined the call");
});
it("returns '<sender> joined the call' when the event is from someone else", () => {
const otherUserId = "@other:example.com";
const event = mkEvent({
event: true,
type: "m.call.answer",
content: {},
user: otherUserId,
room: roomId,
});
expect(preview.getTextFor(event)).toBe(`${otherUserId} joined the call`);
});
});
describe("in a DM room (should not be prefixed)", () => {
beforeEach(() => {
const cli = MatrixClientPeg.safeGet();
// Make a 1:1 room so shouldPrefixMessagesIn returns false
const room = new Room(roomId, cli, cli.getSafeUserId());
jest.spyOn(room.currentState, "getJoinedMemberCount").mockReturnValue(2);
mocked(cli.getRoom).mockReturnValue(room);
});
afterEach(() => {
mocked(MatrixClientPeg.safeGet().getRoom).mockReturnValue(null);
});
it("returns 'Call in progress' regardless of sender", () => {
const otherUserId = "@other:example.com";
const event = mkEvent({
event: true,
type: "m.call.answer",
content: {},
user: otherUserId,
room: roomId,
});
expect(preview.getTextFor(event, DefaultTagID.DM)).toBe("Call in progress");
});
});
});
});
@@ -0,0 +1,77 @@
/*
* Copyright 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import { Room } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { LegacyCallHangupEvent } from "../../../../../src/stores/message-preview/previews/LegacyCallHangupEvent";
import { DefaultTagID } from "../../../../../src/stores/room-list-v3/skip-list/tag";
import { mkEvent, stubClient } from "../../../../test-utils";
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
describe("LegacyCallHangupEvent", () => {
const preview = new LegacyCallHangupEvent();
const roomId = "!room:example.com";
beforeAll(() => {
stubClient();
});
describe("getTextFor", () => {
describe("in a room that should be prefixed (non-DM)", () => {
it("returns 'You ended the call' when the event is from self", () => {
const selfUserId = MatrixClientPeg.safeGet().getSafeUserId();
const event = mkEvent({
event: true,
type: "m.call.hangup",
content: {},
user: selfUserId,
room: roomId,
});
expect(preview.getTextFor(event)).toBe("You ended the call");
});
it("returns '<sender> ended the call' when the event is from someone else", () => {
const otherUserId = "@other:example.com";
const event = mkEvent({
event: true,
type: "m.call.hangup",
content: {},
user: otherUserId,
room: roomId,
});
expect(preview.getTextFor(event)).toBe(`${otherUserId} ended the call`);
});
});
describe("in a DM room (should not be prefixed)", () => {
beforeEach(() => {
const cli = MatrixClientPeg.safeGet();
// Make a 1:1 room so shouldPrefixMessagesIn returns false
const room = new Room(roomId, cli, cli.getSafeUserId());
jest.spyOn(room.currentState, "getJoinedMemberCount").mockReturnValue(2);
mocked(cli.getRoom).mockReturnValue(room);
});
afterEach(() => {
mocked(MatrixClientPeg.safeGet().getRoom).mockReturnValue(null);
});
it("returns 'Call ended' regardless of sender", () => {
const otherUserId = "@other:example.com";
const event = mkEvent({
event: true,
type: "m.call.hangup",
content: {},
user: otherUserId,
room: roomId,
});
expect(preview.getTextFor(event, DefaultTagID.DM)).toBe("Call ended");
});
});
});
});
@@ -0,0 +1,89 @@
/*
* Copyright 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import { Room } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { LegacyCallInviteEventPreview } from "../../../../../src/stores/message-preview/previews/LegacyCallInviteEventPreview";
import { DefaultTagID } from "../../../../../src/stores/room-list-v3/skip-list/tag";
import { mkEvent, stubClient } from "../../../../test-utils";
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
describe("LegacyCallInviteEventPreview", () => {
const preview = new LegacyCallInviteEventPreview();
const roomId = "!room:example.com";
beforeAll(() => {
stubClient();
});
describe("getTextFor", () => {
describe("in a room that should be prefixed (non-DM)", () => {
it("returns 'You started a call' when the event is from self", () => {
const selfUserId = MatrixClientPeg.safeGet().getSafeUserId();
const event = mkEvent({
event: true,
type: "m.call.invite",
content: {},
user: selfUserId,
room: roomId,
});
expect(preview.getTextFor(event)).toBe("You started a call");
});
it("returns '<sender> started a call' when the event is from someone else", () => {
const otherUserId = "@other:example.com";
const event = mkEvent({
event: true,
type: "m.call.invite",
content: {},
user: otherUserId,
room: roomId,
});
expect(preview.getTextFor(event)).toBe(`${otherUserId} started a call`);
});
});
describe("in a DM room (should not be prefixed)", () => {
beforeEach(() => {
const cli = MatrixClientPeg.safeGet();
// Make a 1:1 room so shouldPrefixMessagesIn returns false
const room = new Room(roomId, cli, cli.getSafeUserId());
jest.spyOn(room.currentState, "getJoinedMemberCount").mockReturnValue(2);
mocked(cli.getRoom).mockReturnValue(room);
});
afterEach(() => {
mocked(MatrixClientPeg.safeGet().getRoom).mockReturnValue(null);
});
it("returns 'Waiting for answer' when the event is from self", () => {
const selfUserId = MatrixClientPeg.safeGet().getSafeUserId();
const event = mkEvent({
event: true,
type: "m.call.invite",
content: {},
user: selfUserId,
room: roomId,
});
expect(preview.getTextFor(event, DefaultTagID.DM)).toBe("Waiting for answer");
});
it("returns '<sender> is calling' when the event is from someone else", () => {
const otherUserId = "@other:example.com";
const event = mkEvent({
event: true,
type: "m.call.invite",
content: {},
user: otherUserId,
room: roomId,
});
expect(preview.getTextFor(event, DefaultTagID.DM)).toBe(`${otherUserId} is calling`);
});
});
});
});
@@ -8,7 +8,8 @@ Please see LICENSE files in the repository root for full details.
import { RelationType } from "matrix-js-sdk/src/matrix";
import { MessageEventPreview } from "../../../../../src/stores/room-list/previews/MessageEventPreview";
// Import directly from the file to avoid circular dependencies with MessagePreviewStore
import { MessageEventPreview } from "../../../../../src/stores/message-preview/previews/MessageEventPreview";
import { mkEvent, stubClient } from "../../../../test-utils";
describe("MessageEventPreview", () => {
@@ -8,7 +8,8 @@ Please see LICENSE files in the repository root for full details.
import { type MatrixClient } from "matrix-js-sdk/src/matrix";
import { PollStartEventPreview } from "../../../../../src/stores/room-list/previews/PollStartEventPreview";
// Import directly from the file to avoid circular dependencies with MessagePreviewStore
import { PollStartEventPreview } from "../../../../../src/stores/message-preview/previews/PollStartEventPreview";
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
import { makePollStartEvent } from "../../../../test-utils";
@@ -10,7 +10,8 @@ import { RelationType, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { mkEvent, stubClient } from "../../../../test-utils";
import { ReactionEventPreview } from "../../../../../src/stores/room-list/previews/ReactionEventPreview";
// Import directly from the file to avoid circular dependencies with MessagePreviewStore
import { ReactionEventPreview } from "../../../../../src/stores/message-preview/previews/ReactionEventPreview";
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
describe("ReactionEventPreview", () => {
@@ -0,0 +1,115 @@
/*
* Copyright 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import { Room } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { StickerEventPreview } from "../../../../../src/stores/message-preview/previews/StickerEventPreview";
import { DefaultTagID } from "../../../../../src/stores/room-list-v3/skip-list/tag";
import { mkEvent, stubClient } from "../../../../test-utils";
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
describe("StickerEventPreview", () => {
const preview = new StickerEventPreview();
const roomId = "!room:example.com";
beforeAll(() => {
stubClient();
});
describe("getTextFor", () => {
it("returns null when the event has no body", () => {
const event = mkEvent({
event: true,
type: "m.sticker",
content: {},
user: "@other:example.com",
room: roomId,
});
expect(preview.getTextFor(event)).toBeNull();
});
it("returns null when the body is an empty string", () => {
const event = mkEvent({
event: true,
type: "m.sticker",
content: { body: "" },
user: "@other:example.com",
room: roomId,
});
expect(preview.getTextFor(event)).toBeNull();
});
describe("in a room that should be prefixed (non-DM)", () => {
// Default stub: getRoom returns null → shouldPrefixMessagesIn returns true
it("returns '<sender>: <stickerName>' when the event is from someone else", () => {
const otherUserId = "@other:example.com";
const event = mkEvent({
event: true,
type: "m.sticker",
content: { body: "wave" },
user: otherUserId,
room: roomId,
});
expect(preview.getTextFor(event)).toBe(`${otherUserId}: wave`);
});
it("returns just the sticker name when the event is from self", () => {
const selfUserId = MatrixClientPeg.safeGet().getSafeUserId();
const event = mkEvent({
event: true,
type: "m.sticker",
content: { body: "wave" },
user: selfUserId,
room: roomId,
});
expect(preview.getTextFor(event)).toBe("wave");
});
});
describe("in a DM room (should not be prefixed)", () => {
beforeEach(() => {
const cli = MatrixClientPeg.safeGet();
// Make a 1:1 room so shouldPrefixMessagesIn returns false
const room = new Room(roomId, cli, cli.getSafeUserId());
jest.spyOn(room.currentState, "getJoinedMemberCount").mockReturnValue(2);
mocked(cli.getRoom).mockReturnValue(room);
});
afterEach(() => {
mocked(MatrixClientPeg.safeGet().getRoom).mockReturnValue(null);
});
it("returns just the sticker name regardless of sender", () => {
const otherUserId = "@other:example.com";
const event = mkEvent({
event: true,
type: "m.sticker",
content: { body: "wave" },
user: otherUserId,
room: roomId,
});
expect(preview.getTextFor(event, DefaultTagID.DM)).toBe("wave");
});
});
describe("in a thread", () => {
it("returns just the sticker name regardless of sender", () => {
const otherUserId = "@other:example.com";
const event = mkEvent({
event: true,
type: "m.sticker",
content: { body: "wave" },
user: otherUserId,
room: roomId,
});
expect(preview.getTextFor(event, undefined, true)).toBe("wave");
});
});
});
});
@@ -19,7 +19,7 @@ import { createTestClient, flushPromises } from "../../test-utils";
import { RoomNotificationState } from "../../../src/stores/notifications/RoomNotificationState";
import { RoomNotificationStateStore } from "../../../src/stores/notifications/RoomNotificationStateStore";
import { NotificationStateEvents } from "../../../src/stores/notifications/NotificationState";
import { type MessagePreview, MessagePreviewStore } from "../../../src/stores/room-list/MessagePreviewStore";
import { type MessagePreview, MessagePreviewStore } from "../../../src/stores/message-preview";
import { UPDATE_EVENT } from "../../../src/stores/AsyncStore";
import SettingsStore from "../../../src/settings/SettingsStore";
import DMRoomMap from "../../../src/utils/DMRoomMap";