diff --git a/changelog.d/6109.bugfix b/changelog.d/6109.bugfix new file mode 100644 index 0000000000..43b1d610c7 --- /dev/null +++ b/changelog.d/6109.bugfix @@ -0,0 +1 @@ +Fix sending multiple invites to a room reaching only one or two people diff --git a/vector/src/main/java/im/vector/app/features/invite/InviteUsersToRoomViewModel.kt b/vector/src/main/java/im/vector/app/features/invite/InviteUsersToRoomViewModel.kt index cad46e06a1..63cf666f30 100644 --- a/vector/src/main/java/im/vector/app/features/invite/InviteUsersToRoomViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/invite/InviteUsersToRoomViewModel.kt @@ -28,8 +28,9 @@ import im.vector.app.core.resources.StringProvider import im.vector.app.features.userdirectory.PendingSelection import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map -import kotlinx.coroutines.launch +import kotlinx.coroutines.flow.onCompletion import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.getRoom @@ -55,39 +56,38 @@ class InviteUsersToRoomViewModel @AssistedInject constructor( } private fun inviteUsersToRoom(selections: Set) { - viewModelScope.launch { - _viewEvents.post(InviteUsersToRoomViewEvents.Loading) - selections.asFlow() - .map { user -> - when (user) { - is PendingSelection.UserPendingSelection -> room.membershipService().invite(user.user.userId, null) - is PendingSelection.ThreePidPendingSelection -> room.membershipService().invite3pid(user.threePid) - } + _viewEvents.post(InviteUsersToRoomViewEvents.Loading) + selections.asFlow() + .map { user -> + when (user) { + is PendingSelection.UserPendingSelection -> room.membershipService().invite(user.user.userId, null) + is PendingSelection.ThreePidPendingSelection -> room.membershipService().invite3pid(user.threePid) } - .catch { cause -> - _viewEvents.post(InviteUsersToRoomViewEvents.Failure(cause)) + }.onCompletion { error -> + if (error != null) return@onCompletion + + val successMessage = when (selections.size) { + 1 -> stringProvider.getString( + R.string.invitation_sent_to_one_user, + selections.first().getBestName() + ) + 2 -> stringProvider.getString( + R.string.invitations_sent_to_two_users, + selections.first().getBestName(), + selections.last().getBestName() + ) + else -> stringProvider.getQuantityString( + R.plurals.invitations_sent_to_one_and_more_users, + selections.size - 1, + selections.first().getBestName(), + selections.size - 1 + ) } - .collect { - val successMessage = when (selections.size) { - 1 -> stringProvider.getString( - R.string.invitation_sent_to_one_user, - selections.first().getBestName() - ) - 2 -> stringProvider.getString( - R.string.invitations_sent_to_two_users, - selections.first().getBestName(), - selections.last().getBestName() - ) - else -> stringProvider.getQuantityString( - R.plurals.invitations_sent_to_one_and_more_users, - selections.size - 1, - selections.first().getBestName(), - selections.size - 1 - ) - } - _viewEvents.post(InviteUsersToRoomViewEvents.Success(successMessage)) - } - } + _viewEvents.post(InviteUsersToRoomViewEvents.Success(successMessage)) + } + .catch { cause -> + _viewEvents.post(InviteUsersToRoomViewEvents.Failure(cause)) + }.launchIn(viewModelScope) } fun getUserIdsOfRoomMembers(): Set {