Merge pull request #6110 from vector-im/defect/jorgem/PSE-613-fix-multi-invites-to-room

Fix sending multiple invites to a room reaching only one or two people
This commit is contained in:
Benoit Marty 2022-05-23 09:39:47 +02:00 committed by GitHub
commit 71e14ea43b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 32 deletions

1
changelog.d/6109.bugfix Normal file
View file

@ -0,0 +1 @@
Fix sending multiple invites to a room reaching only one or two people

View file

@ -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<PendingSelection>) {
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<String> {