Skip to content

Commit

Permalink
fix: persist searched team members [WPB-5262] (#2184)
Browse files Browse the repository at this point in the history
* Commit with unresolved merge conflicts outside of

* resolved conflicts

* removed unused import
  • Loading branch information
saleniuk authored Nov 3, 2023
1 parent bfcae0a commit d5616b2
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.wire.kalium.logic.data.publicuser.model.UserSearchResult
import com.wire.kalium.logic.data.user.SelfUser
import com.wire.kalium.logic.data.user.UserDataSource
import com.wire.kalium.logic.data.user.UserMapper
import com.wire.kalium.logic.data.user.type.UserEntityTypeMapper
import com.wire.kalium.logic.di.MapperProvider
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.flatMap
Expand All @@ -35,11 +34,13 @@ import com.wire.kalium.logic.wrapApiRequest
import com.wire.kalium.network.api.base.authenticated.userDetails.ListUserRequest
import com.wire.kalium.network.api.base.authenticated.userDetails.UserDetailsApi
import com.wire.kalium.network.api.base.authenticated.userDetails.qualifiedIds
import com.wire.kalium.network.api.base.model.isTeamMember
import com.wire.kalium.persistence.dao.ConnectionEntity
import com.wire.kalium.persistence.dao.MetadataDAO
import com.wire.kalium.persistence.dao.QualifiedIDEntity
import com.wire.kalium.persistence.dao.UserDAO
import com.wire.kalium.persistence.dao.UserDetailsEntity
import com.wire.kalium.persistence.dao.UserTypeEntity
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterNotNull
Expand Down Expand Up @@ -93,7 +94,6 @@ internal class SearchUserRepositoryImpl(
private val userDetailsApi: UserDetailsApi,
private val userSearchAPiWrapper: UserSearchApiWrapper,
private val userMapper: UserMapper = MapperProvider.userMapper(),
private val userTypeEntityMapper: UserEntityTypeMapper = MapperProvider.userTypeEntityMapper()
) : SearchUserRepository {

override suspend fun searchKnownUsersByNameOrHandleOrEmail(
Expand Down Expand Up @@ -157,7 +157,22 @@ internal class SearchUserRepositoryImpl(
response.map { userProfileDTOList ->
val otherUserList = if (userProfileDTOList.isEmpty()) emptyList() else {
val selfUser = getSelfUser()
userProfileDTOList.map { userProfileDTO ->
val (teamMembers, otherUsers) = userProfileDTOList
.partition { it.isTeamMember(selfUser.teamId?.value, selfUser.id.domain) }

// We need to store all found team members locally and not return them as they will be "known" users from now on.
userDAO.upsertUsers(
teamMembers.map { userProfileDTO ->
userMapper.fromUserProfileDtoToUserEntity(
userProfile = userProfileDTO,
connectionState = ConnectionEntity.State.ACCEPTED,
userTypeEntity = userDAO.observeUserDetailsByQualifiedID(userProfileDTO.id.toDao())
.firstOrNull()?.userType ?: UserTypeEntity.STANDARD
)
}
)

otherUsers.map { userProfileDTO ->
userMapper.fromUserProfileDtoToOtherUser(userProfileDTO, selfUser)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.wire.kalium.network.api.base.authenticated.userDetails.UserDetailsApi
import com.wire.kalium.network.api.base.authenticated.userDetails.qualifiedIds
import com.wire.kalium.network.api.base.model.SelfUserDTO
import com.wire.kalium.network.api.base.model.UserProfileDTO
import com.wire.kalium.network.api.base.model.isTeamMember
import com.wire.kalium.persistence.dao.ConnectionEntity
import com.wire.kalium.persistence.dao.MetadataDAO
import com.wire.kalium.persistence.dao.QualifiedIDEntity
Expand Down Expand Up @@ -259,9 +260,9 @@ internal class UserDataSource internal constructor(
val selfUserDomain = selfUserId.domain
val selfUserTeamId = selfTeamIdProvider().getOrNull()?.value
val teamMembers = listUserProfileDTO
.filter { userProfileDTO -> isTeamMember(selfUserTeamId, userProfileDTO, selfUserDomain) }
.filter { userProfileDTO -> userProfileDTO.isTeamMember(selfUserTeamId, selfUserDomain) }
val otherUsers = listUserProfileDTO
.filter { userProfileDTO -> !isTeamMember(selfUserTeamId, userProfileDTO, selfUserDomain) }
.filter { userProfileDTO -> !userProfileDTO.isTeamMember(selfUserTeamId, selfUserDomain) }

userDAO.upsertUsers(
teamMembers.map { userProfileDTO ->
Expand Down Expand Up @@ -291,14 +292,6 @@ internal class UserDataSource internal constructor(
)
}

private fun isTeamMember(
selfUserTeamId: String?,
userProfileDTO: UserProfileDTO,
selfUserDomain: String?
) = (selfUserTeamId != null &&
userProfileDTO.teamId == selfUserTeamId &&
userProfileDTO.id.domain == selfUserDomain)

override suspend fun fetchUsersIfUnknownByIds(ids: Set<UserId>): Either<CoreFailure, Unit> = wrapStorageRequest {
val qualifiedIDList = ids.map { it.toDao() }
val knownUsers = userDAO.getUsersDetailsByQualifiedIDList(ids.map { it.toDao() })
Expand Down
Loading

0 comments on commit d5616b2

Please sign in to comment.