deleteMessages method

Future<CtrlMessage> deleteMessages(
  1. List<DelRange> ranges,
  2. bool hard
)

Delete messages. Hard-deleting messages requires Owner permission

Implementation

Future<CtrlMessage> deleteMessages(List<DelRange> ranges, bool hard) async {
  if (!isSubscribed) {
    return Future.error(
        Exception('Cannot delete messages in inactive topic'));
  }

  ranges.sort((r1, r2) {
    if (r1.low! < r2.low!) {
      return 1;
    }
    if (r1.low == r2.low) {
      return r2.hi == 0 || (r1.hi! >= r2.hi!) == true ? 1 : -1;
    }
    return -1;
  });

  // Remove pending messages from ranges possibly clipping some ranges.
  // ignore: omit_local_variable_types
  List<DelRange> toSend = [];
  ranges.forEach((r) {
    if (r.low! < _configService.appSettings.localSeqId) {
      if (r.hi == null || r.hi! < _configService.appSettings.localSeqId) {
        toSend.add(r);
      } else {
        // Clip hi to max allowed value.
        toSend.add(DelRange(low: r.low, hi: _maxSeq + 1));
      }
    }
  });

  // Send {del} message, return promise
  Future<dynamic> result;
  if (toSend.isNotEmpty) {
    result = _tinodeService.deleteMessages(name ?? '', toSend, hard);
  } else {
    result = Future.value({
      'params': {'del': 0}
    });
  }

  var response = await result;
  var ctrl = CtrlMessage.fromMessage(response);

  if (ctrl.params['del'] > _maxDel) {
    _maxDel = ctrl.params['del'];
  }

  ranges.forEach((r) {
    if (r.hi != 0) {
      flushMessageRange(r.low!, r.hi!);
    } else {
      flushMessage(r.low!);
    }
  });

  _updateDeletedRanges();
  // Calling with no parameters to indicate the messages were deleted.
  onData.add(null);
  return ctrl;
}