diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 387bf6dd0a8..dd159c1c706 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -645,12 +645,21 @@ public function deleteMessage(Room $chat, IComment $comment, Participant $partic $this->commentsManager->save($comment); - $this->attachmentService->deleteAttachmentByMessageId((int)$comment->getId()); + $messageId = (int)$comment->getId(); + $this->attachmentService->deleteAttachmentByMessageId($messageId); $this->referenceManager->invalidateCache($chat->getToken()); $this->unreadCountCache->clear($chat->getId() . '-'); + if ($chat->getLastPinnedId() === $messageId) { + $pinnedMessages = $this->attachmentService->getAttachmentsByType($chat, Attachment::TYPE_PINNED, 0, 1); + if (!empty($pinnedMessages)) { + $pinnedMessage = array_shift($pinnedMessages); + $this->roomService->setLastPinnedId($chat, $pinnedMessage->getMessageId()); + } + } + return $this->addSystemMessage( $chat, $participant, diff --git a/tests/integration/features/chat-1/pinned-messages.feature b/tests/integration/features/chat-1/pinned-messages.feature index fc634a974fd..62579f29b7f 100644 --- a/tests/integration/features/chat-1/pinned-messages.feature +++ b/tests/integration/features/chat-1/pinned-messages.feature @@ -89,3 +89,20 @@ Feature: chat-1/pinned-messages Then user "participant2" is participant of the following rooms (v4) | id | type | lastPinnedId | hiddenPinnedId | | room | 3 | EMPTY | EMPTY | + + Scenario: Deleting last pinned message + Given user "participant1" creates room "room" (v4) + | roomType | 3 | + | roomName | room | + And user "participant1" adds user "participant2" to room "room" with 200 (v4) + When user "participant1" sends message "Message 1" to room "room" with 201 + When user "participant1" pins message "Message 1" in room "room" with 200 + When user "participant1" sends message "Message 2" to room "room" with 201 + When user "participant1" pins message "Message 2" in room "room" with 200 + Then user "participant2" is participant of the following rooms (v4) + | id | type | lastPinnedId | hiddenPinnedId | + | room | 3 | Message 2 | EMPTY | + When user "participant1" deletes message "Message 2" from room "room" with 200 + Then user "participant2" is participant of the following rooms (v4) + | id | type | lastPinnedId | hiddenPinnedId | + | room | 3 | Message 1 | EMPTY |