Skip to content

Commit

Permalink
refactor: improve clarity in getting connection requests (#2195)
Browse files Browse the repository at this point in the history
This commit consists of multiple changes that contribute to clarifying the handling of connections within various components in our architecture.
First, the return type of the `fromDaoToConversationDetails` and `observeConnectionRequestList` functions in the `ConnectionMapper` and `ConnectionRepository` classes respectively, have been updated to explicitly return the `Connection` nested class from `ConversationDetails`. This enhances code readability by better illustrating what specific type of conversation details the function is dealing with.
Second, in the `ConversationScope` class, a deprecated message has been added to the `observeConnectionList` interface, explaining that the name is misleading and not descriptive of its function. This method was replaced by `observePendingConnectionRequestsUseCase`, which better illustrates its function of observing connection requests.
Lastly, `ObserveConnectionListUseCase.kt` was renamed as `ObservePendingConnectionRequestsUseCase.kt`, further enhancing the consistency and readability of our codebase. With these changes, any user can understand that this class is specifically related to observing pending connection requests.

Co-authored-by: Yamil Medina <[email protected]>
  • Loading branch information
2 people authored and augustocdias committed Nov 9, 2023
1 parent 3f798c0 commit 4096453
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlinx.datetime.toInstant
interface ConnectionMapper {
fun fromApiToDao(state: ConnectionDTO): ConnectionEntity
fun fromDaoToModel(connection: ConnectionEntity): Connection
fun fromDaoToConversationDetails(connection: ConnectionEntity): ConversationDetails
fun fromDaoToConversationDetails(connection: ConnectionEntity): ConversationDetails.Connection
fun fromApiToModel(state: ConnectionDTO): Connection
fun modelToDao(state: Connection): ConnectionEntity
}
Expand Down Expand Up @@ -72,7 +72,9 @@ internal class ConnectionMapperImpl(
)
}

override fun fromDaoToConversationDetails(connection: ConnectionEntity): ConversationDetails = with(connection) {
override fun fromDaoToConversationDetails(
connection: ConnectionEntity
): ConversationDetails.Connection = with(connection) {
ConversationDetails.Connection(
conversationId = qualifiedConversationId.toModel(),
otherUser = otherUser?.let { userMapper.fromUserDetailsEntityToOtherUser(it) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ interface ConnectionRepository {
suspend fun getConnections(): Either<StorageFailure, Flow<List<ConversationDetails>>>
suspend fun insertConnectionFromEvent(event: Event.User.NewConnection): Either<CoreFailure, Unit>
suspend fun observeConnectionList(): Flow<List<Connection>>
suspend fun observeConnectionRequestList(): Flow<List<ConversationDetails>>
suspend fun observeConnectionRequestList(): Flow<List<ConversationDetails.Connection>>
suspend fun observeConnectionRequestsForNotification(): Flow<List<ConversationDetails>>
suspend fun setConnectionAsNotified(userId: UserId)
suspend fun setAllConnectionsAsNotified()
Expand Down Expand Up @@ -155,7 +155,7 @@ internal class ConnectionDataSource(
observeConnectionRequestList()
}

override suspend fun observeConnectionRequestList(): Flow<List<ConversationDetails>> {
override suspend fun observeConnectionRequestList(): Flow<List<ConversationDetails.Connection>> {
return connectionDAO.getConnectionRequests().map { connections ->
connections
.map { connection ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,41 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map

@Deprecated(
"Name is misleading and will be removed in the future",
ReplaceWith("ObservePendingConnectionRequestsUseCase")
)
typealias ObserveConnectionListUseCase = ObservePendingConnectionRequestsUseCase

/**
* Use Case that listen to any user connection changes
* Use Case that lists the current pending connection requests.
*
* Only connections that are [ConnectionState.PENDING] or [ConnectionState.SENT] are returned.
*
* @see ConnectionState
*/
fun interface ObserveConnectionListUseCase {
fun interface ObservePendingConnectionRequestsUseCase {
/**
* Use case [ObserveConnectionListUseCase] operation
* Use case [ObservePendingConnectionRequestsUseCase] operation
*
* @return a [Flow<List<Connection>>] containing all current connections
* @return a [Flow] with a list of [ConversationDetails] containing all current connections
*/
suspend operator fun invoke(): Flow<List<ConversationDetails>>
suspend operator fun invoke(): Flow<List<ConversationDetails.Connection>>
}

internal class ObserveConnectionListUseCaseImpl internal constructor(
internal class ObservePendingConnectionRequestsUseCaseImpl internal constructor(
private val connectionRepository: ConnectionRepository,
) : ObserveConnectionListUseCase {
) : ObservePendingConnectionRequestsUseCase {

override suspend operator fun invoke(): Flow<List<ConversationDetails>> {
override suspend operator fun invoke(): Flow<List<ConversationDetails.Connection>> {
return connectionRepository.observeConnectionRequestList()
.map { conversationDetails ->
/** Ignored connections are filtered because they should not be visible
* in conversation list
*/
conversationDetails
.filter {
when (it) {
is ConversationDetails.Connection -> it.connection.status != ConnectionState.IGNORED
else -> false
}
}
}
.distinctUntilChanged()
conversationDetails.filter {
it.connection.status != ConnectionState.IGNORED
}
}.distinctUntilChanged()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import com.wire.kalium.logic.di.UserStorage
import com.wire.kalium.logic.feature.connection.MarkConnectionRequestAsNotifiedUseCase
import com.wire.kalium.logic.feature.connection.MarkConnectionRequestAsNotifiedUseCaseImpl
import com.wire.kalium.logic.feature.connection.ObserveConnectionListUseCase
import com.wire.kalium.logic.feature.connection.ObserveConnectionListUseCaseImpl
import com.wire.kalium.logic.feature.connection.ObservePendingConnectionRequestsUseCase
import com.wire.kalium.logic.feature.connection.ObservePendingConnectionRequestsUseCaseImpl
import com.wire.kalium.logic.feature.conversation.guestroomlink.CanCreatePasswordProtectedLinksUseCase
import com.wire.kalium.logic.feature.conversation.guestroomlink.GenerateGuestRoomLinkUseCase
import com.wire.kalium.logic.feature.conversation.guestroomlink.GenerateGuestRoomLinkUseCaseImpl
Expand Down Expand Up @@ -171,8 +172,15 @@ class ConversationScope internal constructor(
val updateConversationArchivedStatus: UpdateConversationArchivedStatusUseCase
get() = UpdateConversationArchivedStatusUseCaseImpl(conversationRepository)

@Deprecated(
"Name is misleading, and this field will be removed",
ReplaceWith("observePendingConnectionRequests")
)
val observeConnectionList: ObserveConnectionListUseCase
get() = ObserveConnectionListUseCaseImpl(connectionRepository)
get() = observePendingConnectionRequests

val observePendingConnectionRequests: ObservePendingConnectionRequestsUseCase
get() = ObservePendingConnectionRequestsUseCaseImpl(connectionRepository)

val markConnectionRequestAsNotified: MarkConnectionRequestAsNotifiedUseCase
get() = MarkConnectionRequestAsNotifiedUseCaseImpl(connectionRepository)
Expand Down

0 comments on commit 4096453

Please sign in to comment.