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

Commit

Permalink
Migrate SpotlightDialog for dynamic room predecessors (#10076)
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 authored Feb 3, 2023
1 parent add23e4 commit 97506cb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/components/views/dialogs/spotlight/SpotlightDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import { useDebouncedCallback } from "../../../../hooks/spotlight/useDebouncedCa
import { useRecentSearches } from "../../../../hooks/spotlight/useRecentSearches";
import { useProfileInfo } from "../../../../hooks/useProfileInfo";
import { usePublicRoomDirectory } from "../../../../hooks/usePublicRoomDirectory";
import { useFeatureEnabled } from "../../../../hooks/useSettings";
import { useSpaceResults } from "../../../../hooks/useSpaceResults";
import { useUserDirectory } from "../../../../hooks/useUserDirectory";
import { getKeyBindingsManager } from "../../../../KeyBindingsManager";
Expand Down Expand Up @@ -93,6 +92,7 @@ import { TooltipOption } from "./TooltipOption";
import { isLocalRoom } from "../../../../utils/localRoom/isLocalRoom";
import { shouldShowFeedback } from "../../../../utils/Feedback";
import RoomAvatar from "../../avatars/RoomAvatar";
import { useFeatureEnabled } from "../../../../hooks/useSettings";

const MAX_RECENT_SEARCHES = 10;
const SECTION_LIMIT = 50; // only show 50 results per section for performance reasons
Expand Down Expand Up @@ -242,8 +242,8 @@ export const useWebSearchMetrics = (numResults: number, queryLength: number, via
}, [numResults, queryLength, viaSpotlight]);
};

const findVisibleRooms = (cli: MatrixClient): Room[] => {
return cli.getVisibleRooms().filter((room) => {
const findVisibleRooms = (cli: MatrixClient, msc3946ProcessDynamicPredecessor: boolean): Room[] => {
return cli.getVisibleRooms(msc3946ProcessDynamicPredecessor).filter((room) => {
// Do not show local rooms
if (isLocalRoom(room)) return false;

Expand All @@ -252,9 +252,13 @@ const findVisibleRooms = (cli: MatrixClient): Room[] => {
});
};

const findVisibleRoomMembers = (cli: MatrixClient, filterDMs = true): RoomMember[] => {
const findVisibleRoomMembers = (
cli: MatrixClient,
msc3946ProcessDynamicPredecessor: boolean,
filterDMs = true,
): RoomMember[] => {
return Object.values(
findVisibleRooms(cli)
findVisibleRooms(cli, msc3946ProcessDynamicPredecessor)
.filter((room) => !filterDMs || !DMRoomMap.shared().getUserIdForRoomId(room.roomId))
.reduce((members, room) => {
for (const member of room.getJoinedMembers()) {
Expand Down Expand Up @@ -304,6 +308,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
const memberScores = buildMemberScores(cli);
return compareMembers(activityScores, memberScores);
}, [cli]);
const msc3946ProcessDynamicPredecessor = useFeatureEnabled("feature_dynamic_room_predecessors");

const ownInviteLink = makeUserPermalink(cli.getUserId());
const [inviteLinkCopied, setInviteLinkCopied] = useState<boolean>(false);
Expand Down Expand Up @@ -339,7 +344,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n

const possibleResults = useMemo<Result[]>(() => {
const userResults: IMemberResult[] = [];
const roomResults = findVisibleRooms(cli).map(toRoomResult);
const roomResults = findVisibleRooms(cli, msc3946ProcessDynamicPredecessor).map(toRoomResult);
// If we already have a DM with the user we're looking for, we will
// show that DM instead of the user themselves
const alreadyAddedUserIds = roomResults.reduce((userIds, result) => {
Expand All @@ -349,7 +354,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
userIds.add(userId);
return userIds;
}, new Set<string>());
for (const user of [...findVisibleRoomMembers(cli), ...users]) {
for (const user of [...findVisibleRoomMembers(cli, msc3946ProcessDynamicPredecessor), ...users]) {
// Make sure we don't have any user more than once
if (alreadyAddedUserIds.has(user.userId)) continue;
alreadyAddedUserIds.add(user.userId);
Expand Down Expand Up @@ -381,7 +386,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
),
...publicRooms.map(toPublicRoomResult),
].filter((result) => filter === null || result.filter.includes(filter));
}, [cli, users, profile, publicRooms, filter]);
}, [cli, users, profile, publicRooms, filter, msc3946ProcessDynamicPredecessor]);

const results = useMemo<Record<Section, Result[]>>(() => {
const results: Record<Section, Result[]> = {
Expand Down
25 changes: 25 additions & 0 deletions test/components/views/dialogs/SpotlightDialog-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { DirectoryMember, startDmOnFirstMessage } from "../../../../src/utils/di
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { mkRoom, stubClient } from "../../../test-utils";
import { shouldShowFeedback } from "../../../../src/utils/Feedback";
import SettingsStore from "../../../../src/settings/SettingsStore";
import { SettingLevel } from "../../../../src/settings/SettingLevel";

jest.mock("../../../../src/utils/Feedback");

Expand Down Expand Up @@ -202,6 +204,26 @@ describe("Spotlight Dialog", () => {
});
});

describe("when MSC3946 dynamic room predecessors is enabled", () => {
beforeEach(() => {
SettingsStore.setValue("feature_dynamic_room_predecessors", null, SettingLevel.DEVICE, true);
});

afterEach(() => {
SettingsStore.setValue("feature_dynamic_room_predecessors", null, SettingLevel.DEVICE, null);
});

it("should call getVisibleRooms with MSC3946 dynamic room predecessors", async () => {
const wrapper = mount(<SpotlightDialog onFinished={() => null} />);
await act(async () => {
await sleep(1);
});
wrapper.update();
expect(mockedClient.getVisibleRooms).toHaveBeenCalledWith(true);
wrapper.unmount();
});
});

describe("should apply manually selected filter", () => {
it("with public rooms", async () => {
const wrapper = mount(<SpotlightDialog onFinished={() => null} />);
Expand All @@ -224,6 +246,9 @@ describe("Spotlight Dialog", () => {
expect(options.length).toBe(1);
expect(options.first().text()).toContain(testPublicRoom.name);

// assert that getVisibleRooms is called without MSC3946 dynamic room predecessors
expect(mockedClient.getVisibleRooms).toHaveBeenCalledWith(false);

wrapper.unmount();
});
it("with people", async () => {
Expand Down

0 comments on commit 97506cb

Please sign in to comment.