isRead method

bool isRead(
  1. ChatItem item,
  2. UserId? me, [
  3. List<ChatMember> members = const []
])

Indicates whether the provided ChatItem was read by some User other than me.

If members are provided, then accounts its ChatMember.joinedAt for a more precise read indication.

Implementation

bool isRead(
  ChatItem item,
  UserId? me, [
  List<ChatMember> members = const [],
]) {
  if (members.isNotEmpty) {
    if (members.length <= 1) {
      return true;
    }

    final Iterable<ChatMember> membersWithoutMe = members.where(
      (e) => e.user.id != me,
    );

    if (membersWithoutMe.isNotEmpty) {
      final PreciseDateTime firstJoinedAt = membersWithoutMe
          .fold<PreciseDateTime>(
            membersWithoutMe.first.joinedAt,
            (at, member) =>
                member.joinedAt.isBefore(at) ? member.joinedAt : at,
          );

      if (item.at.isBefore(firstJoinedAt)) {
        return true;
      }
    }
  }

  return lastReads.any((e) => !e.at.isBefore(item.at) && e.memberId != me);
}