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

Don't show the same user twice in Spotlight #8978

Merged
merged 11 commits into from
Jul 6, 2022
Merged
29 changes: 29 additions & 0 deletions cypress/integration/12-spotlight/spotlight.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ declare global {
roomHeaderName(
options?: Partial<Loggable & Timeoutable & Withinable & Shadow>
): Chainable<JQuery<HTMLElement>>;
startDM(name: string): Chainable<void>;
}
}
}
Expand Down Expand Up @@ -109,6 +110,16 @@ Cypress.Commands.add("roomHeaderName", (
return cy.get(".mx_RoomHeader_nametext", options);
});

Cypress.Commands.add("startDM", (name: string) => {
cy.openSpotlightDialog().within(() => {
cy.spotlightFilter(Filter.People);
cy.spotlightSearch().clear().type(name);
cy.spotlightResults().eq(0).click();
});
// Opening a DM takes time...
cy.wait(3000);
});

describe("Spotlight", () => {
let synapse: SynapseInstance;

Expand Down Expand Up @@ -319,6 +330,24 @@ describe("Spotlight", () => {
});
});

it("should close spotlight after starting a DM", () => {
cy.startDM(bot1Name);
cy.get(".mx_SpotlightDialog").should("have.length", 0);
});

it("should show the same user only once", () => {
cy.startDM(bot1Name);
cy.visit("/#/home");

cy.openSpotlightDialog().within(() => {
cy.spotlightFilter(Filter.People);
cy.spotlightSearch().clear().type(bot1Name);
// Sometimes it take a bit of time for all the results to show up
cy.wait(100);
cy.spotlightResults().should("have.length", 1);
});
});

it("should be able to navigate results via keyboard", () => {
cy.openSpotlightDialog().within(() => {
cy.spotlightFilter(Filter.People);
Expand Down
21 changes: 20 additions & 1 deletion src/components/views/dialogs/spotlight/SpotlightDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
() => {
const roomMembers = findVisibleRoomMembers(cli);
const roomMemberIds = new Set(roomMembers.map(item => item.userId));
return [
const results = [
...SpaceStore.instance.enabledMetaSpaces.map(spaceKey => ({
section: Section.Spaces,
filter: [],
Expand All @@ -341,6 +341,25 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
...(profile ? [new DirectoryMember(profile)] : []).map(toMemberResult),
...publicRooms.map(toPublicRoomResult),
].filter(result => filter === null || result.filter.includes(filter));

// Find userIds with whom we have a DM
const hiddenUserIds = [...results].reduce((userIds, result) => {
const room: Room = result["room"];
if (!room) return userIds;
const userId = DMRoomMap.shared().getUserIdForRoomId(room.roomId);
if (!userId) return userIds;
if (room.getJoinedMemberCount() > 2) return userIds;

userIds.add(userId);
return userIds;
}, new Set<string>());

// Filter out members with whom we have a DM
return results.filter((result) => {
const userId = result["member"]?.userId;
if (!userId) return true;
return !hiddenUserIds.has(userId);
});
},
[cli, users, profile, publicRooms, filter],
);
Expand Down