Skip to content

Commit

Permalink
chore: one on one conversation details [WPB-15479] (#3260)
Browse files Browse the repository at this point in the history
* chore: one on one conversation details

* detekt fix
  • Loading branch information
Garzas authored Jan 30, 2025
1 parent 952e034 commit 76d2206
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ interface ConversationRepository {
suspend fun addConversationToDeleteQueue(conversationId: ConversationId)
suspend fun removeConversationFromDeleteQueue(conversationId: ConversationId)
suspend fun getConversationsDeleteQueue(): List<ConversationId>
suspend fun observeOneToOneConversationDetailsWithOtherUser(
otherUserId: UserId
): Flow<Either<StorageFailure, ConversationDetails.OneOne>>
}

@Suppress("LongParameterList", "TooManyFunctions", "LargeClass")
Expand Down Expand Up @@ -793,6 +796,14 @@ internal class ConversationDataSource internal constructor(
.mapRight { conversationMapper.fromDaoModel(it) }
}

override suspend fun observeOneToOneConversationDetailsWithOtherUser(
otherUserId: UserId
): Flow<Either<StorageFailure, ConversationDetails.OneOne>> {
return conversationDAO.observeOneOnOneConversationDetailsWithOtherUser(otherUserId.toDao())
.map { it?.let { conversationMapper.fromDaoModelToDetails(it) as? ConversationDetails.OneOne } }
.wrapStorageRequest()
}

override suspend fun getOneOnOneConversationsWithOtherUser(
otherUserId: UserId,
protocol: Conversation.Protocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ class ConversationScope internal constructor(
val getConversationDetails: GetConversationUseCase
get() = GetConversationUseCase(conversationRepository)

val getOneToOneConversation: GetOneToOneConversationUseCase
get() = GetOneToOneConversationUseCase(conversationRepository)
val getOneToOneConversation: GetOneToOneConversationDetailsUseCase
get() = GetOneToOneConversationDetailsUseCase(conversationRepository)

val observeConversationListDetails: ObserveConversationListDetailsUseCase
get() = ObserveConversationListDetailsUseCaseImpl(conversationRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.wire.kalium.logic.feature.conversation

import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.conversation.ConversationDetails
import com.wire.kalium.logic.data.conversation.ConversationRepository
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.functional.fold
Expand All @@ -35,18 +36,18 @@ import kotlinx.coroutines.flow.map
* @return [Result.Success] with [Conversation] in case of success,
* or [Result.Failure] if something went wrong - can't get data from local DB.
*/
class GetOneToOneConversationUseCase internal constructor(
class GetOneToOneConversationDetailsUseCase internal constructor(
private val conversationRepository: ConversationRepository,
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl
) {

suspend operator fun invoke(otherUserId: UserId): Flow<Result> =
conversationRepository.observeOneToOneConversationWithOtherUser(otherUserId)
conversationRepository.observeOneToOneConversationDetailsWithOtherUser(otherUserId)
.map { result -> result.fold({ Result.Failure }, { Result.Success(it) }) }
.flowOn(dispatchers.io)

sealed class Result {
data class Success(val conversation: Conversation) : Result()
data class Success(val conversation: ConversationDetails.OneOne) : Result()
data object Failure : Result()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ class ConversationRepositoryTest {
}

@Test
fun givenUserHasKnownContactAndConversation_WhenGettingConversationDetailsByExistingConversation_ReturnTheCorrectConversation() =
fun givenUserHasKnownContactAndConversation_WhenGettingConversationByExistingConversation_ReturnTheCorrectConversation() =
runTest {
// given
val (_, conversationRepository) = Arrangement()
Expand All @@ -436,6 +436,25 @@ class ConversationRepositoryTest {
}
}

@Test
fun givenUserHasKnownContactAndConversation_WhenGettingConversationDetailsByExistingConversation_ReturnTheCorrectConversationDetails() =
runTest {
// given
val (_, conversationRepository) = Arrangement()
.withSelfUserFlow(flowOf(TestUser.SELF))
.withExpectedConversationDetailsWithOtherUser(TestConversation.VIEW_ONE_ON_ONE.copy(otherUserId = OTHER_USER_ID.toDao()))
.withExpectedOtherKnownUser(TestUser.OTHER)
.arrange()

// when
conversationRepository.observeOneToOneConversationDetailsWithOtherUser(OTHER_USER_ID).test {
val result = awaitItem()
// then
assertIs<Either.Right<ConversationDetails.OneOne>>(result)
awaitComplete()
}
}

@Test
fun whenCallingUpdateMutedStatusRemotely_thenShouldDelegateCallToConversationApi() = runTest {
val (arrangement, conversationRepository) = Arrangement()
Expand Down Expand Up @@ -1528,6 +1547,12 @@ class ConversationRepositoryTest {
}.returns(flowOf(conversation))
}

suspend fun withExpectedConversationDetailsWithOtherUser(conversation: ConversationViewEntity?) = apply {
coEvery {
conversationDAO.observeOneOnOneConversationDetailsWithOtherUser(any())
}.returns(flowOf(conversation))
}

suspend fun withUpdateConversationMemberStateResult(response: NetworkResponse<Unit>) = apply {
coEvery {
conversationApi.updateConversationMemberState(any(), any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.conversation.Conversation.Member
import com.wire.kalium.logic.data.conversation.Conversation.ProtocolInfo
import com.wire.kalium.logic.data.conversation.ConversationRepositoryTest
import com.wire.kalium.logic.data.conversation.ConversationRepositoryTest.Companion.OTHER_USER_ID
import com.wire.kalium.logic.data.conversation.MutedConversationStatus
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.GroupID
import com.wire.kalium.logic.data.id.toApi
import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.data.mls.CipherSuite
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.network.api.authenticated.conversation.ConvProtocol
Expand Down Expand Up @@ -300,6 +302,7 @@ object TestConversation {
val ENTITY_GROUP = ENTITY.copy(
type = ConversationEntity.Type.GROUP
)

val VIEW_ENTITY = ConversationViewEntity(
id = ENTITY_ID,
name = "convo name",
Expand Down Expand Up @@ -348,6 +351,10 @@ object TestConversation {
folderName = null
)

val VIEW_ONE_ON_ONE = VIEW_ENTITY.copy(
type = ConversationEntity.Type.ONE_ON_ONE,
)

val MLS_PROTOCOL_INFO = ProtocolInfo.MLS(
GROUP_ID,
ProtocolInfo.MLSCapable.GroupState.PENDING_JOIN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,7 @@ SELECT * FROM ConversationDetails WHERE qualifiedId = ?;

selectConversationDetailsByGroupId:
SELECT * FROM ConversationDetails WHERE mls_group_id = ?;

selectActiveOneOnOneConversationDetails:
SELECT * FROM ConversationDetails
WHERE qualifiedId = (SELECT active_one_on_one_conversation_id FROM User WHERE qualified_id = :user_id);
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ interface ConversationDAO {
suspend fun getEstablishedSelfMLSGroupId(): String?

suspend fun selectGroupStatusMembersNamesAndHandles(groupID: String): EpochChangesDataEntity?
suspend fun observeOneOnOneConversationDetailsWithOtherUser(userId: UserIDEntity): Flow<ConversationViewEntity?>
}

data class NameAndHandleEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ internal class ConversationDAOImpl internal constructor(
.flowOn(coroutineContext)
}

override suspend fun observeOneOnOneConversationDetailsWithOtherUser(userId: UserIDEntity): Flow<ConversationViewEntity?> {
return conversationDetailsQueries.selectActiveOneOnOneConversationDetails(userId, conversationMapper::fromViewToModel)
.asFlow()
.mapToOneOrNull()
.flowOn(coroutineContext)
}

override suspend fun getConversationProtocolInfo(qualifiedID: QualifiedIDEntity): ConversationEntity.ProtocolInfo? =
withContext(coroutineContext) {
conversationQueries.selectProtocolInfoByQualifiedId(qualifiedID, conversationMapper::mapProtocolInfo).executeAsOneOrNull()
Expand Down

0 comments on commit 76d2206

Please sign in to comment.