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(