Await room: first attempt to wait for a room to be ready

This commit is contained in:
ganfra 2023-07-04 18:19:06 +02:00
parent c05c414099
commit d59f59e9f6
12 changed files with 186 additions and 44 deletions

View file

@ -31,14 +31,16 @@ import io.element.android.libraries.matrix.api.sync.SyncService
import io.element.android.libraries.matrix.api.user.MatrixSearchUserResults
import io.element.android.libraries.matrix.api.user.MatrixUser
import io.element.android.libraries.matrix.api.verification.SessionVerificationService
import kotlinx.coroutines.TimeoutCancellationException
import java.io.Closeable
import kotlin.time.Duration
interface MatrixClient : Closeable {
val sessionId: SessionId
val roomSummaryDataSource: RoomSummaryDataSource
val mediaLoader: MatrixMediaLoader
fun getRoom(roomId: RoomId): MatrixRoom?
fun findDM(userId: UserId): MatrixRoom?
suspend fun getRoom(roomId: RoomId): MatrixRoom?
suspend fun findDM(userId: UserId): MatrixRoom?
suspend fun ignoreUser(userId: UserId): Result<Unit>
suspend fun unignoreUser(userId: UserId): Result<Unit>
suspend fun createRoom(createRoomParams: CreateRoomParameters): Result<RoomId>

View file

@ -25,8 +25,8 @@ interface RoomSummaryDataSource {
data class Loaded(val numberOfRooms: Int): LoadingState()
}
fun updateAllRoomsVisibleRange(range: IntRange)
fun allRoomsLoadingState(): StateFlow<LoadingState>
fun allRooms(): StateFlow<List<RoomSummary>>
fun inviteRooms(): StateFlow<List<RoomSummary>>
fun updateRoomListVisibleRange(range: IntRange)
}

View file

@ -57,12 +57,15 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import org.matrix.rustcomponents.sdk.Client
import org.matrix.rustcomponents.sdk.ClientDelegate
import org.matrix.rustcomponents.sdk.Room
import org.matrix.rustcomponents.sdk.RoomListItem
import org.matrix.rustcomponents.sdk.use
import timber.log.Timber
import java.io.File
@ -91,7 +94,6 @@ class RustMatrixClient constructor(
)
private val notificationService = RustNotificationService(client)
private val clientDelegate = object : ClientDelegate {
override fun didReceiveAuthError(isSoftLogout: Boolean) {
//TODO handle this
@ -127,9 +129,16 @@ class RustMatrixClient constructor(
}.launchIn(sessionCoroutineScope)
}
override fun getRoom(roomId: RoomId): MatrixRoom? {
val roomListItem = roomListService.roomOrNull(roomId.value) ?: return null
val fullRoom = roomListItem.fullRoom()
override suspend fun getRoom(roomId: RoomId): MatrixRoom? {
var cachedPairOfRoom = pairOfRoom(roomId)
if (cachedPairOfRoom == null) {
roomSummaryDataSource.allRoomsLoadingState().firstOrNull {
it is RoomSummaryDataSource.LoadingState.Loaded
}
cachedPairOfRoom = pairOfRoom(roomId)
}
if (cachedPairOfRoom == null) return null
val (roomListItem, fullRoom) = cachedPairOfRoom
return RustMatrixRoom(
sessionId = sessionId,
roomListItem = roomListItem,
@ -141,7 +150,19 @@ class RustMatrixClient constructor(
)
}
override fun findDM(userId: UserId): MatrixRoom? {
private suspend fun pairOfRoom(roomId: RoomId): Pair<RoomListItem, Room>? {
Timber.v("Resume get pair of room for $roomId")
val cachedRoomListItem = roomListService.roomOrNull(roomId.value)
val fullRoom = cachedRoomListItem?.fullRoom()
Timber.v("Finish get pair of room for $roomId")
return if (cachedRoomListItem == null || fullRoom == null) {
null
} else {
Pair(cachedRoomListItem, fullRoom)
}
}
override suspend fun findDM(userId: UserId): MatrixRoom? {
val roomId = client.getDmRoom(userId.value)?.use { RoomId(it.id()) }
return roomId?.let { getRoom(it) }
}

View file

@ -25,6 +25,7 @@ import org.matrix.rustcomponents.sdk.RoomList
import org.matrix.rustcomponents.sdk.RoomListEntriesListener
import org.matrix.rustcomponents.sdk.RoomListEntriesUpdate
import org.matrix.rustcomponents.sdk.RoomListEntry
import org.matrix.rustcomponents.sdk.RoomListException
import org.matrix.rustcomponents.sdk.RoomListItem
import org.matrix.rustcomponents.sdk.RoomListLoadingState
import org.matrix.rustcomponents.sdk.RoomListLoadingStateListener
@ -60,8 +61,8 @@ fun RoomList.entriesFlow(onInitialList: suspend (List<RoomListEntry>) -> Unit):
fun RoomListService.roomOrNull(roomId: String): RoomListItem? {
return try {
room(roomId)
} catch (failure: Throwable) {
Timber.e(failure, "Failed finding room with id=$roomId")
} catch (exception: RoomListException) {
Timber.e(exception, "Failed finding room with id=$roomId")
return null
}
}

View file

@ -90,7 +90,7 @@ internal class RustRoomSummaryDataSource(
return allRoomsLoadingState
}
override fun updateRoomListVisibleRange(range: IntRange) {
override fun updateAllRoomsVisibleRange(range: IntRange) {
Timber.v("setVisibleRange=$range")
sessionCoroutineScope.launch {
try {

View file

@ -68,7 +68,7 @@ class FakeMatrixClient(
return getRoomResults[roomId]
}
override fun findDM(userId: UserId): MatrixRoom? {
override suspend fun findDM(userId: UserId): MatrixRoom? {
return findDmResult
}

View file

@ -54,7 +54,7 @@ class FakeRoomSummaryDataSource : RoomSummaryDataSource {
var latestSlidingSyncRange: IntRange? = null
private set
override fun updateRoomListVisibleRange(range: IntRange) {
override fun updateAllRoomsVisibleRange(range: IntRange) {
latestSlidingSyncRange = range
}
}