Skip to content

Commit

Permalink
fix: second message edit will fail (#2976) (#2979)
Browse files Browse the repository at this point in the history
Co-authored-by: Mohamad Jaara <[email protected]>
Co-authored-by: Yamil Medina <[email protected]>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent 10ce34e commit 1549f4c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -100,5 +100,4 @@ internal class MessageTextEditHandlerImpl internal constructor(
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 1549f4c

Please sign in to comment.