query method

Future<ChannelState> query (
  1. {Map<String, dynamic> options: const {},
  2. PaginationParams messagesPagination,
  3. PaginationParams membersPagination,
  4. PaginationParams watchersPagination}
)

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

Implementation

Future<ChannelState> query({
  Map<String, dynamic> options = const {},
  PaginationParams messagesPagination,
  PaginationParams membersPagination,
  PaginationParams watchersPagination,
}) async {
  var path = '/channels/$type';
  if (id != null) {
    path = '$path/$id';
  }
  path = '$path/query';

  final payload = Map<String, dynamic>.from({
    'state': true,
  })
    ..addAll(options);

  if (_extraData != null) {
    payload['data'] = _extraData;
  }

  if (messagesPagination != null) {
    payload['messages'] = messagesPagination.toJson();
  }
  if (membersPagination != null) {
    payload['members'] = membersPagination.toJson();
  }
  if (watchersPagination != null) {
    payload['watchers'] = watchersPagination.toJson();
  }

  if (cid != null) {
    final updatedState = await _client.offlineStorage?.getChannel(cid);
    if (state == null && updatedState != null) {
      _initState(updatedState);
    }
  }

  final response = await _client.post(path, data: payload);
  final updatedState = _client.decode(response.data, ChannelState.fromJson);

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

  state?.updateChannelState(updatedState);
  return updatedState;
}