diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index 43fbd040d..53c95f17c 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -498,7 +498,13 @@ class RoomController extends AEnvironmentAwareController { $lastReadMessage = $this->chatManager->getLastReadMessageFromLegacy($room, $currentUser); $currentParticipant->setLastReadMessage($lastReadMessage); } - $roomData['unreadMessages'] = $this->chatManager->getUnreadCount($room, $lastReadMessage); + if ($room->getLastMessage() && $lastReadMessage === (int) $room->getLastMessage()->getId()) { + // When the last message is the last read message, there are no unread messages, + // so we can save the query. + $roomData['unreadMessages'] = 0; + } else { + $roomData['unreadMessages'] = $this->chatManager->getUnreadCount($room, $lastReadMessage); + } $lastMention = $currentParticipant->getLastMentionMessage(); $roomData['unreadMention'] = $lastMention !== 0 && $lastReadMessage < $lastMention;