Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Support MSC3946 in RoomCreate tile
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Feb 1, 2023
1 parent ab1f6b6 commit 37b8c28
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/components/views/messages/RoomCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import EventTileBubble from "./EventTileBubble";
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
import RoomContext from "../../../contexts/RoomContext";
import { useRoomState } from "../../../hooks/useRoomState";
import SettingsStore from "../../../settings/SettingsStore";

interface IProps {
/** The m.room.create MatrixEvent that this tile represents */
Expand All @@ -40,14 +41,19 @@ interface IProps {
* room.
*/
export const RoomCreate: React.FC<IProps> = ({ mxEvent, timestamp }) => {
const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors");

// Note: we ask the room for its predecessor here, instead of directly using
// the information inside mxEvent. This allows us the flexibility later to
// use a different predecessor (e.g. through MSC3946) and still display it
// in the timeline location of the create event.
const roomContext = useContext(RoomContext);
const predecessor = useRoomState(
roomContext.room,
useCallback((state) => state.findPredecessor(), []),
useCallback(
(state) => state.findPredecessor(msc3946ProcessDynamicPredecessor),
[msc3946ProcessDynamicPredecessor],
),
);

const onLinkClicked = useCallback(
Expand Down
82 changes: 77 additions & 5 deletions test/components/views/messages/RoomCreate-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,37 @@ describe("<RoomCreate />", () => {
content: {},
event_id: "$create",
});
const predecessorEvent = new MatrixEvent({
type: EventType.RoomPredecessor,
state_key: "",
sender: userId,
room_id: roomId,
content: {
predecessor_room_id: "old_room_id_from_predecessor",
},
event_id: "$create",
});
const predecessorEventWithEventId = new MatrixEvent({
type: EventType.RoomPredecessor,
state_key: "",
sender: userId,
room_id: roomId,
content: {
predecessor_room_id: "old_room_id_from_predecessor",
last_known_event_id: "old_tombstone_id",
},
event_id: "$create",
});
stubClient();
const client = mocked(MatrixClientPeg.get());
const room = new Room(roomId, client, userId);
upsertRoomStateEvents(room, [createEvent]);
const roomJustCreate = new Room(roomId, client, userId);
upsertRoomStateEvents(roomJustCreate, [createEvent]);
const roomNoPredecessors = new Room(roomId, client, userId);
upsertRoomStateEvents(roomNoPredecessors, [createEventWithoutPredecessor]);
const roomCreateAndPredecessor = new Room(roomId, client, userId);
upsertRoomStateEvents(roomCreateAndPredecessor, [createEvent, predecessorEvent]);
const roomCreateAndPredecessorWithEventId = new Room(roomId, client, userId);
upsertRoomStateEvents(roomCreateAndPredecessorWithEventId, [createEvent, predecessorEventWithEventId]);
const roomNoPredecessors = new Room(roomId, client, userId);
upsertRoomStateEvents(roomNoPredecessors, [createEventWithoutPredecessor]);

Expand All @@ -81,12 +108,12 @@ describe("<RoomCreate />", () => {
}

it("Renders as expected", () => {
const roomCreate = renderRoomCreate(room);
const roomCreate = renderRoomCreate(roomJustCreate);
expect(roomCreate.asFragment()).toMatchSnapshot();
});

it("Links to the old version of the room", () => {
renderRoomCreate(room);
renderRoomCreate(roomJustCreate);
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id/tombstone_event_id",
Expand All @@ -99,7 +126,7 @@ describe("<RoomCreate />", () => {
});

it("Opens the old room on click", async () => {
renderRoomCreate(room);
renderRoomCreate(roomJustCreate);
const link = screen.getByText("Click here to see older messages.");

await act(() => userEvent.click(link));
Expand All @@ -115,4 +142,49 @@ describe("<RoomCreate />", () => {
}),
);
});

it("Ignores m.predecessor if labs flag is off", () => {
renderRoomCreate(roomCreateAndPredecessor);
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id/tombstone_event_id",
);
});

describe("When feature_dynamic_room_predecessors = true", () => {
beforeEach(() => {
jest.spyOn(SettingsStore, "getValue").mockImplementation(
(settingName) => settingName === "feature_dynamic_room_predecessors",
);
});

afterEach(() => {
jest.spyOn(SettingsStore, "getValue").mockReset();
});

it("Uses the create event if there is no m.predecessor", () => {
renderRoomCreate(roomJustCreate);
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id/tombstone_event_id",
);
});

it("Uses m.predecessor when it's there", () => {
renderRoomCreate(roomCreateAndPredecessor);
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id_from_predecessor",
);
});

/* TODO: enable when findPredecessor looks for this attribute
it("Links to the event in the room if event ID is provided", () => {
renderRoomCreate(roomCreateAndPredecessorWithEventId);
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id_from_predecessor/last_known_event_id",
);
});*/
});
});

0 comments on commit 37b8c28

Please sign in to comment.