deleteMessagesList method

Future<CtrlMessage> deleteMessagesList(
  1. List<int> list,
  2. bool hard
)

Delete multiple messages defined by their IDs. Hard-deleting messages requires Owner permission

Implementation

Future<CtrlMessage> deleteMessagesList(List<int> list, bool hard) {
  list.sort((a, b) => a - b);

  // Convert the array of IDs to ranges
  var ranges = <DelRange>[];
  list.map((id) {
    if (ranges.isEmpty) {
      // First element.
      ranges.add(DelRange(low: id));
    } else {
      var prev = ranges[ranges.length - 1];
      if ((prev.hi == null && (id != prev.low! + 1)) || (id > prev.hi!)) {
        // New range.
        ranges.add(DelRange(low: id));
      } else {
        // Expand existing range.
        prev.hi = prev.hi != null ? max(prev.hi!, id + 1) : id + 1;
      }
    }
    return ranges;
  });

  // Send {del} message, return promise
  return deleteMessages(ranges, hard);
}