diff --git a/app/src/main/java/org/futo/circles/feature/people/PeopleDataSource.kt b/app/src/main/java/org/futo/circles/feature/people/PeopleDataSource.kt index cef1b01c2acf09f3b9f08dfed43ec8f324c7bbc3..bd0b1f1831d8b78a169b26fadb159d6a9798ba24 100644 --- a/app/src/main/java/org/futo/circles/feature/people/PeopleDataSource.kt +++ b/app/src/main/java/org/futo/circles/feature/people/PeopleDataSource.kt @@ -8,39 +8,29 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.withContext -import org.futo.circles.core.extensions.getRoomOwner import org.futo.circles.core.feature.room.knoks.KnockRequestsDataSource import org.futo.circles.core.feature.select_users.SearchUserDataSource -import org.futo.circles.core.feature.workspace.SharedCircleDataSource -import org.futo.circles.core.feature.workspace.SpacesTreeAccountDataSource -import org.futo.circles.core.model.CIRCLES_SPACE_ACCOUNT_DATA_KEY -import org.futo.circles.core.provider.MatrixSessionProvider -import org.futo.circles.core.utils.getJoinedRoomById import org.futo.circles.core.utils.getSpacesLiveData -import org.futo.circles.core.utils.getTimelineRoomFor +import org.futo.circles.feature.people.category.PeopleCategoryDataSource import org.futo.circles.mapping.toPeopleUserListItem import org.futo.circles.model.PeopleCategoryListItem import org.futo.circles.model.PeopleListItem import org.futo.circles.model.PeopleRequestNotificationListItem import org.matrix.android.sdk.api.session.room.model.Membership -import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.user.model.User import javax.inject.Inject class PeopleDataSource @Inject constructor( private val searchUserDataSource: SearchUserDataSource, - private val sharedCircleDataSource: SharedCircleDataSource, private val knockRequestsDataSource: KnockRequestsDataSource, - private val spacesTreeAccountDataSource: SpacesTreeAccountDataSource + private val peopleCategoryDataSource: PeopleCategoryDataSource ) { - private val session = MatrixSessionProvider.getSessionOrThrow() - private val profileRoomId = sharedCircleDataSource.getSharedCirclesSpaceId() ?: "" - private fun getKnockRequestCountFlow(): Flow<Int> = - knockRequestsDataSource.getKnockRequestsListItemsLiveData(profileRoomId)?.map { - it.size - }?.asFlow() ?: flowOf() + knockRequestsDataSource.getKnockRequestsListItemsLiveData(peopleCategoryDataSource.getProfileRoomId()) + ?.map { + it.size + }?.asFlow() ?: flowOf() private fun getProfileSpaceInvitesCountFlow() = getSpacesLiveData(listOf(Membership.INVITE)).map { it.size }.asFlow() @@ -49,7 +39,7 @@ class PeopleDataSource @Inject constructor( suspend fun getPeopleList(query: String) = combine( searchUserDataSource.searchKnownUsers(query), searchUserDataSource.searchSuggestions(query), - getIgnoredUserFlow(), + peopleCategoryDataSource.getIgnoredUserFlow(), getKnockRequestCountFlow(), getProfileSpaceInvitesCountFlow() ) { knowUsers, suggestions, ignoredUsers, knocksCount, profileInvitesCount -> @@ -69,9 +59,6 @@ class PeopleDataSource @Inject constructor( searchUserDataSource.loadAllRoomMembersIfNeeded() } - private fun getIgnoredUserFlow() = session.userService().getIgnoredUsersLive().asFlow() - - private fun buildList( query: String, knowUsers: List<User>, @@ -106,55 +93,25 @@ class PeopleDataSource @Inject constructor( ): List<PeopleListItem> { val list = mutableListOf<PeopleListItem>() val requestsCount = knocksCount + profileInvitesCount - val knownIds = knowUsers.map { it.userId } - val followingUsersIds = getPeopleImFollowingIds() - val followersUsersIds = getFollowersIds() - val connectionsIds = - knowUsers.mapNotNull { if (isConnection(it.userId)) it.userId else null } - val otherMemberIds = - knownIds - connectionsIds.toSet() - followersUsersIds.toSet() - followingUsersIds.toSet() + val followingUsers = peopleCategoryDataSource.getPeopleImFollowing() + val followersUsers = peopleCategoryDataSource.getFollowers() + val connections = peopleCategoryDataSource.getMyConnections(knowUsers) + val otherMembers = peopleCategoryDataSource.getOtherUsers( + knowUsers, + connections, + followersUsers, + followingUsers + ) list.apply { if (requestsCount > 0) add(PeopleRequestNotificationListItem(requestsCount)) - add(PeopleCategoryListItem.connections.copy(count = connectionsIds.size)) - add(PeopleCategoryListItem.followingUsers.copy(count = followingUsersIds.size)) - add(PeopleCategoryListItem.followersUsers.copy(count = followersUsersIds.size)) - add(PeopleCategoryListItem.others.copy(count = otherMemberIds.size)) + add(PeopleCategoryListItem.connections.copy(count = connections.size)) + add(PeopleCategoryListItem.followingUsers.copy(count = followingUsers.size)) + add(PeopleCategoryListItem.followersUsers.copy(count = followersUsers.size)) + add(PeopleCategoryListItem.others.copy(count = otherMembers.size)) add(PeopleCategoryListItem.ignored.copy(count = ignoredUsers.size)) } return list } - //All the joined members (except me) in all of my circle timeline rooms - private fun getFollowersIds(): List<String> { - val myCirclesSpace = getMyCirclesSpaceSummary() ?: return emptyList() - val myTimelinesFollowers = myCirclesSpace.spaceChildren?.mapNotNull { - getTimelineRoomFor(it.childRoomId)?.roomSummary()?.otherMemberIds - }?.flatMap { it.toSet() } ?: emptyList() - - return myTimelinesFollowers - } - - //All the creators of all the timeline rooms that I'm following in my circles - private fun getPeopleImFollowingIds(): List<String> { - val myCirclesSpace = getMyCirclesSpaceSummary() ?: return emptyList() - val peopleIamFollowing = myCirclesSpace.spaceChildren?.mapNotNull { - getJoinedRoomById(it.childRoomId)?.roomSummary()?.spaceChildren?.mapNotNull { - getRoomOwner(it.childRoomId)?.userId?.takeIf { it != session.myUserId } - } - }?.flatMap { it.toSet() } ?: emptyList() - - return peopleIamFollowing - } - - private fun getMyCirclesSpaceSummary(): RoomSummary? { - val circlesSpaceId = spacesTreeAccountDataSource.getRoomIdByKey( - CIRCLES_SPACE_ACCOUNT_DATA_KEY - ) ?: "" - return getJoinedRoomById(circlesSpaceId)?.roomSummary() - } - - private fun isConnection(userId: String) = - sharedCircleDataSource.getSharedCircleFor(userId) != null - } \ No newline at end of file