requestHistory method

Future<int> requestHistory({
  1. int historyCount = defaultHistoryCount,
  2. void onHistoryReceived()?,
  3. dynamic direction = Direction.b,
  4. StateFilter? filter,
})

Request more previous events from the server. historyCount defines how many events should be received maximum. When the request is answered, onHistoryReceived will be triggered before the historical events will be published in the onEvent stream. filter allows you to specify a StateFilter object to filter the events, which can include various criteria such as event types (e.g., EventTypes.Message) and other state-related filters. The StateFilter object will have lazyLoadMembers set to true by default, but this can be overridden. Returns the actual count of received timeline events.

Implementation

Future<int> requestHistory({
  int historyCount = defaultHistoryCount,
  void Function()? onHistoryReceived,
  direction = Direction.b,
  StateFilter? filter,
}) async {
  final prev_batch = this.prev_batch;

  final storeInDatabase = !isArchived;

  // Ensure stateFilter is not null and set lazyLoadMembers to true if not already set
  filter ??= StateFilter(lazyLoadMembers: true);
  filter.lazyLoadMembers ??= true;

  if (prev_batch == null) {
    throw 'Tried to request history without a prev_batch token';
  }
  final resp = await client.getRoomEvents(
    id,
    direction,
    from: prev_batch,
    limit: historyCount,
    filter: jsonEncode(filter.toJson()),
  );

  if (onHistoryReceived != null) onHistoryReceived();

  Future<void> loadFn() async {
    if (!((resp.chunk.isNotEmpty) && resp.end != null)) return;

    await client.handleSync(
      SyncUpdate(
        nextBatch: '',
        rooms: RoomsUpdate(
          join: membership == Membership.join
              ? {
                  id: JoinedRoomUpdate(
                    state: resp.state,
                    timeline: TimelineUpdate(
                      limited: false,
                      events: direction == Direction.b
                          ? resp.chunk
                          : resp.chunk.reversed.toList(),
                      prevBatch:
                          direction == Direction.b ? resp.end : resp.start,
                    ),
                  ),
                }
              : null,
          leave: membership != Membership.join
              ? {
                  id: LeftRoomUpdate(
                    state: resp.state,
                    timeline: TimelineUpdate(
                      limited: false,
                      events: direction == Direction.b
                          ? resp.chunk
                          : resp.chunk.reversed.toList(),
                      prevBatch:
                          direction == Direction.b ? resp.end : resp.start,
                    ),
                  ),
                }
              : null,
        ),
      ),
      direction: direction,
    );
  }

  await client.database.transaction(() async {
    if (storeInDatabase && direction == Direction.b) {
      this.prev_batch = resp.end;
      await client.database.setRoomPrevBatch(resp.end, id, client);
    }
    await loadFn();
  });

  return resp.chunk.length;
}