Skip to content

Commit

Permalink
fix: unread self mention update (#2678)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas authored Mar 25, 2024
1 parent f4a09b4 commit e5bc7ec
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<MessageEntity.Mention>,
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)
Expand Down

0 comments on commit e5bc7ec

Please sign in to comment.