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

Implement MSC3973: Search users in the user directory with the Widget API #10269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/stores/widgets/StopGapWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
WidgetDriver,
WidgetEventCapability,
WidgetKind,
ISearchUserDirectoryResult,
} from "matrix-widget-api";
import { ClientEvent, ITurnServer as IClientTurnServer } from "matrix-js-sdk/src/client";
import { EventType } from "matrix-js-sdk/src/@types/event";
Expand Down Expand Up @@ -486,4 +487,19 @@ export class StopGapWidgetDriver extends WidgetDriver {
prevBatch: prevBatch ?? undefined,
};
}

public async searchUserDirectory(searchTerm: string, limit?: number): Promise<ISearchUserDirectoryResult> {
const client = MatrixClientPeg.get();

const { limited, results } = await client.searchUserDirectory({ term: searchTerm, limit });
dhenneke marked this conversation as resolved.
Show resolved Hide resolved

return {
limited,
results: results.map((r) => ({
userId: r.user_id,
displayName: r.display_name,
avatarUrl: r.avatar_url,
})),
};
}
}
36 changes: 36 additions & 0 deletions test/stores/widgets/StopGapWidgetDriver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,40 @@ describe("StopGapWidgetDriver", () => {
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
});
});

describe("searchUserDirectory", () => {
let driver: WidgetDriver;

beforeEach(() => {
driver = mkDefaultDriver();
});

it("searches for users in the user directory", async () => {
client.searchUserDirectory.mockResolvedValue({
limited: false,
results: [{ user_id: "@user", display_name: "Name", avatar_url: "mxc://" }],
});

await expect(driver.searchUserDirectory("foo")).resolves.toEqual({
limited: false,
results: [{ userId: "@user", displayName: "Name", avatarUrl: "mxc://" }],
});

expect(client.searchUserDirectory).toHaveBeenCalledWith({ term: "foo", limit: undefined });
});

it("searches for users with a custom limit", async () => {
client.searchUserDirectory.mockResolvedValue({
limited: true,
results: [],
});

await expect(driver.searchUserDirectory("foo", 25)).resolves.toEqual({
limited: true,
results: [],
});

expect(client.searchUserDirectory).toHaveBeenCalledWith({ term: "foo", limit: 25 });
});
});
});
2 changes: 2 additions & 0 deletions test/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ export function createTestClient(): MatrixClient {
room_id: roomId,
});
}),

searchUserDirectory: jest.fn().mockResolvedValue({ limited: false, results: [] }),
} as unknown as MatrixClient;

client.reEmitter = new ReEmitter(client);
Expand Down
10 changes: 1 addition & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6510,15 +6510,7 @@ matrix-web-i18n@^1.3.0:
"@babel/traverse" "^7.18.5"
walk "^2.3.15"

matrix-widget-api@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/matrix-widget-api/-/matrix-widget-api-1.1.1.tgz#d3fec45033d0cbc14387a38ba92dac4dbb1be962"
integrity sha512-gNSgmgSwvOsOcWK9k2+tOhEMYBiIMwX95vMZu0JqY7apkM02xrOzUBuPRProzN8CnbIALH7e3GAhatF6QCNvtA==
dependencies:
"@types/events" "^3.0.0"
events "^3.2.0"

matrix-widget-api@^1.3.1:
matrix-widget-api@^1.0.0, matrix-widget-api@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/matrix-widget-api/-/matrix-widget-api-1.3.1.tgz#e38f404c76bb15c113909505c1c1a5b4d781c2f5"
integrity sha512-+rN6vGvnXm+fn0uq9r2KWSL/aPtehD6ObC50jYmUcEfgo8CUpf9eUurmjbRlwZkWq3XHXFuKQBUCI9UzqWg37Q==
Expand Down