Browse Source

knock : add knock function to the matrix client

pull/3725/head
ganfra 2 weeks ago
parent
commit
ed5454bbb3
  1. 2
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/MatrixClient.kt
  2. 20
      libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt
  3. 6
      libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/FakeMatrixClient.kt

2
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/MatrixClient.kt

@ -65,7 +65,7 @@ interface MatrixClient : Closeable {
suspend fun removeAvatar(): Result<Unit> suspend fun removeAvatar(): Result<Unit>
suspend fun joinRoom(roomId: RoomId): Result<RoomSummary?> suspend fun joinRoom(roomId: RoomId): Result<RoomSummary?>
suspend fun joinRoomByIdOrAlias(roomIdOrAlias: RoomIdOrAlias, serverNames: List<String>): Result<RoomSummary?> suspend fun joinRoomByIdOrAlias(roomIdOrAlias: RoomIdOrAlias, serverNames: List<String>): Result<RoomSummary?>
suspend fun knockRoom(roomId: RoomId): Result<Unit> suspend fun knockRoom(roomId: RoomId): Result<RoomSummary?>
fun syncService(): SyncService fun syncService(): SyncService
fun sessionVerificationService(): SessionVerificationService fun sessionVerificationService(): SessionVerificationService
fun pushersService(): PushersService fun pushersService(): PushersService

20
libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt

@ -270,7 +270,7 @@ class RustMatrixClient(
return withTimeout(timeout) { return withTimeout(timeout) {
getRoomSummaryFlow(roomIdOrAlias) getRoomSummaryFlow(roomIdOrAlias)
.mapNotNull { optionalRoomSummary -> optionalRoomSummary.getOrNull() } .mapNotNull { optionalRoomSummary -> optionalRoomSummary.getOrNull() }
.filter { roomSummary -> roomSummary.info.currentUserMembership == CurrentUserMembership.JOINED } .filter { roomSummary -> roomSummary.info.currentUserMembership == currentUserMembership }
.first() .first()
// Ensure that the room is ready // Ensure that the room is ready
.also { client.awaitRoomRemoteEcho(it.roomId.value) } .also { client.awaitRoomRemoteEcho(it.roomId.value) }
@ -316,7 +316,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 {
awaitJoinedRoom(roomId.toRoomIdOrAlias(), 30.seconds) awaitRoom(roomId.toRoomIdOrAlias(), 30.seconds, CurrentUserMembership.JOINED)
} 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")
} }
@ -371,7 +371,7 @@ class RustMatrixClient(
runCatching { runCatching {
client.joinRoomById(roomId.value).destroy() client.joinRoomById(roomId.value).destroy()
try { try {
awaitJoinedRoom(roomId.toRoomIdOrAlias(), 10.seconds) awaitRoom(roomId.toRoomIdOrAlias(), 10.seconds, CurrentUserMembership.JOINED)
} 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
@ -386,7 +386,7 @@ class RustMatrixClient(
serverNames = serverNames, serverNames = serverNames,
).destroy() ).destroy()
try { try {
awaitJoinedRoom(roomIdOrAlias, 10.seconds) awaitRoom(roomIdOrAlias, 10.seconds, CurrentUserMembership.JOINED)
} 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
@ -394,8 +394,16 @@ class RustMatrixClient(
} }
} }
override suspend fun knockRoom(roomId: RoomId): Result<Unit> { override suspend fun knockRoom(roomId: RoomId): Result<RoomSummary?> = withContext(sessionDispatcher){
return Result.failure(NotImplementedError("Not yet implemented")) runCatching {
client.knock(roomId.toRoomIdOrAlias().identifier).destroy()
try {
awaitRoom(roomId.toRoomIdOrAlias(), 10.seconds, CurrentUserMembership.KNOCKED)
} catch (e: Exception) {
Timber.e(e, "Timeout waiting for the room to be available in the room list")
null
}
}
} }
override suspend fun trackRecentlyVisitedRoom(roomId: RoomId): Result<Unit> = withContext(sessionDispatcher) { override suspend fun trackRecentlyVisitedRoom(roomId: RoomId): Result<Unit> = withContext(sessionDispatcher) {

6
libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/FakeMatrixClient.kt

@ -114,8 +114,8 @@ class FakeMatrixClient(
var joinRoomByIdOrAliasLambda: (RoomIdOrAlias, List<String>) -> Result<RoomSummary?> = { _, _ -> var joinRoomByIdOrAliasLambda: (RoomIdOrAlias, List<String>) -> Result<RoomSummary?> = { _, _ ->
Result.success(null) Result.success(null)
} }
var knockRoomLambda: (RoomId) -> Result<Unit> = { var knockRoomLambda: (RoomId) -> Result<RoomSummary?> = {
Result.success(Unit) Result.success(null)
} }
var getRoomSummaryFlowLambda = { _: RoomIdOrAlias -> var getRoomSummaryFlowLambda = { _: RoomIdOrAlias ->
flowOf<Optional<RoomSummary>>(Optional.empty()) flowOf<Optional<RoomSummary>>(Optional.empty())
@ -223,7 +223,7 @@ class FakeMatrixClient(
return joinRoomByIdOrAliasLambda(roomIdOrAlias, serverNames) return joinRoomByIdOrAliasLambda(roomIdOrAlias, serverNames)
} }
override suspend fun knockRoom(roomId: RoomId): Result<Unit> = knockRoomLambda(roomId) override suspend fun knockRoom(roomId: RoomId): Result<RoomSummary?> = knockRoomLambda(roomId)
override fun sessionVerificationService(): SessionVerificationService = sessionVerificationService override fun sessionVerificationService(): SessionVerificationService = sessionVerificationService

Loading…
Cancel
Save