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

Commit 15edcaa

Browse files
committed
Display a tile for predecessor event if it contains via servers
1 parent a6c4f4c commit 15edcaa

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

src/components/views/messages/RoomPredecessorTile.tsx

+31-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18-
import React, { useCallback, useContext } from "react";
18+
import React, { ReactElement, useCallback, useContext } from "react";
1919
import { logger } from "matrix-js-sdk/src/logger";
2020
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
2121

@@ -29,6 +29,8 @@ import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
2929
import RoomContext from "../../../contexts/RoomContext";
3030
import { useRoomState } from "../../../hooks/useRoomState";
3131
import SettingsStore from "../../../settings/SettingsStore";
32+
import { Room } from "matrix-js-sdk/src/matrix";
33+
import MatrixToPermalinkConstructor from "../../../utils/permalinks/MatrixToPermalinkConstructor";
3234

3335
interface IProps {
3436
/** The m.room.create MatrixEvent that this tile represents */
@@ -86,7 +88,10 @@ export const RoomPredecessorTile: React.FC<IProps> = ({ mxEvent, timestamp }) =>
8688
}
8789

8890
const prevRoom = MatrixClientPeg.get().getRoom(predecessor.roomId);
89-
if (!prevRoom) {
91+
92+
// We need either the previous room, or some servers to find it with.
93+
// Otherwise, we must bail out here
94+
if (!prevRoom && !predecessor.viaServers) {
9095
logger.warn(`Failed to find predecessor room with id ${predecessor.roomId}`);
9196
return (
9297
<EventTileBubble
@@ -104,14 +109,11 @@ export const RoomPredecessorTile: React.FC<IProps> = ({ mxEvent, timestamp }) =>
104109
</EventTileBubble>
105110
);
106111
}
107-
const permalinkCreator = new RoomPermalinkCreator(prevRoom, predecessor.roomId);
108-
permalinkCreator.load();
109-
let predecessorPermalink: string;
110-
if (predecessor.eventId) {
111-
predecessorPermalink = permalinkCreator.forEvent(predecessor.eventId);
112-
} else {
113-
predecessorPermalink = permalinkCreator.forRoom();
114-
}
112+
113+
const predecessorPermalink = prevRoom
114+
? createLinkWithRoom(prevRoom, predecessor.roomId, predecessor.eventId)
115+
: createLinkWithoutRoom(predecessor.roomId, predecessor.viaServers);
116+
115117
const link = (
116118
<a href={predecessorPermalink} onClick={onLinkClicked}>
117119
{_t("Click here to see older messages.")}
@@ -126,4 +128,23 @@ export const RoomPredecessorTile: React.FC<IProps> = ({ mxEvent, timestamp }) =>
126128
timestamp={timestamp}
127129
/>
128130
);
131+
132+
function createLinkWithRoom(room: Room, roomId: string, eventId?: string): string {
133+
const permalinkCreator = new RoomPermalinkCreator(room, roomId);
134+
permalinkCreator.load();
135+
if (eventId) {
136+
return permalinkCreator.forEvent(eventId);
137+
} else {
138+
return permalinkCreator.forRoom();
139+
}
140+
}
141+
142+
function createLinkWithoutRoom(roomId: string, viaServers: string[], eventId?: string): string {
143+
const matrixToPermalinkConstructor = new MatrixToPermalinkConstructor();
144+
if (eventId) {
145+
return matrixToPermalinkConstructor.forEvent(roomId, eventId, viaServers);
146+
} else {
147+
return matrixToPermalinkConstructor.forRoom(roomId, viaServers);
148+
}
149+
}
129150
};

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

+37-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ describe("<RoomPredecessorTile />", () => {
6262
},
6363
event_id: "$create",
6464
});
65+
const viaPredecessorEvent = 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+
via_servers: ["a.example.com", "b.example.com"],
73+
},
74+
event_id: "$create",
75+
});
6576
const predecessorEventWithEventId = new MatrixEvent({
6677
type: EventType.RoomPredecessor,
6778
state_key: "",
@@ -83,6 +94,8 @@ describe("<RoomPredecessorTile />", () => {
8394
upsertRoomStateEvents(roomCreateAndPredecessorWithEventId, [createEvent, predecessorEventWithEventId]);
8495
const roomNoPredecessors = new Room(roomId, client, userId);
8596
upsertRoomStateEvents(roomNoPredecessors, [createEventWithoutPredecessor]);
97+
const roomCreateAndViaPredecessor = new Room(roomId, client, userId);
98+
upsertRoomStateEvents(roomCreateAndViaPredecessor, [createEvent, viaPredecessorEvent]);
8699

87100
beforeEach(() => {
88101
jest.clearAllMocks();
@@ -161,7 +174,7 @@ describe("<RoomPredecessorTile />", () => {
161174
mocked(MatrixClientPeg.get().getRoom).mockReturnValue(null);
162175
});
163176

164-
it("Shows an error", () => {
177+
it("Shows an error if there are no via servers", () => {
165178
renderTile(roomCreateAndPredecessor);
166179
expect(screen.getByText("Can't find the old version of this room", { exact: false })).toBeInTheDocument();
167180
});
@@ -201,5 +214,28 @@ describe("<RoomPredecessorTile />", () => {
201214
"https://matrix.to/#/old_room_id_from_predecessor/tombstone_event_id_from_predecessor",
202215
);
203216
});
217+
218+
describe("If the predecessor room is not found", () => {
219+
filterConsole("Failed to find predecessor room with id old_room_id");
220+
221+
beforeEach(() => {
222+
mocked(MatrixClientPeg.get().getRoom).mockReturnValue(null);
223+
});
224+
225+
it("Shows an error if there are no via servers", () => {
226+
renderTile(roomCreateAndPredecessor);
227+
expect(
228+
screen.getByText("Can't find the old version of this room", { exact: false }),
229+
).toBeInTheDocument();
230+
});
231+
232+
it("Shows a tile if there are via servers", () => {
233+
renderTile(roomCreateAndViaPredecessor);
234+
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
235+
"href",
236+
"https://matrix.to/#/old_room_id_from_predecessor?via=a.example.com&via=b.example.com",
237+
);
238+
});
239+
});
204240
});
205241
});

0 commit comments

Comments
 (0)