diff --git a/docs/chat.md b/docs/chat.md index 210c1ea7a..2cd349c16 100644 --- a/docs/chat.md +++ b/docs/chat.md @@ -163,7 +163,7 @@ See [OCP\RichObjectStrings\Definitions](https://github.com/nextcloud/server/blob field | type | Description ---|---|--- - `offset` | int | Offset parameter to get another chunk of chat messages + `lastKnownMessageId` | int | Serves as an offset for the query. The lastKnownMessageId for the next page is available in the `X-Chat-Last-Given` header. `limit` | int | Number of chat messages with shares you want to get * Response: @@ -173,6 +173,12 @@ See [OCP\RichObjectStrings\Definitions](https://github.com/nextcloud/server/blob + `404 Not Found` When the conversation could not be found for the participant + `412 Precondition Failed` When the lobby is active and the user is not a moderator + - Header: + + field | type | Description + ---|---|--- + `X-Chat-Last-Given` | int | Offset (lastKnownMessageId) for the next page. + - Data: Array of messages as defined in [Receive chat messages of a conversation](#receive-chat-messages-of-a-conversation) diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 9209f9342..290867592 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -564,6 +564,25 @@ class ChatManager { $this->notifier->removePendingNotificationsForRoom($chat); } + /** + * Search for comments with a given content + * + * @param Room $chat + * @param int $offset + * @param int $limit + * @return IComment[] + */ + public function getSharedObjectMessages(Room $chat, int $offset, int $limit): array { + return $this->commentsManager->getCommentsWithVerbForObjectSinceComment( + 'chat', + (string) $chat->getId(), + ['object_shared'], + $offset, + 'desc', + $limit + ); + } + /** * Search for comments with a given content * diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 686a75359..df22cabe0 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -706,15 +706,15 @@ class ChatController extends AEnvironmentAwareController { * @RequireReadWriteConversation * @RequireModeratorOrNoLobby * - * @param int $offset + * @param int $lastKnownMessageId * @param int $limit * @return DataResponse */ - public function getObjectsSharedInRoom(int $offset = 0, int $limit = 50): DataResponse { - $offset = max(0, $offset); + public function getObjectsSharedInRoom(int $lastKnownMessageId = 0, int $limit = 100): DataResponse { + $offset = max(0, $lastKnownMessageId); $limit = min(200, $limit); - $comments = $this->chatManager->searchForObjects('', [$this->room->getId()], 'object_shared', $offset, $limit); + $comments = $this->chatManager->getSharedObjectMessages($this->room, $offset, $limit); $messages = []; foreach ($comments as $comment) { @@ -728,7 +728,14 @@ class ChatController extends AEnvironmentAwareController { $messages[] = $message->toArray(); } - return new DataResponse($messages); + $response = new DataResponse($messages, Http::STATUS_OK); + + $newLastKnown = end($comments); + if ($newLastKnown instanceof IComment) { + $response->addHeader('X-Chat-Last-Given', $newLastKnown->getId()); + } + + return $response; } /** diff --git a/tests/integration/features/bootstrap/FeatureContext.php b/tests/integration/features/bootstrap/FeatureContext.php index f13b5cd66..7a60aba3b 100644 --- a/tests/integration/features/bootstrap/FeatureContext.php +++ b/tests/integration/features/bootstrap/FeatureContext.php @@ -1561,7 +1561,7 @@ class FeatureContext implements Context, SnippetAcceptingContext { * @param string $statusCode * @param string $apiVersion */ - public function userSeesTheFollowingSharedMediaInRoom($user, $identifier, $statusCode, $apiVersion = 'v1', TableNode $formData = null) { + public function userSeesTheFollowingSharedMediaInRoom($user, $identifier, $statusCode, $apiVersion = 'v1', TableNode $formData = null): void { $this->setCurrentUser($user); $this->sendRequest('GET', '/apps/spreed/api/' . $apiVersion . '/chat/' . self::$identifierToToken[$identifier] . '/share'); $this->assertStatusCode($this->response, $statusCode);