From 99700d2f164f9b39fd781f98d002b4180d46af3f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 26 Mar 2024 09:56:10 +0000 Subject: [PATCH] fix: unread self mention update (#2678) (#2680) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jakub Żerko Co-authored-by: Mohamad Jaara --- .../persistence/dao/message/MessageDAOImpl.kt | 3 +- .../dao/message/MessageTextEditTest.kt | 107 ++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOImpl.kt b/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOImpl.kt index 8c7fd377451..df1408bc0b5 100644 --- a/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOImpl.kt +++ b/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOImpl.kt @@ -342,7 +342,8 @@ internal class MessageDAOImpl internal constructor( user_id = it.userId ) } - val selfMention = newTextContent.mentions.firstNotNullOfOrNull { it.userId == selfUserId } + + val selfMention = newTextContent.mentions.firstOrNull { it.userId == selfUserId } if (selfMention != null) { unreadEventsQueries.updateEvent(UnreadEventTypeEntity.MENTION, currentMessageId, conversationId) } else { diff --git a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageTextEditTest.kt b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageTextEditTest.kt index e979b785a7d..ca84276fda2 100644 --- a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageTextEditTest.kt +++ b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageTextEditTest.kt @@ -20,13 +20,17 @@ package com.wire.kalium.persistence.dao.message import app.cash.turbine.test import com.wire.kalium.persistence.dao.receipt.ReceiptTypeEntity +import com.wire.kalium.persistence.dao.unread.UnreadEventTypeEntity import com.wire.kalium.persistence.utils.stubs.newRegularMessageEntity import com.wire.kalium.util.DateTimeUtil.toIsoDateTimeString import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map import kotlinx.coroutines.test.runTest import kotlinx.datetime.Clock import kotlinx.datetime.Instant import kotlin.test.Test +import kotlin.test.assertContains import kotlin.test.assertContentEquals import kotlin.test.assertEquals import kotlin.test.assertIs @@ -186,6 +190,109 @@ class MessageTextEditTest : BaseMessageTest() { assertEquals(editDate, editStatus.lastDate) } + @Test + fun givenUnreadTextWasInserted_whenUpdatingContentWithSelfMention_thenShouldUpdateUnreadEvent() = runTest { + // Given + insertInitialData() + + // When + val mentions = listOf(MessageEntity.Mention(0, 1, SELF_USER_ID)) + messageDAO.updateTextMessageContent( + editTimeStamp = Instant.DISTANT_FUTURE.toIsoDateTimeString(), + conversationId = CONVERSATION_ID, + currentMessageId = ORIGINAL_MESSAGE_ID, + newTextContent = ORIGINAL_CONTENT.copy( + messageBody = "Howdy", + mentions = mentions + ), + newMessageId = NEW_MESSAGE_ID + ) + + // Then + val unreadEvents = messageDAO.observeUnreadEvents() + .map { it[CONVERSATION_ID]!!.map { event -> event.type } } + .first() + + assertContains(unreadEvents, UnreadEventTypeEntity.MENTION) + } + + @Test + fun givenUnreadTextWasInsertedWithSelfMention_whenUpdatingContentWithoutSelfMention_thenShouldUpdateUnreadEvent() = runTest { + // Given + val initMentions = listOf(MessageEntity.Mention(0, 1, SELF_USER_ID), MessageEntity.Mention(2, 3, OTHER_USER.id)) + + insertInitialDataWithMentions( + mentions = initMentions, + ) + + // When + val mentions = listOf(MessageEntity.Mention(0, 1, OTHER_USER.id)) + messageDAO.updateTextMessageContent( + editTimeStamp = Instant.DISTANT_FUTURE.toIsoDateTimeString(), + conversationId = CONVERSATION_ID, + currentMessageId = ORIGINAL_MESSAGE_ID, + newTextContent = ORIGINAL_CONTENT.copy( + messageBody = "Howdy", + mentions = mentions + ), + newMessageId = NEW_MESSAGE_ID + ) + + // Then + val unreadEvents = messageDAO.observeUnreadEvents() + .map { it[CONVERSATION_ID]!!.map { event -> event.type } } + .first() + + assertContains(unreadEvents, UnreadEventTypeEntity.MESSAGE) + } + + @Test + fun givenTextWasInsertedAndIsRead_whenUpdatingContentWithSelfMention_thenUnreadEventShouldNotChange() = runTest { + // Given + val initMentions = listOf( + MessageEntity.Mention(0, 1, SELF_USER_ID), + MessageEntity.Mention(2, 3, OTHER_USER.id) + ) + + insertInitialDataWithMentions( + mentions = initMentions, + updateConversationReadDate = true + ) + + // When + val mentions = listOf(MessageEntity.Mention(0, 1, OTHER_USER.id)) + messageDAO.updateTextMessageContent( + editTimeStamp = Instant.DISTANT_FUTURE.toIsoDateTimeString(), + conversationId = CONVERSATION_ID, + currentMessageId = ORIGINAL_MESSAGE_ID, + newTextContent = ORIGINAL_CONTENT.copy( + messageBody = "Howdy", + mentions = mentions + ), + newMessageId = NEW_MESSAGE_ID + ) + + // Then + val unreadEvents = messageDAO.observeUnreadEvents() + .first()[CONVERSATION_ID] + + assertTrue(unreadEvents.isNullOrEmpty()) + } + + private suspend fun insertInitialDataWithMentions( + mentions: List, + updateConversationReadDate: Boolean = false + ) { + super.insertInitialData() + + messageDAO.insertOrIgnoreMessage( + ORIGINAL_MESSAGE.copy( + content = ORIGINAL_CONTENT.copy(mentions = mentions) + ), + updateConversationReadDate = updateConversationReadDate + ) + } + override suspend fun insertInitialData() { super.insertInitialData() messageDAO.insertOrIgnoreMessage(ORIGINAL_MESSAGE)