Show a tile for an unloaded predecessor room if it has via_servers (#10483)

* Improve typing in constructor of RoomPermalinkCreator

* Provide via servers if present when navigating to predecessor room from Advanced Room Settings

* Show an error tile when the predecessor room is not found

* Test for MatrixToPermalinkConstructor.forRoom

* Test for MatrixToPermalinkConstructor.forEvent

* Display a tile for predecessor event if it contains via servers

* Fix missing case where event id is provided as well as via servers

* Refactor RoomPredecessor tests

* Return lost filterConsole to its home

* Comments for IState in AdvancedRoomSettingsTab

* Explain why we might render a tile even without prevRoom

* Guess the old room's via servers if they are not provided

* Fix TypeScript errors

* Adjust regular expression (hopefully) to avoid potential catastrophic backtracking

* Another attempt at avoiding super-liner regex performance

* Tests for guessServerNameFromRoomId and better implementation

* Further attempt to prevent backtracking

---------

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Andy Balaam 2023-04-12 16:26:45 +01:00 committed by GitHub
parent 075cb9e622
commit c496985ff3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 320 additions and 89 deletions

View file

@ -22,11 +22,14 @@ import { EventType, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import dis from "../../../../src/dispatcher/dispatcher";
import SettingsStore from "../../../../src/settings/SettingsStore";
import { RoomPredecessorTile } from "../../../../src/components/views/messages/RoomPredecessorTile";
import {
guessServerNameFromRoomId,
RoomPredecessorTile,
} from "../../../../src/components/views/messages/RoomPredecessorTile";
import { stubClient, upsertRoomStateEvents } from "../../../test-utils/test-utils";
import { Action } from "../../../../src/dispatcher/actions";
import RoomContext from "../../../../src/contexts/RoomContext";
import { getRoomContext } from "../../../test-utils";
import { filterConsole, getRoomContext } from "../../../test-utils";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
jest.mock("../../../../src/dispatcher/dispatcher");
@ -34,55 +37,56 @@ jest.mock("../../../../src/dispatcher/dispatcher");
describe("<RoomPredecessorTile />", () => {
const userId = "@alice:server.org";
const roomId = "!room:server.org";
const createEvent = new MatrixEvent({
type: EventType.RoomCreate,
state_key: "",
sender: userId,
room_id: roomId,
content: {
predecessor: { room_id: "old_room_id", event_id: "tombstone_event_id" },
},
event_id: "$create",
});
const createEventWithoutPredecessor = new MatrixEvent({
type: EventType.RoomCreate,
state_key: "",
sender: userId,
room_id: roomId,
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: "tombstone_event_id_from_predecessor",
},
event_id: "$create",
});
stubClient();
const client = mocked(MatrixClientPeg.get());
const roomJustCreate = new Room(roomId, client, userId);
upsertRoomStateEvents(roomJustCreate, [createEvent]);
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]);
function makeRoom({
createEventHasPredecessor = false,
predecessorEventExists = false,
predecessorEventHasEventId = false,
predecessorEventHasViaServers = false,
}): Room {
const room = new Room(roomId, client, userId);
const createInfo = {
type: EventType.RoomCreate,
state_key: "",
sender: userId,
room_id: roomId,
content: {},
event_id: "$create",
};
if (createEventHasPredecessor) {
createInfo.content = {
predecessor: { room_id: "old_room_id", event_id: "$tombstone_event_id" },
};
}
const createEvent = new MatrixEvent(createInfo);
upsertRoomStateEvents(room, [createEvent]);
if (predecessorEventExists) {
const predecessorInfo = {
type: EventType.RoomPredecessor,
state_key: "",
sender: userId,
room_id: roomId,
content: {
predecessor_room_id: "old_room_id_from_predecessor",
last_known_event_id: predecessorEventHasEventId
? "$tombstone_event_id_from_predecessor"
: undefined,
via_servers: predecessorEventHasViaServers ? ["a.example.com", "b.example.com"] : undefined,
},
event_id: "$predecessor",
};
const predecessorEvent = new MatrixEvent(predecessorInfo);
upsertRoomStateEvents(room, [predecessorEvent]);
}
return room;
}
beforeEach(() => {
jest.clearAllMocks();
@ -98,6 +102,10 @@ describe("<RoomPredecessorTile />", () => {
});
function renderTile(room: Room) {
// Find this room's create event (it should have one!)
const createEvent = room.currentState.getStateEvents("m.room.create")[0];
expect(createEvent).toBeTruthy();
return render(
<RoomContext.Provider value={getRoomContext(room, {})}>
<RoomPredecessorTile mxEvent={createEvent} />
@ -106,25 +114,29 @@ describe("<RoomPredecessorTile />", () => {
}
it("Renders as expected", () => {
const roomCreate = renderTile(roomJustCreate);
const roomCreate = renderTile(makeRoom({ createEventHasPredecessor: true }));
expect(roomCreate.asFragment()).toMatchSnapshot();
});
it("Links to the old version of the room", () => {
renderTile(roomJustCreate);
renderTile(makeRoom({ createEventHasPredecessor: true }));
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id/tombstone_event_id",
"https://matrix.to/#/old_room_id/$tombstone_event_id",
);
});
it("Shows an empty div if there is no predecessor", () => {
renderTile(roomNoPredecessors);
expect(screen.queryByText("Click here to see older messages.", { exact: false })).toBeNull();
describe("(filtering warnings about no predecessor)", () => {
filterConsole("RoomPredecessorTile unexpectedly used in a room with no predecessor.");
it("Shows an empty div if there is no predecessor", () => {
renderTile(makeRoom({}));
expect(screen.queryByText("Click here to see older messages.", { exact: false })).toBeNull();
});
});
it("Opens the old room on click", async () => {
renderTile(roomJustCreate);
renderTile(makeRoom({ createEventHasPredecessor: true }));
const link = screen.getByText("Click here to see older messages.");
await act(() => userEvent.click(link));
@ -132,7 +144,7 @@ describe("<RoomPredecessorTile />", () => {
await waitFor(() =>
expect(dis.dispatch).toHaveBeenCalledWith({
action: Action.ViewRoom,
event_id: "tombstone_event_id",
event_id: "$tombstone_event_id",
highlighted: true,
room_id: "old_room_id",
metricsTrigger: "Predecessor",
@ -142,13 +154,26 @@ describe("<RoomPredecessorTile />", () => {
});
it("Ignores m.predecessor if labs flag is off", () => {
renderTile(roomCreateAndPredecessor);
renderTile(makeRoom({ createEventHasPredecessor: true, predecessorEventExists: true }));
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id/tombstone_event_id",
"https://matrix.to/#/old_room_id/$tombstone_event_id",
);
});
describe("If the predecessor room is not found", () => {
filterConsole("Failed to find predecessor room with id old_room_id");
beforeEach(() => {
mocked(MatrixClientPeg.get().getRoom).mockReturnValue(null);
});
it("Shows an error if there are no via servers", () => {
renderTile(makeRoom({ createEventHasPredecessor: true, predecessorEventExists: true }));
expect(screen.getByText("Can't find the old version of this room", { exact: false })).toBeInTheDocument();
});
});
describe("When feature_dynamic_room_predecessors = true", () => {
beforeEach(() => {
jest.spyOn(SettingsStore, "getValue").mockImplementation(
@ -161,15 +186,15 @@ describe("<RoomPredecessorTile />", () => {
});
it("Uses the create event if there is no m.predecessor", () => {
renderTile(roomJustCreate);
renderTile(makeRoom({ createEventHasPredecessor: true }));
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id/tombstone_event_id",
"https://matrix.to/#/old_room_id/$tombstone_event_id",
);
});
it("Uses m.predecessor when it's there", () => {
renderTile(roomCreateAndPredecessor);
renderTile(makeRoom({ createEventHasPredecessor: true, predecessorEventExists: true }));
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id_from_predecessor",
@ -177,11 +202,91 @@ describe("<RoomPredecessorTile />", () => {
});
it("Links to the event in the room if event ID is provided", () => {
renderTile(roomCreateAndPredecessorWithEventId);
renderTile(
makeRoom({
createEventHasPredecessor: true,
predecessorEventExists: true,
predecessorEventHasEventId: true,
}),
);
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id_from_predecessor/tombstone_event_id_from_predecessor",
"https://matrix.to/#/old_room_id_from_predecessor/$tombstone_event_id_from_predecessor",
);
});
describe("If the predecessor room is not found", () => {
filterConsole("Failed to find predecessor room with id old_room_id");
beforeEach(() => {
mocked(MatrixClientPeg.get().getRoom).mockReturnValue(null);
});
it("Shows an error if there are no via servers", () => {
renderTile(makeRoom({ createEventHasPredecessor: true, predecessorEventExists: true }));
expect(
screen.getByText("Can't find the old version of this room", { exact: false }),
).toBeInTheDocument();
});
it("Shows a tile if there are via servers", () => {
renderTile(
makeRoom({
createEventHasPredecessor: true,
predecessorEventExists: true,
predecessorEventHasViaServers: true,
}),
);
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id_from_predecessor?via=a.example.com&via=b.example.com",
);
});
it("Shows a tile linking to an event if there are via servers", () => {
renderTile(
makeRoom({
createEventHasPredecessor: true,
predecessorEventExists: true,
predecessorEventHasEventId: true,
predecessorEventHasViaServers: true,
}),
);
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
"href",
"https://matrix.to/#/old_room_id_from_predecessor/$tombstone_event_id_from_predecessor?via=a.example.com&via=b.example.com",
);
});
});
});
});
describe("guessServerNameFromRoomId", () => {
it("Extracts the domain name from a standard room ID", () => {
expect(guessServerNameFromRoomId("!436456:example.com")).toEqual("example.com");
});
it("Extracts the domain name and port when included", () => {
expect(guessServerNameFromRoomId("!436456:example.com:8888")).toEqual("example.com:8888");
});
it("Handles an IPv4 address for server name", () => {
expect(guessServerNameFromRoomId("!436456:127.0.0.1")).toEqual("127.0.0.1");
});
it("Handles an IPv4 address and port", () => {
expect(guessServerNameFromRoomId("!436456:127.0.0.1:81")).toEqual("127.0.0.1:81");
});
it("Handles an IPv6 address for server name", () => {
expect(guessServerNameFromRoomId("!436456:::1")).toEqual("::1");
});
it("Handles an IPv6 address and port", () => {
expect(guessServerNameFromRoomId("!436456:::1:8080")).toEqual("::1:8080");
});
it("Returns null when the room ID contains no colon", () => {
expect(guessServerNameFromRoomId("!436456")).toBeNull();
});
});

View file

@ -14,7 +14,7 @@ exports[`<RoomPredecessorTile /> Renders as expected 1`] = `
class="mx_EventTileBubble_subtitle"
>
<a
href="https://matrix.to/#/old_room_id/tombstone_event_id"
href="https://matrix.to/#/old_room_id/$tombstone_event_id"
>
Click here to see older messages.
</a>

View file

@ -47,7 +47,7 @@ describe("AdvancedRoomSettingsTab", () => {
mocked(dis.dispatch).mockReset();
mocked(room.findPredecessor).mockImplementation((msc3946: boolean) =>
msc3946
? { roomId: "old_room_id_via_predecessor" }
? { roomId: "old_room_id_via_predecessor", viaServers: ["one.example.com", "two.example.com"] }
: { roomId: "old_room_id", eventId: "tombstone_event_id" },
);
});
@ -138,6 +138,7 @@ describe("AdvancedRoomSettingsTab", () => {
action: Action.ViewRoom,
event_id: undefined,
room_id: "old_room_id_via_predecessor",
via_servers: ["one.example.com", "two.example.com"],
metricsTrigger: "WebPredecessorSettings",
metricsViaKeyboard: false,
});