hasNewMessages property

bool hasNewMessages

Checks if the last event has a read marker of the user. Warning: This compares the origin server timestamp which might not map to the real sort order of the timeline.

Implementation

bool get hasNewMessages {
  final lastEvent = this.lastEvent;

  // There is no known event or the last event is only a state fallback event,
  // we assume there is no new messages.
  if (lastEvent == null ||
      !client.roomPreviewLastEvents.contains(lastEvent.type)) return false;

  // Read marker is on the last event so no new messages.
  if (lastEvent.receipts
      .any((receipt) => receipt.user.senderId == client.userID!)) {
    return false;
  }

  // If the last event is sent, we mark the room as read.
  if (lastEvent.senderId == client.userID) return false;

  // Get the timestamp of read marker and compare
  final readAtMilliseconds = receiptState.global.latestOwnReceipt?.ts ?? 0;
  return readAtMilliseconds < lastEvent.originServerTs.millisecondsSinceEpoch;
}