getRoomEvents method

Future<GetRoomEventsResponse> getRoomEvents(
  1. String roomId,
  2. Direction dir, {
  3. String? from,
  4. String? to,
  5. int? limit,
  6. String? filter,
})

This API returns a list of message and state events for a room. It uses pagination query parameters to paginate history in the room.

Note: This endpoint supports lazy-loading of room member events. See Lazy-loading room members for more information.

roomId The room to get events from.

from The token to start returning events from. This token can be obtained from a prev_batch or next_batch token returned by the /sync endpoint, or from an end token returned by a previous request to this endpoint.

This endpoint can also accept a value returned as a start token by a previous request to this endpoint, though servers are not required to support this. Clients should not rely on the behaviour.

If it is not provided, the homeserver shall return a list of messages from the first or last (per the value of the dir parameter) visible event in the room history for the requesting user.

to The token to stop returning events at. This token can be obtained from a prev_batch or next_batch token returned by the /sync endpoint, or from an end token returned by a previous request to this endpoint.

dir The direction to return events from. If this is set to f, events will be returned in chronological order starting at from. If it is set to b, events will be returned in reverse chronological order, again starting at from.

limit The maximum number of events to return. Default: 10.

filter A JSON RoomEventFilter to filter returned events with.

Implementation

Future<GetRoomEventsResponse> getRoomEvents(
  String roomId,
  Direction dir, {
  String? from,
  String? to,
  int? limit,
  String? filter,
}) async {
  final requestUri = Uri(
    path: '_matrix/client/v3/rooms/${Uri.encodeComponent(roomId)}/messages',
    queryParameters: {
      if (from != null) 'from': from,
      if (to != null) 'to': to,
      'dir': dir.name,
      if (limit != null) 'limit': limit.toString(),
      if (filter != null) 'filter': filter,
    },
  );
  final request = Request('GET', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  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 GetRoomEventsResponse.fromJson(json as Map<String, Object?>);
}