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

Commit c496985

Browse files
andybalaamt3chguy
andauthored
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 <[email protected]>
1 parent 075cb9e commit c496985

File tree

8 files changed

+320
-89
lines changed

8 files changed

+320
-89
lines changed

src/components/views/messages/RoomPredecessorTile.tsx

Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
import React, { useCallback, useContext } from "react";
1919
import { logger } from "matrix-js-sdk/src/logger";
2020
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
21+
import { Room } from "matrix-js-sdk/src/matrix";
2122

2223
import dis from "../../../dispatcher/dispatcher";
2324
import { Action } from "../../../dispatcher/actions";
@@ -29,6 +30,7 @@ import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
2930
import RoomContext from "../../../contexts/RoomContext";
3031
import { useRoomState } from "../../../hooks/useRoomState";
3132
import SettingsStore from "../../../settings/SettingsStore";
33+
import MatrixToPermalinkConstructor from "../../../utils/permalinks/MatrixToPermalinkConstructor";
3234

3335
interface IProps {
3436
/** The m.room.create MatrixEvent that this tile represents */
@@ -86,18 +88,56 @@ 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}`);
91-
return <></>;
92-
}
93-
const permalinkCreator = new RoomPermalinkCreator(prevRoom, predecessor.roomId);
94-
permalinkCreator.load();
95-
let predecessorPermalink: string;
96-
if (predecessor.eventId) {
97-
predecessorPermalink = permalinkCreator.forEvent(predecessor.eventId);
98-
} else {
99-
predecessorPermalink = permalinkCreator.forRoom();
96+
97+
const guessedLink = guessLinkForRoomId(predecessor.roomId);
98+
99+
return (
100+
<EventTileBubble
101+
className="mx_CreateEvent"
102+
title={_t("This room is a continuation of another conversation.")}
103+
timestamp={timestamp}
104+
>
105+
<div className="mx_EventTile_body">
106+
<span className="mx_EventTile_tileError">
107+
{!!guessedLink ? (
108+
<>
109+
{_t(
110+
"Can't find the old version of this room (room ID: %(roomId)s), and we have not been " +
111+
"provided with 'via_servers' to look for it. It's possible that guessing the " +
112+
"server from the room ID will work. If you want to try, click this link:",
113+
{
114+
roomId: predecessor.roomId,
115+
},
116+
)}
117+
<a href={guessedLink}>{guessedLink}</a>
118+
</>
119+
) : (
120+
_t(
121+
"Can't find the old version of this room (room ID: %(roomId)s), and we have not been " +
122+
"provided with 'via_servers' to look for it.",
123+
{
124+
roomId: predecessor.roomId,
125+
},
126+
)
127+
)}
128+
</span>
129+
</div>
130+
</EventTileBubble>
131+
);
100132
}
133+
// Otherwise, we expect to be able to find this room either because it is
134+
// already loaded, or because we have via_servers that we can use.
135+
// So we go ahead with rendering the tile.
136+
137+
const predecessorPermalink = prevRoom
138+
? createLinkWithRoom(prevRoom, predecessor.roomId, predecessor.eventId)
139+
: createLinkWithoutRoom(predecessor.roomId, predecessor.viaServers, predecessor.eventId);
140+
101141
const link = (
102142
<a href={predecessorPermalink} onClick={onLinkClicked}>
103143
{_t("Click here to see older messages.")}
@@ -112,4 +152,59 @@ export const RoomPredecessorTile: React.FC<IProps> = ({ mxEvent, timestamp }) =>
112152
timestamp={timestamp}
113153
/>
114154
);
155+
156+
function createLinkWithRoom(room: Room, roomId: string, eventId?: string): string {
157+
const permalinkCreator = new RoomPermalinkCreator(room, roomId);
158+
permalinkCreator.load();
159+
if (eventId) {
160+
return permalinkCreator.forEvent(eventId);
161+
} else {
162+
return permalinkCreator.forRoom();
163+
}
164+
}
165+
166+
function createLinkWithoutRoom(roomId: string, viaServers: string[], eventId?: string): string {
167+
const matrixToPermalinkConstructor = new MatrixToPermalinkConstructor();
168+
if (eventId) {
169+
return matrixToPermalinkConstructor.forEvent(roomId, eventId, viaServers);
170+
} else {
171+
return matrixToPermalinkConstructor.forRoom(roomId, viaServers);
172+
}
173+
}
174+
175+
/**
176+
* Guess the permalink for a room based on its room ID.
177+
*
178+
* The spec says that Room IDs are opaque [1] so this can only ever be a
179+
* guess. There is no guarantee that this room exists on this server.
180+
*
181+
* [1] https://spec.matrix.org/v1.5/appendices/#room-ids-and-event-ids
182+
*/
183+
function guessLinkForRoomId(roomId: string): string | null {
184+
const serverName = guessServerNameFromRoomId(roomId);
185+
if (serverName) {
186+
return new MatrixToPermalinkConstructor().forRoom(roomId, [serverName]);
187+
} else {
188+
return null;
189+
}
190+
}
115191
};
192+
193+
/**
194+
* @internal Public for test only
195+
*
196+
* Guess the server name for a room based on its room ID.
197+
*
198+
* The spec says that Room IDs are opaque [1] so this can only ever be a
199+
* guess. There is no guarantee that this room exists on this server.
200+
*
201+
* [1] https://spec.matrix.org/v1.5/appendices/#room-ids-and-event-ids
202+
*/
203+
export function guessServerNameFromRoomId(roomId: string): string | null {
204+
const m = roomId.match(/^[^:]*:(.*)/);
205+
if (m) {
206+
return m[1];
207+
} else {
208+
return null;
209+
}
210+
}

src/components/views/settings/tabs/room/AdvancedRoomSettingsTab.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,16 @@ interface IRecommendedVersion {
4242
interface IState {
4343
// This is eventually set to the value of room.getRecommendedVersion()
4444
upgradeRecommendation?: IRecommendedVersion;
45+
46+
/** The room ID of this room's predecessor, if it exists. */
4547
oldRoomId?: string;
48+
49+
/** The ID of tombstone event in this room's predecessor, if it exists. */
4650
oldEventId?: string;
51+
52+
/** The via servers to use to find this room's predecessor, if it exists. */
53+
oldViaServers?: string[];
54+
4755
upgraded?: boolean;
4856
}
4957

@@ -65,6 +73,7 @@ export default class AdvancedRoomSettingsTab extends React.Component<IProps, ISt
6573
if (predecessor) {
6674
additionalStateChanges.oldRoomId = predecessor.roomId;
6775
additionalStateChanges.oldEventId = predecessor.eventId;
76+
additionalStateChanges.oldViaServers = predecessor.viaServers;
6877
}
6978

7079
this.setState({
@@ -88,6 +97,7 @@ export default class AdvancedRoomSettingsTab extends React.Component<IProps, ISt
8897
action: Action.ViewRoom,
8998
room_id: this.state.oldRoomId,
9099
event_id: this.state.oldEventId,
100+
via_servers: this.state.oldViaServers,
91101
metricsTrigger: "WebPredecessorSettings",
92102
metricsViaKeyboard: e.type !== "click",
93103
});

src/i18n/strings/en_EN.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2458,8 +2458,10 @@
24582458
"%(senderDisplayName)s changed the avatar for %(roomName)s": "%(senderDisplayName)s changed the avatar for %(roomName)s",
24592459
"%(senderDisplayName)s removed the room avatar.": "%(senderDisplayName)s removed the room avatar.",
24602460
"%(senderDisplayName)s changed the room avatar to <img/>": "%(senderDisplayName)s changed the room avatar to <img/>",
2461-
"Click here to see older messages.": "Click here to see older messages.",
24622461
"This room is a continuation of another conversation.": "This room is a continuation of another conversation.",
2462+
"Can't find the old version of this room (room ID: %(roomId)s), and we have not been provided with 'via_servers' to look for it. It's possible that guessing the server from the room ID will work. If you want to try, click this link:": "Can't find the old version of this room (room ID: %(roomId)s), and we have not been provided with 'via_servers' to look for it. It's possible that guessing the server from the room ID will work. If you want to try, click this link:",
2463+
"Can't find the old version of this room (room ID: %(roomId)s), and we have not been provided with 'via_servers' to look for it.": "Can't find the old version of this room (room ID: %(roomId)s), and we have not been provided with 'via_servers' to look for it.",
2464+
"Click here to see older messages.": "Click here to see older messages.",
24632465
"Add an Integration": "Add an Integration",
24642466
"You are about to be taken to a third-party site so you can authenticate your account for use with %(integrationsUrl)s. Do you wish to continue?": "You are about to be taken to a third-party site so you can authenticate your account for use with %(integrationsUrl)s. Do you wish to continue?",
24652467
"Edited at %(date)s": "Edited at %(date)s",

src/utils/permalinks/Permalinks.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class RoomPermalinkCreator {
9696
// Some of the tests done by this class are relatively expensive, so normally
9797
// throttled to not happen on every update. Pass false as the shouldThrottle
9898
// param to disable this behaviour, eg. for tests.
99-
public constructor(private room: Room, roomId: string | null = null, shouldThrottle = true) {
99+
public constructor(private room: Room | null, roomId: string | null = null, shouldThrottle = true) {
100100
this.roomId = room ? room.roomId : roomId!;
101101

102102
if (!this.roomId) {
@@ -118,12 +118,12 @@ export class RoomPermalinkCreator {
118118

119119
public start(): void {
120120
this.load();
121-
this.room.currentState.on(RoomStateEvent.Update, this.onRoomStateUpdate);
121+
this.room?.currentState.on(RoomStateEvent.Update, this.onRoomStateUpdate);
122122
this.started = true;
123123
}
124124

125125
public stop(): void {
126-
this.room.currentState.removeListener(RoomStateEvent.Update, this.onRoomStateUpdate);
126+
this.room?.currentState.removeListener(RoomStateEvent.Update, this.onRoomStateUpdate);
127127
this.started = false;
128128
}
129129

@@ -171,15 +171,15 @@ export class RoomPermalinkCreator {
171171
}
172172

173173
private updateHighestPlUser(): void {
174-
const plEvent = this.room.currentState.getStateEvents("m.room.power_levels", "");
174+
const plEvent = this.room?.currentState.getStateEvents("m.room.power_levels", "");
175175
if (plEvent) {
176176
const content = plEvent.getContent();
177177
if (content) {
178178
const users: Record<string, number> = content.users;
179179
if (users) {
180180
const entries = Object.entries(users);
181181
const allowedEntries = entries.filter(([userId]) => {
182-
const member = this.room.getMember(userId);
182+
const member = this.room?.getMember(userId);
183183
if (!member || member.membership !== "join") {
184184
return false;
185185
}
@@ -213,8 +213,8 @@ export class RoomPermalinkCreator {
213213
private updateAllowedServers(): void {
214214
const bannedHostsRegexps: RegExp[] = [];
215215
let allowedHostsRegexps = [ANY_REGEX]; // default allow everyone
216-
if (this.room.currentState) {
217-
const aclEvent = this.room.currentState.getStateEvents(EventType.RoomServerAcl, "");
216+
if (this.room?.currentState) {
217+
const aclEvent = this.room?.currentState.getStateEvents(EventType.RoomServerAcl, "");
218218
if (aclEvent && aclEvent.getContent()) {
219219
const getRegex = (hostname: string): RegExp =>
220220
new RegExp("^" + utils.globToRegexp(hostname, false) + "$");
@@ -237,12 +237,14 @@ export class RoomPermalinkCreator {
237237

238238
private updatePopulationMap(): void {
239239
const populationMap: { [server: string]: number } = {};
240-
for (const member of this.room.getJoinedMembers()) {
241-
const serverName = getServerName(member.userId);
242-
if (!populationMap[serverName]) {
243-
populationMap[serverName] = 0;
240+
if (this.room) {
241+
for (const member of this.room.getJoinedMembers()) {
242+
const serverName = getServerName(member.userId);
243+
if (!populationMap[serverName]) {
244+
populationMap[serverName] = 0;
245+
}
246+
populationMap[serverName]++;
244247
}
245-
populationMap[serverName]++;
246248
}
247249
this.populationMap = populationMap;
248250
}

0 commit comments

Comments
 (0)