Skip to content

Commit

Permalink
fix: persist searched team members [WPB-5262] (#2179)
Browse files Browse the repository at this point in the history
  • Loading branch information
saleniuk authored Nov 1, 2023
1 parent 8f733ec commit 0df069c
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,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.UserEntity
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 @@ -158,7 +160,21 @@ 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.upsertTeamMembers(
teamMembers.map { userProfileDTO ->
userMapper.fromUserProfileDtoToUserEntity(
userProfile = userProfileDTO,
connectionState = ConnectionEntity.State.ACCEPTED,
userTypeEntity = UserTypeEntity.STANDARD
)
}
)

otherUsers.map { userProfileDTO ->
publicUserMapper.fromUserProfileDtoToOtherUser(
userDetailResponse = userProfileDTO,
userType = userTypeMapper.fromTeamAndDomain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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 @@ -250,9 +251,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.upsertTeamMembers(
teamMembers.map { userProfileDTO ->
userMapper.fromUserProfileDtoToUserEntity(
Expand Down Expand Up @@ -280,14 +281,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.getUsersByQualifiedIDList(ids.map { it.toDao() })
Expand Down
Loading

0 comments on commit 0df069c

Please sign in to comment.