diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 385fc8ded05..0cd1e7f7a8c 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -428,7 +428,7 @@ public function shareObjectToChat(string $objectType, string $objectId, string $ * for the follow-up query. * * @param 0|1 $lookIntoFuture Polling for new messages (1) or getting the history of the chat (0) - * @param int $limit Number of chat messages to receive (100 by default, 200 at most) + * @param int<1, 200> $limit Number of chat messages to receive (100 by default, 200 at most) * @param int $lastKnownMessageId The last known message (serves as offset) * @psalm-param non-negative-int $lastKnownMessageId * @param int $lastCommonReadId The last known common read message @@ -469,7 +469,7 @@ public function receiveMessages( int $markNotificationsAsRead = 1, int $threadId = 0, ): DataResponse { - $limit = min(200, $limit); + $limit = min(200, max(1, $limit)); $timeout = min(30, $timeout); if ($this->room->isFederatedConversation()) { @@ -816,7 +816,7 @@ protected function prepareCommentsAsDataResponse(array $comments, int $lastCommo * * @param int $messageId The focused message which should be in the "middle" of the returned context * @psalm-param non-negative-int $messageId - * @param int<1, 100> $limit Number of chat messages to receive in both directions (50 by default, 100 at most, might return 201 messages) + * @param int<0, 100> $limit Number of chat messages to receive in both directions (50 by default, 100 at most, might return 201 messages) * @param int $threadId Limit the chat message list to a given thread * @psalm-param non-negative-int $threadId * @return DataResponse, array{'X-Chat-Last-Common-Read'?: numeric-string, X-Chat-Last-Given?: numeric-string}>|DataResponse @@ -839,7 +839,7 @@ public function getMessageContext( int $limit = 50, int $threadId = 0, ): DataResponse { - $limit = min(100, $limit); + $limit = min(100, max(0, $limit)); if ($this->room->isFederatedConversation()) { /** @var \OCA\Talk\Federation\Proxy\TalkV1\Controller\ChatController $proxy */ @@ -858,9 +858,16 @@ public function getMessageContext( // Guest in a fully expired chat, no history, just loading the chat from beginning for now $commentsHistory = $commentsFuture = []; } else { - $commentsHistory = $this->chatManager->getHistory($this->room, $messageId, $limit, true, $threadId); + // Load at least 1 history entry to get the includeLastKnown + $historyLimit = max(1, $limit); + $commentsHistory = $this->chatManager->getHistory($this->room, $messageId, $historyLimit, true, $threadId); $commentsHistory = array_reverse($commentsHistory); - $commentsFuture = $this->chatManager->waitForNewMessages($this->room, $messageId, $limit, 0, $currentUser, false, threadId: $threadId); + if ($limit === 0) { + // No context requested, only the known message + $commentsFuture = []; + } else { + $commentsFuture = $this->chatManager->waitForNewMessages($this->room, $messageId, $limit, 0, $currentUser, false, threadId: $threadId); + } } return $this->prepareCommentsAsDataResponse(array_merge($commentsHistory, $commentsFuture)); @@ -1571,7 +1578,7 @@ public function getObjectsSharedInRoomOverview(int $limit = 7): DataResponse { return $proxy->getObjectsSharedInRoomOverview($this->room, $this->participant, $limit); } - $limit = min(20, $limit); + $limit = min(20, max(1, $limit)); $objectTypes = [ Attachment::TYPE_AUDIO, @@ -1668,7 +1675,7 @@ public function getObjectsSharedInRoom(string $objectType, int $lastKnownMessage } $offset = max(0, $lastKnownMessageId); - $limit = min(200, $limit); + $limit = min(200, max(1, $limit)); $attachments = $this->attachmentService->getAttachmentsByType($this->room, $objectType, $offset, $limit); $messageIds = array_map(static fn (Attachment $attachment): int => $attachment->getMessageId(), $attachments); diff --git a/openapi-full.json b/openapi-full.json index 8fd0e183c7d..1cc4d460963 100644 --- a/openapi-full.json +++ b/openapi-full.json @@ -19824,7 +19824,9 @@ "schema": { "type": "integer", "format": "int64", - "default": 100 + "default": 100, + "minimum": 1, + "maximum": 200 } }, { @@ -20790,7 +20792,7 @@ "type": "integer", "format": "int64", "default": 50, - "minimum": 1, + "minimum": 0, "maximum": 100 } }, diff --git a/openapi.json b/openapi.json index c9a06adb641..54fcf4f72a2 100644 --- a/openapi.json +++ b/openapi.json @@ -19729,7 +19729,9 @@ "schema": { "type": "integer", "format": "int64", - "default": 100 + "default": 100, + "minimum": 1, + "maximum": 200 } }, { @@ -20695,7 +20697,7 @@ "type": "integer", "format": "int64", "default": 50, - "minimum": 1, + "minimum": 0, "maximum": 100 } },