searchUserDirectory method

Future<SearchUserDirectoryResponse> searchUserDirectory(
  1. String searchTerm, {
  2. int? limit,
})

Performs a search for users. The homeserver may determine which subset of users are searched. However, the homeserver MUST at a minimum consider users who are visible to the requester based on their membership in rooms known to the homeserver. This means:

  • users that share a room with the requesting user
  • users who are joined to rooms known to the homeserver that have a public join rule
  • users who are joined to rooms known to the homeserver that have a world_readable history visibility

The search MUST consider local users to the homeserver, and SHOULD query remote users as part of the search.

The search is performed case-insensitively on user IDs and display names preferably using a collation determined based upon the Accept-Language header provided in the request, if present.

limit The maximum number of results to return. Defaults to 10.

searchTerm The term to search for

Implementation

Future<SearchUserDirectoryResponse> searchUserDirectory(
  String searchTerm, {
  int? limit,
}) async {
  final requestUri = Uri(path: '_matrix/client/v3/user_directory/search');
  final request = Request('POST', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  request.headers['content-type'] = 'application/json';
  request.bodyBytes = utf8.encode(
    jsonEncode({
      if (limit != null) 'limit': limit,
      'search_term': searchTerm,
    }),
  );
  final response = await httpClient.send(request);
  final responseBody = await response.stream.toBytes();
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  final responseString = utf8.decode(responseBody);
  final json = jsonDecode(responseString);
  return SearchUserDirectoryResponse.fromJson(json as Map<String, Object?>);
}