From c1df42fbbfcb1e72371a30ec1ba0cf2396beebe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BBerko?= Date: Thu, 12 Oct 2023 13:59:31 +0200 Subject: [PATCH 1/2] fix: remove unread event after mark delete message --- .../db_user/com/wire/kalium/persistence/UnreadEvents.sq | 8 ++++++++ .../kalium/persistence/dao/message/MessageDAOImpl.kt | 9 +++++++++ .../kalium/persistence/dao/message/MessageDAOTest.kt | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/persistence/src/commonMain/db_user/com/wire/kalium/persistence/UnreadEvents.sq b/persistence/src/commonMain/db_user/com/wire/kalium/persistence/UnreadEvents.sq index f7da2f59fd6..020b4745972 100644 --- a/persistence/src/commonMain/db_user/com/wire/kalium/persistence/UnreadEvents.sq +++ b/persistence/src/commonMain/db_user/com/wire/kalium/persistence/UnreadEvents.sq @@ -12,6 +12,9 @@ CREATE TABLE UnreadEvent ( PRIMARY KEY (id, conversation_id) ); +deleteUnreadEvent: +DELETE FROM UnreadEvent WHERE id = ? AND conversation_id = ?; + deleteUnreadEvents: DELETE FROM UnreadEvent WHERE creation_date <= ? AND conversation_id = ?; @@ -19,6 +22,11 @@ insertEvent: INSERT OR IGNORE INTO UnreadEvent(id, type, conversation_id, creation_date) VALUES(?, ?, ?, ?); +updateEvent: +UPDATE OR IGNORE UnreadEvent +SET type = :type +WHERE id = :id AND conversation_id = :conversation_id; + getUnreadEvents: SELECT conversation_id, type FROM UnreadEvent; 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 9082b1f3088..6e432cb4cfa 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 @@ -33,6 +33,7 @@ import com.wire.kalium.persistence.dao.conversation.ConversationEntity import com.wire.kalium.persistence.dao.unread.ConversationUnreadEventEntity import com.wire.kalium.persistence.dao.unread.UnreadEventEntity import com.wire.kalium.persistence.dao.unread.UnreadEventMapper +import com.wire.kalium.persistence.dao.unread.UnreadEventTypeEntity import com.wire.kalium.persistence.util.mapToList import com.wire.kalium.persistence.util.mapToOneOrNull import kotlinx.coroutines.flow.Flow @@ -73,6 +74,7 @@ internal class MessageDAOImpl internal constructor( override suspend fun markMessageAsDeleted(id: String, conversationsId: QualifiedIDEntity) = withContext(coroutineContext) { queries.markMessageAsDeleted(id, conversationsId) + unreadEventsQueries.deleteUnreadEvent(id, conversationsId) } override suspend fun deleteAllMessages() = withContext(coroutineContext) { @@ -297,6 +299,13 @@ internal class MessageDAOImpl internal constructor( user_id = it.userId ) } + val selfMention = newTextContent.mentions.firstNotNullOfOrNull { it.userId == selfUserId } + if(selfMention != null) { + unreadEventsQueries.updateEvent(UnreadEventTypeEntity.MENTION, currentMessageId, conversationId) + } else { + unreadEventsQueries.updateEvent(UnreadEventTypeEntity.MESSAGE, currentMessageId, conversationId) + } + queries.updateMessageId(newMessageId, currentMessageId, conversationId) queries.updateQuotedMessageId(newMessageId, currentMessageId, conversationId) } diff --git a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOTest.kt b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOTest.kt index 0e6fa4d8f01..d55460c66b1 100644 --- a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOTest.kt +++ b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOTest.kt @@ -18,6 +18,7 @@ package com.wire.kalium.persistence.dao.message +import app.cash.turbine.test import com.wire.kalium.persistence.BaseDatabaseTest import com.wire.kalium.persistence.dao.QualifiedIDEntity import com.wire.kalium.persistence.dao.UserDAO @@ -43,6 +44,7 @@ import kotlin.test.Test import kotlin.test.assertContains import kotlin.test.assertContentEquals import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertIs import kotlin.test.assertNotNull import kotlin.test.assertTrue @@ -264,6 +266,7 @@ class MessageDAOTest : BaseDatabaseTest() { status = MessageEntity.Status.SENT ) ) + messageDAO.insertOrIgnoreMessages(allMessages) messageDAO.markMessageAsDeleted(messageUuid, deleteMessageConversationId) @@ -274,6 +277,8 @@ class MessageDAOTest : BaseDatabaseTest() { val notDeletedMessage = messageDAO.getMessageById(messageUuid, visibleMessageConversationId) assertTrue { notDeletedMessage?.visibility == MessageEntity.Visibility.VISIBLE } + + assertFalse { messageDAO.observeUnreadEvents().first().containsKey(deleteMessageConversationId) } } @Test From ce8da06cce89f054debacc65d303e4f7cf0b1673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BBerko?= Date: Thu, 12 Oct 2023 14:05:27 +0200 Subject: [PATCH 2/2] detekt fix --- .../com/wire/kalium/persistence/dao/message/MessageDAOImpl.kt | 2 +- 1 file changed, 1 insertion(+), 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 6e432cb4cfa..b827848b52a 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 @@ -300,7 +300,7 @@ internal class MessageDAOImpl internal constructor( ) } val selfMention = newTextContent.mentions.firstNotNullOfOrNull { it.userId == selfUserId } - if(selfMention != null) { + if (selfMention != null) { unreadEventsQueries.updateEvent(UnreadEventTypeEntity.MENTION, currentMessageId, conversationId) } else { unreadEventsQueries.updateEvent(UnreadEventTypeEntity.MESSAGE, currentMessageId, conversationId)