diff --git a/persistence/src/commonMain/db_user/com/wire/kalium/persistence/Conversations.sq b/persistence/src/commonMain/db_user/com/wire/kalium/persistence/Conversations.sq index a6529a0b32b..368b2ab68a0 100644 --- a/persistence/src/commonMain/db_user/com/wire/kalium/persistence/Conversations.sq +++ b/persistence/src/commonMain/db_user/com/wire/kalium/persistence/Conversations.sq @@ -393,9 +393,15 @@ AND Conversation.mls_group_state IS 'ESTABLISHED' ORDER BY Conversation.type DESC LIMIT 1; getMLSGroupIdByUserId: -SELECT Conversation.mls_group_id FROM Member -JOIN Conversation ON Conversation.qualified_id = Member.conversation -WHERE Conversation.mls_group_id IS NOT NULL AND mls_group_state IS 'ESTABLISHED' AND Member.user = :userId LIMIT 1; +SELECT mls_group_id FROM + (SELECT COUNT(Member.user) AS users_amout, Conversation.mls_group_id, Conversation.mls_group_state FROM Member + JOIN Conversation ON Conversation.qualified_id = Member.conversation + WHERE Member.user = :userId OR Member.user = (SELECT SelfUser.id FROM SelfUser LIMIT 1) + GROUP BY Conversation.qualified_id) +WHERE users_amout > 1 -- both (Self and Other users) belongs to that conversation +AND mls_group_id IS NOT NULL +AND mls_group_state IS 'ESTABLISHED' +LIMIT 1; getMLSGroupIdByConversationId: SELECT Conversation.mls_group_id FROM Conversation diff --git a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/ConversationDAOTest.kt b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/ConversationDAOTest.kt index 494cbbe4d53..c31791cc209 100644 --- a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/ConversationDAOTest.kt +++ b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/ConversationDAOTest.kt @@ -272,16 +272,17 @@ class ConversationDAOTest : BaseDatabaseTest() { ConversationEntity.GroupState.PENDING_WELCOME_MESSAGE, ConversationEntity.CipherSuite.MLS_256_DHKEMP521_AES256GCM_SHA512_P521, (conversationEntity2.protocolInfo as ConversationEntity.ProtocolInfo.MLS).groupId, - ) val result = conversationDAO.getConversationByQualifiedID(conversationEntity2.id) assertEquals( (result?.protocolInfo as ConversationEntity.ProtocolInfo.MLS).groupState, ConversationEntity.GroupState.PENDING_WELCOME_MESSAGE ) assertEquals( - (result?.protocolInfo as ConversationEntity.ProtocolInfo.MLS).cipherSuite, ConversationEntity.CipherSuite.MLS_256_DHKEMP521_AES256GCM_SHA512_P521 + (result?.protocolInfo as ConversationEntity.ProtocolInfo.MLS).cipherSuite, + ConversationEntity.CipherSuite.MLS_256_DHKEMP521_AES256GCM_SHA512_P521 ) } + @Test fun givenExistingConversation_ThenConversationIsUpdatedOnInsert() = runTest { conversationDAO.insertConversation(conversationEntity1) @@ -1650,8 +1651,6 @@ class ConversationDAOTest : BaseDatabaseTest() { fun givenEstablishedMLSConversationExists_whenGettingMLSGroupIdByUserId_thenReturnsMLSGroupId() = runTest { // given val expected = (conversationEntity4.protocolInfo as ConversationEntity.ProtocolInfo.MLS).groupId - userDAO.upsertUser(user1) - userDAO.upsertUser(user2) conversationDAO.insertConversation(conversationEntity1.copy(id = user1.id, type = ConversationEntity.Type.SELF)) conversationDAO.insertConversation(conversationEntity4) @@ -1659,6 +1658,7 @@ class ConversationDAOTest : BaseDatabaseTest() { listOf( MemberEntity(user1.id, MemberEntity.Role.Admin), MemberEntity(user2.id, MemberEntity.Role.Admin), + MemberEntity(selfUserId, MemberEntity.Role.Admin), ), conversationEntity4.id ) @@ -1666,6 +1666,21 @@ class ConversationDAOTest : BaseDatabaseTest() { assertEquals(expected, conversationDAO.getMLSGroupIdByUserId(user1.id)) } + @Test + fun givenEstablishedMLSConversationExistsButSelfUserIsNotMember_whenGettingMLSGroupIdByUserId_thenNull() = runTest { + // given + conversationDAO.insertConversation(conversationEntity1.copy(id = user1.id, type = ConversationEntity.Type.SELF)) + conversationDAO.insertConversation(conversationEntity4) + memberDAO.insertMembersWithQualifiedId( + listOf( + MemberEntity(user1.id, MemberEntity.Role.Admin), + MemberEntity(user2.id, MemberEntity.Role.Admin), + ), + conversationEntity4.id + ) + // then + assertEquals(null, conversationDAO.getMLSGroupIdByUserId(user1.id)) + } @Test fun givenMLSSelfConversationDoesNotExists_whenGettingE2EIClientInfoByClientId_thenShouldReturnNull() = runTest {