From 1549f4c551c53ae9f90dc39746b91678b586f15a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 31 Aug 2024 01:58:37 +0200 Subject: [PATCH] fix: second message edit will fail (#2976) (#2979) Co-authored-by: Mohamad Jaara Co-authored-by: Yamil Medina --- .../handler/MessageTextEditHandler.kt | 3 +- .../receiver/MessageTextEditHandlerTest.kt | 39 ++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/handler/MessageTextEditHandler.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/handler/MessageTextEditHandler.kt index 61e01d5b796..aa07f66e28c 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/handler/MessageTextEditHandler.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/handler/MessageTextEditHandler.kt @@ -63,7 +63,7 @@ internal class MessageTextEditHandlerImpl internal constructor( && editStatus is Message.EditStatus.Edited ) { // if the locally stored message is also already edited, we check which one is newer - if (editStatus.lastEditInstant < message.date) { + if (editStatus.lastEditInstant > message.date) { // our local pending or failed edit is newer than one we got from the backend so we update locally only message id and date messageRepository.updateTextMessage( conversationId = message.conversationId, @@ -100,5 +100,4 @@ internal class MessageTextEditHandlerImpl internal constructor( ) } } - } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/MessageTextEditHandlerTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/MessageTextEditHandlerTest.kt index c3d68d3499e..7479678b7a1 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/MessageTextEditHandlerTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/MessageTextEditHandlerTest.kt @@ -118,7 +118,7 @@ class MessageTextEditHandlerTest { @Test fun givenEditIsOlderThanLocalPendingStoredEdit_whenHandling_thenShouldUpdateOnlyMessageIdAndDate() = runTest { val originalContent = TestMessage.TEXT_CONTENT - val originalEditStatus = Message.EditStatus.Edited(Instant.UNIX_FIRST_DATE) + val originalEditStatus = Message.EditStatus.Edited(Instant.DISTANT_FUTURE) // original message date is newer than edit date val originalMessage = ORIGINAL_MESSAGE.copy( editStatus = originalEditStatus, content = originalContent, @@ -127,7 +127,7 @@ class MessageTextEditHandlerTest { ) val editContent = EDIT_CONTENT val editMessage = EDIT_MESSAGE.copy( - date = EDIT_MESSAGE.date - 10.minutes, + date = originalEditStatus.lastEditInstant - 10.minutes, // edit date is older than original message date content = editContent ) val expectedContent = MessageContent.TextEdited( @@ -151,6 +151,41 @@ class MessageTextEditHandlerTest { } } + @Test + fun givenAnAlreadyEditedMessage_whenNewEditIsInTheFuture_thenMessageContentIsUpdatd() = runTest { + val originalContent = TestMessage.TEXT_CONTENT + val originalEditStatus = Message.EditStatus.Edited(Instant.UNIX_FIRST_DATE) + val originalMessage = ORIGINAL_MESSAGE.copy( + editStatus = originalEditStatus, + content = originalContent, + status = Message.Status.Sent + ) + val editContent = EDIT_CONTENT + val editMessage = EDIT_MESSAGE.copy( + date = Instant.UNIX_FIRST_DATE + 10.minutes, + content = editContent + ) + val expectedContent = MessageContent.TextEdited( + editMessageId = editContent.editMessageId, + newContent = editContent.newContent, + newMentions = editContent.newMentions + ) + val (arrangement, messageTextEditHandler) = arrange { + withGetMessageById(Either.Right(originalMessage)) + } + + messageTextEditHandler.handle(editMessage, editContent) + + with(arrangement) { + coVerify { + messageRepository.updateTextMessage(any(), eq(expectedContent), eq(editMessage.id), eq(editMessage.date)) + }.wasInvoked(exactly = once) + coVerify { + messageRepository.updateMessageStatus(any(), any(), any()) + }.wasInvoked(exactly = once) + } + } + private suspend fun arrange(block: suspend Arrangement.() -> Unit) = Arrangement(block).arrange() private class Arrangement(