query method

Future<ChannelState> query({
  1. bool state = true,
  2. bool watch = false,
  3. bool presence = false,
  4. PaginationParams? messagesPagination,
  5. PaginationParams? membersPagination,
  6. PaginationParams? watchersPagination,
  7. bool preferOffline = false,
})

Query the API, get messages, members or other channel fields.

Set preferOffline to true to avoid the API call if the data is already in the offline storage.

Implementation

Future<ChannelState> query({
  bool state = true,
  bool watch = false,
  bool presence = false,
  PaginationParams? messagesPagination,
  PaginationParams? membersPagination,
  PaginationParams? watchersPagination,
  bool preferOffline = false,
}) async {
  if (preferOffline && cid != null) {
    final updatedState = await _client.chatPersistenceClient
        ?.getChannelStateByCid(cid!, messagePagination: messagesPagination);
    if (updatedState != null && updatedState.messages.isNotEmpty) {
      if (this.state == null) {
        _initState(updatedState);
      } else {
        this.state?.updateChannelState(updatedState);
      }
      return updatedState;
    }
  }

  try {
    final updatedState = await _client.queryChannel(
      type,
      channelId: id,
      channelData: _extraData,
      state: state,
      watch: watch,
      presence: presence,
      messagesPagination: messagesPagination,
      membersPagination: membersPagination,
      watchersPagination: watchersPagination,
    );

    if (_id == null) {
      _id = updatedState.channel!.id;
      _cid = updatedState.channel!.cid;
    }

    this.state?.updateChannelState(updatedState);
    return updatedState;
  } catch (e) {
    if (!_client.persistenceEnabled) {
      rethrow;
    }
    return _client.chatPersistenceClient!.getChannelStateByCid(
      cid!,
      messagePagination: messagesPagination,
    );
  }
}