Wait for a room with joined state in /sync after creating it (#3421)

This commit is contained in:
Jorge Martin Espinosa 2024-09-09 09:36:49 +02:00 committed by GitHub
parent 48dae851df
commit daa5130300

View file

@ -27,6 +27,7 @@ import io.element.android.libraries.matrix.api.notification.NotificationService
import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService
import io.element.android.libraries.matrix.api.oidc.AccountManagementAction import io.element.android.libraries.matrix.api.oidc.AccountManagementAction
import io.element.android.libraries.matrix.api.pusher.PushersService import io.element.android.libraries.matrix.api.pusher.PushersService
import io.element.android.libraries.matrix.api.room.CurrentUserMembership
import io.element.android.libraries.matrix.api.room.MatrixRoom import io.element.android.libraries.matrix.api.room.MatrixRoom
import io.element.android.libraries.matrix.api.room.MatrixRoomInfo import io.element.android.libraries.matrix.api.room.MatrixRoomInfo
import io.element.android.libraries.matrix.api.room.RoomMembershipObserver import io.element.android.libraries.matrix.api.room.RoomMembershipObserver
@ -245,18 +246,20 @@ class RustMatrixClient(
} }
/** /**
* Wait for the room to be available in the room list. * Wait for the room to be available in the room list, with a membership for the current user of [CurrentUserMembership.JOINED].
* @param roomIdOrAlias the room id or alias to wait for * @param roomIdOrAlias the room id or alias to wait for
* @param timeout the timeout to wait for the room to be available * @param timeout the timeout to wait for the room to be available
* @throws TimeoutCancellationException if the room is not available after the timeout * @throws TimeoutCancellationException if the room is not available after the timeout
*/ */
private suspend fun awaitRoom(roomIdOrAlias: RoomIdOrAlias, timeout: Duration): RoomSummary { private suspend fun awaitJoinedRoom(roomIdOrAlias: RoomIdOrAlias, timeout: Duration): RoomSummary {
val predicate: (List<RoomSummary>) -> Boolean = when (roomIdOrAlias) { val predicate: (List<RoomSummary>) -> Boolean = when (roomIdOrAlias) {
is RoomIdOrAlias.Alias -> { roomSummaries: List<RoomSummary> -> is RoomIdOrAlias.Alias -> { roomSummaries: List<RoomSummary> ->
roomSummaries.flatMap { it.aliases }.contains(roomIdOrAlias.roomAlias) val found = roomSummaries.find { it.aliases.contains(roomIdOrAlias.roomAlias) }
found != null && found.currentUserMembership == CurrentUserMembership.JOINED
} }
is RoomIdOrAlias.Id -> { roomSummaries: List<RoomSummary> -> is RoomIdOrAlias.Id -> { roomSummaries: List<RoomSummary> ->
roomSummaries.map { it.roomId }.contains(roomIdOrAlias.roomId) val found = roomSummaries.find { it.roomId == roomIdOrAlias.roomId }
found != null && found.currentUserMembership == CurrentUserMembership.JOINED
} }
} }
return withTimeout(timeout) { return withTimeout(timeout) {
@ -306,7 +309,7 @@ class RustMatrixClient(
val roomId = RoomId(client.createRoom(rustParams)) val roomId = RoomId(client.createRoom(rustParams))
// Wait to receive the room back from the sync but do not returns failure if it fails. // Wait to receive the room back from the sync but do not returns failure if it fails.
try { try {
awaitRoom(roomId.toRoomIdOrAlias(), 30.seconds) awaitJoinedRoom(roomId.toRoomIdOrAlias(), 30.seconds)
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e, "Timeout waiting for the room to be available in the room list") Timber.e(e, "Timeout waiting for the room to be available in the room list")
} }
@ -361,7 +364,7 @@ class RustMatrixClient(
runCatching { runCatching {
client.joinRoomById(roomId.value).destroy() client.joinRoomById(roomId.value).destroy()
try { try {
awaitRoom(roomId.toRoomIdOrAlias(), 10.seconds) awaitJoinedRoom(roomId.toRoomIdOrAlias(), 10.seconds)
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e, "Timeout waiting for the room to be available in the room list") Timber.e(e, "Timeout waiting for the room to be available in the room list")
null null
@ -376,7 +379,7 @@ class RustMatrixClient(
serverNames = serverNames, serverNames = serverNames,
).destroy() ).destroy()
try { try {
awaitRoom(roomIdOrAlias, 10.seconds) awaitJoinedRoom(roomIdOrAlias, 10.seconds)
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e, "Timeout waiting for the room to be available in the room list") Timber.e(e, "Timeout waiting for the room to be available in the room list")
null null
@ -503,7 +506,7 @@ class RustMatrixClient(
var room = getRoom(roomId) var room = getRoom(roomId)
if (room == null) { if (room == null) {
emit(Optional.empty()) emit(Optional.empty())
awaitRoom(roomId.toRoomIdOrAlias(), INFINITE) awaitJoinedRoom(roomId.toRoomIdOrAlias(), INFINITE)
room = getRoom(roomId) room = getRoom(roomId)
} }
room?.use { room?.use {