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

Fix all rooms search generating permalinks to wrong room id #10625

Merged
merged 12 commits into from
Apr 26, 2023
27 changes: 25 additions & 2 deletions src/components/structures/RoomSearchView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ interface Props {
// XXX: why doesn't searching on name work?
export const RoomSearchView = forwardRef<ScrollPanel, Props>(
(
{ term, scope, promise, abortController, resizeNotifier, permalinkCreator, className, onUpdate }: Props,
{
term,
scope,
promise,
abortController,
resizeNotifier,
permalinkCreator: _permalinkCreator,
className,
onUpdate,
}: Props,
ref: RefObject<ScrollPanel>,
) => {
const client = useContext(MatrixClientContext);
Expand All @@ -68,6 +77,10 @@ export const RoomSearchView = forwardRef<ScrollPanel, Props>(
const [highlights, setHighlights] = useState<string[] | null>(null);
const [results, setResults] = useState<ISearchResults | null>(null);
const aborted = useRef(false);
// The permalinkCreator prop we are passed is only for the room the user was viewing
// So we will need to create additional room permalink creators for "All rooms" mode results
// to be able to generate share permalinks for results from other rooms
const permalinkCreators = useRef(new Map<string, RoomPermalinkCreator>()).current;
t3chguy marked this conversation as resolved.
Show resolved Hide resolved

const handleSearchResult = useCallback(
(searchPromise: Promise<ISearchResults>): Promise<boolean> => {
Expand Down Expand Up @@ -217,7 +230,7 @@ export const RoomSearchView = forwardRef<ScrollPanel, Props>(
const result = results.results[i];

const mxEv = result.context.getEvent();
const roomId = mxEv.getRoomId();
const roomId = mxEv.getRoomId()!;
const room = client.getRoom(roomId);
if (!room) {
// if we do not have the room in js-sdk stores then hide it as we cannot easily show it
Expand Down Expand Up @@ -283,6 +296,16 @@ export const RoomSearchView = forwardRef<ScrollPanel, Props>(
ourEventsIndexes.push(result.context.getOurEventIndex());
}

let permalinkCreator = _permalinkCreator;
if (roomId !== permalinkCreator.roomId) {
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
if (permalinkCreators.has(roomId)) {
permalinkCreator = permalinkCreators.get(roomId)!;
} else {
permalinkCreator = new RoomPermalinkCreator(client.getRoom(roomId), roomId);
permalinkCreators.set(roomId, permalinkCreator);
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
}
}

ret.push(
<SearchResultTile
key={mxEv.getId()}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/permalinks/Permalinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const ANY_REGEX = /.*/;
// the list and magically have the link work.

export class RoomPermalinkCreator {
private roomId: string;
public readonly roomId: string;
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
private highestPlUserId: string | null = null;
private populationMap: { [serverName: string]: number } = {};
private bannedHostsRegexps: RegExp[] = [];
Expand Down