Migrate SpotlightDialog for dynamic room predecessors (#10076)

This commit is contained in:
Michael Weimann 2023-02-03 15:47:07 +01:00 committed by GitHub
parent add23e4d5d
commit 97506cbcdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 8 deletions

View File

@ -50,7 +50,6 @@ import { useDebouncedCallback } from "../../../../hooks/spotlight/useDebouncedCa
import { useRecentSearches } from "../../../../hooks/spotlight/useRecentSearches"; import { useRecentSearches } from "../../../../hooks/spotlight/useRecentSearches";
import { useProfileInfo } from "../../../../hooks/useProfileInfo"; import { useProfileInfo } from "../../../../hooks/useProfileInfo";
import { usePublicRoomDirectory } from "../../../../hooks/usePublicRoomDirectory"; import { usePublicRoomDirectory } from "../../../../hooks/usePublicRoomDirectory";
import { useFeatureEnabled } from "../../../../hooks/useSettings";
import { useSpaceResults } from "../../../../hooks/useSpaceResults"; import { useSpaceResults } from "../../../../hooks/useSpaceResults";
import { useUserDirectory } from "../../../../hooks/useUserDirectory"; import { useUserDirectory } from "../../../../hooks/useUserDirectory";
import { getKeyBindingsManager } from "../../../../KeyBindingsManager"; import { getKeyBindingsManager } from "../../../../KeyBindingsManager";
@ -93,6 +92,7 @@ import { TooltipOption } from "./TooltipOption";
import { isLocalRoom } from "../../../../utils/localRoom/isLocalRoom"; import { isLocalRoom } from "../../../../utils/localRoom/isLocalRoom";
import { shouldShowFeedback } from "../../../../utils/Feedback"; import { shouldShowFeedback } from "../../../../utils/Feedback";
import RoomAvatar from "../../avatars/RoomAvatar"; import RoomAvatar from "../../avatars/RoomAvatar";
import { useFeatureEnabled } from "../../../../hooks/useSettings";
const MAX_RECENT_SEARCHES = 10; const MAX_RECENT_SEARCHES = 10;
const SECTION_LIMIT = 50; // only show 50 results per section for performance reasons const SECTION_LIMIT = 50; // only show 50 results per section for performance reasons
@ -242,8 +242,8 @@ export const useWebSearchMetrics = (numResults: number, queryLength: number, via
}, [numResults, queryLength, viaSpotlight]); }, [numResults, queryLength, viaSpotlight]);
}; };
const findVisibleRooms = (cli: MatrixClient): Room[] => { const findVisibleRooms = (cli: MatrixClient, msc3946ProcessDynamicPredecessor: boolean): Room[] => {
return cli.getVisibleRooms().filter((room) => { return cli.getVisibleRooms(msc3946ProcessDynamicPredecessor).filter((room) => {
// Do not show local rooms // Do not show local rooms
if (isLocalRoom(room)) return false; if (isLocalRoom(room)) return false;
@ -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( return Object.values(
findVisibleRooms(cli) findVisibleRooms(cli, msc3946ProcessDynamicPredecessor)
.filter((room) => !filterDMs || !DMRoomMap.shared().getUserIdForRoomId(room.roomId)) .filter((room) => !filterDMs || !DMRoomMap.shared().getUserIdForRoomId(room.roomId))
.reduce((members, room) => { .reduce((members, room) => {
for (const member of room.getJoinedMembers()) { for (const member of room.getJoinedMembers()) {
@ -304,6 +308,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
const memberScores = buildMemberScores(cli); const memberScores = buildMemberScores(cli);
return compareMembers(activityScores, memberScores); return compareMembers(activityScores, memberScores);
}, [cli]); }, [cli]);
const msc3946ProcessDynamicPredecessor = useFeatureEnabled("feature_dynamic_room_predecessors");
const ownInviteLink = makeUserPermalink(cli.getUserId()); const ownInviteLink = makeUserPermalink(cli.getUserId());
const [inviteLinkCopied, setInviteLinkCopied] = useState<boolean>(false); const [inviteLinkCopied, setInviteLinkCopied] = useState<boolean>(false);
@ -339,7 +344,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
const possibleResults = useMemo<Result[]>(() => { const possibleResults = useMemo<Result[]>(() => {
const userResults: IMemberResult[] = []; 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 // If we already have a DM with the user we're looking for, we will
// show that DM instead of the user themselves // show that DM instead of the user themselves
const alreadyAddedUserIds = roomResults.reduce((userIds, result) => { const alreadyAddedUserIds = roomResults.reduce((userIds, result) => {
@ -349,7 +354,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
userIds.add(userId); userIds.add(userId);
return userIds; return userIds;
}, new Set<string>()); }, 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 // Make sure we don't have any user more than once
if (alreadyAddedUserIds.has(user.userId)) continue; if (alreadyAddedUserIds.has(user.userId)) continue;
alreadyAddedUserIds.add(user.userId); alreadyAddedUserIds.add(user.userId);
@ -381,7 +386,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
), ),
...publicRooms.map(toPublicRoomResult), ...publicRooms.map(toPublicRoomResult),
].filter((result) => filter === null || result.filter.includes(filter)); ].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 = useMemo<Record<Section, Result[]>>(() => {
const results: Record<Section, Result[]> = { const results: Record<Section, Result[]> = {

View File

@ -30,6 +30,8 @@ import { DirectoryMember, startDmOnFirstMessage } from "../../../../src/utils/di
import DMRoomMap from "../../../../src/utils/DMRoomMap"; import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { mkRoom, stubClient } from "../../../test-utils"; import { mkRoom, stubClient } from "../../../test-utils";
import { shouldShowFeedback } from "../../../../src/utils/Feedback"; import { shouldShowFeedback } from "../../../../src/utils/Feedback";
import SettingsStore from "../../../../src/settings/SettingsStore";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
jest.mock("../../../../src/utils/Feedback"); jest.mock("../../../../src/utils/Feedback");
@ -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", () => { describe("should apply manually selected filter", () => {
it("with public rooms", async () => { it("with public rooms", async () => {
const wrapper = mount(<SpotlightDialog onFinished={() => null} />); const wrapper = mount(<SpotlightDialog onFinished={() => null} />);
@ -224,6 +246,9 @@ describe("Spotlight Dialog", () => {
expect(options.length).toBe(1); expect(options.length).toBe(1);
expect(options.first().text()).toContain(testPublicRoom.name); expect(options.first().text()).toContain(testPublicRoom.name);
// assert that getVisibleRooms is called without MSC3946 dynamic room predecessors
expect(mockedClient.getVisibleRooms).toHaveBeenCalledWith(false);
wrapper.unmount(); wrapper.unmount();
}); });
it("with people", async () => { it("with people", async () => {