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

Commit 2a326b0

Browse files
committed
Support MSC3946 in RoomCreate tile
1 parent 35e64fc commit 2a326b0

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

src/components/views/messages/RoomCreate.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import EventTileBubble from "./EventTileBubble";
2828
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
2929
import RoomContext from "../../../contexts/RoomContext";
3030
import { useRoomState } from "../../../hooks/useRoomState";
31+
import SettingsStore from "../../../settings/SettingsStore";
3132

3233
interface IProps {
3334
/** The m.room.create MatrixEvent that this tile represents */
@@ -40,14 +41,19 @@ interface IProps {
4041
* room.
4142
*/
4243
export const RoomCreate: React.FC<IProps> = ({ mxEvent, timestamp }) => {
44+
const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors");
45+
4346
// Note: we ask the room for its predecessor here, instead of directly using
4447
// the information inside mxEvent. This allows us the flexibility later to
4548
// use a different predecessor (e.g. through MSC3946) and still display it
4649
// in the timeline location of the create event.
4750
const roomContext = useContext(RoomContext);
4851
const predecessor = useRoomState(
4952
roomContext.room,
50-
useCallback((state) => state.findPredecessor(), []),
53+
useCallback(
54+
(state) => state.findPredecessor(msc3946ProcessDynamicPredecessor),
55+
[msc3946ProcessDynamicPredecessor],
56+
),
5157
);
5258

5359
const onLinkClicked = useCallback(

test/components/views/messages/RoomCreate-test.tsx

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,35 @@ describe("<RoomCreate />", () => {
5252
content: {},
5353
event_id: "$create",
5454
});
55+
const predecessorEvent = new MatrixEvent({
56+
type: EventType.RoomPredecessor,
57+
state_key: "",
58+
sender: userId,
59+
room_id: roomId,
60+
content: {
61+
predecessor_room_id: "old_room_id_from_predecessor",
62+
},
63+
event_id: "$create",
64+
});
65+
const predecessorEventWithEventId = new MatrixEvent({
66+
type: EventType.RoomPredecessor,
67+
state_key: "",
68+
sender: userId,
69+
room_id: roomId,
70+
content: {
71+
predecessor_room_id: "old_room_id_from_predecessor",
72+
last_known_event_id: "tombstone_event_id_from_predecessor",
73+
},
74+
event_id: "$create",
75+
});
5576
stubClient();
5677
const client = mocked(MatrixClientPeg.get());
57-
const room = new Room(roomId, client, userId);
58-
upsertRoomStateEvents(room, [createEvent]);
78+
const roomJustCreate = new Room(roomId, client, userId);
79+
upsertRoomStateEvents(roomJustCreate, [createEvent]);
80+
const roomCreateAndPredecessor = new Room(roomId, client, userId);
81+
upsertRoomStateEvents(roomCreateAndPredecessor, [createEvent, predecessorEvent]);
82+
const roomCreateAndPredecessorWithEventId = new Room(roomId, client, userId);
83+
upsertRoomStateEvents(roomCreateAndPredecessorWithEventId, [createEvent, predecessorEventWithEventId]);
5984
const roomNoPredecessors = new Room(roomId, client, userId);
6085
upsertRoomStateEvents(roomNoPredecessors, [createEventWithoutPredecessor]);
6186

@@ -81,12 +106,12 @@ describe("<RoomCreate />", () => {
81106
}
82107

83108
it("Renders as expected", () => {
84-
const roomCreate = renderRoomCreate(room);
109+
const roomCreate = renderRoomCreate(roomJustCreate);
85110
expect(roomCreate.asFragment()).toMatchSnapshot();
86111
});
87112

88113
it("Links to the old version of the room", () => {
89-
renderRoomCreate(room);
114+
renderRoomCreate(roomJustCreate);
90115
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
91116
"href",
92117
"https://matrix.to/#/old_room_id/tombstone_event_id",
@@ -99,7 +124,7 @@ describe("<RoomCreate />", () => {
99124
});
100125

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

105130
await act(() => userEvent.click(link));
@@ -115,4 +140,48 @@ describe("<RoomCreate />", () => {
115140
}),
116141
);
117142
});
143+
144+
it("Ignores m.predecessor if labs flag is off", () => {
145+
renderRoomCreate(roomCreateAndPredecessor);
146+
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
147+
"href",
148+
"https://matrix.to/#/old_room_id/tombstone_event_id",
149+
);
150+
});
151+
152+
describe("When feature_dynamic_room_predecessors = true", () => {
153+
beforeEach(() => {
154+
jest.spyOn(SettingsStore, "getValue").mockImplementation(
155+
(settingName) => settingName === "feature_dynamic_room_predecessors",
156+
);
157+
});
158+
159+
afterEach(() => {
160+
jest.spyOn(SettingsStore, "getValue").mockReset();
161+
});
162+
163+
it("Uses the create event if there is no m.predecessor", () => {
164+
renderRoomCreate(roomJustCreate);
165+
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
166+
"href",
167+
"https://matrix.to/#/old_room_id/tombstone_event_id",
168+
);
169+
});
170+
171+
it("Uses m.predecessor when it's there", () => {
172+
renderRoomCreate(roomCreateAndPredecessor);
173+
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
174+
"href",
175+
"https://matrix.to/#/old_room_id_from_predecessor",
176+
);
177+
});
178+
179+
it("Links to the event in the room if event ID is provided", () => {
180+
renderRoomCreate(roomCreateAndPredecessorWithEventId);
181+
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
182+
"href",
183+
"https://matrix.to/#/old_room_id_from_predecessor/tombstone_event_id_from_predecessor",
184+
);
185+
});
186+
});
118187
});

0 commit comments

Comments
 (0)