Merge pull request #5432 from element-hq/feature/bma/leaveSpace
Leave space: use SDK API.
This commit is contained in:
@@ -69,14 +69,6 @@ object MatrixPatterns {
|
||||
str matches PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if a string is a valid space id. This is an alias for [isRoomId]
|
||||
*
|
||||
* @param str the string to test
|
||||
* @return true if the string is a valid space Id
|
||||
*/
|
||||
fun isSpaceId(str: String?) = isRoomId(str)
|
||||
|
||||
/**
|
||||
* Tells if a string is a valid room id.
|
||||
*
|
||||
|
||||
@@ -7,23 +7,7 @@
|
||||
|
||||
package io.element.android.libraries.matrix.api.core
|
||||
|
||||
import io.element.android.libraries.androidutils.metadata.isInDebug
|
||||
import java.io.Serializable
|
||||
|
||||
@JvmInline
|
||||
value class SpaceId(val value: String) : Serializable {
|
||||
init {
|
||||
if (isInDebug && !MatrixPatterns.isSpaceId(value)) {
|
||||
error(
|
||||
"`$value` is not a valid space id.\n" +
|
||||
"Space ids are the same as room ids.\n" +
|
||||
"Example space id: `!space_id:domain`."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String = value
|
||||
}
|
||||
typealias SpaceId = RoomId
|
||||
|
||||
/**
|
||||
* Value to use when no space is selected by the user.
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.element.android.libraries.matrix.api.exception.ClientException
|
||||
|
||||
sealed class RecoveryException(message: String) : Exception(message) {
|
||||
class SecretStorage(message: String) : RecoveryException(message)
|
||||
class Import(message: String) : RecoveryException(message)
|
||||
data object BackupExistsOnServer : RecoveryException("BackupExistsOnServer")
|
||||
data class Client(val exception: ClientException) : RecoveryException(exception.message ?: "Unknown error")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2025 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.api.spaces
|
||||
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
|
||||
interface LeaveSpaceHandle {
|
||||
/**
|
||||
* The id of the space to leave.
|
||||
*/
|
||||
val id: RoomId
|
||||
|
||||
/**
|
||||
* Get a list of rooms that can be left when leaving the space.
|
||||
* It will include the current space and all the subspaces and rooms that the user has joined.
|
||||
*/
|
||||
suspend fun rooms(): Result<List<LeaveSpaceRoom>>
|
||||
|
||||
/**
|
||||
* Leave the space and the given rooms.
|
||||
* If [roomIds] is empty, only the space will be left.
|
||||
*/
|
||||
suspend fun leave(roomIds: List<RoomId>): Result<Unit>
|
||||
|
||||
/**
|
||||
* Close the handle and free resources.
|
||||
*/
|
||||
fun close()
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Copyright 2025 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.api.spaces
|
||||
|
||||
data class LeaveSpaceRoom(
|
||||
val spaceRoom: SpaceRoom,
|
||||
val isLastAdmin: Boolean,
|
||||
)
|
||||
@@ -15,4 +15,6 @@ interface SpaceService {
|
||||
suspend fun joinedSpaces(): Result<List<SpaceRoom>>
|
||||
|
||||
fun spaceRoomList(id: RoomId): SpaceRoomList
|
||||
|
||||
fun getLeaveSpaceHandle(spaceId: RoomId): LeaveSpaceHandle
|
||||
}
|
||||
|
||||
@@ -147,6 +147,8 @@ class RustMatrixClient(
|
||||
private val innerRoomListService = innerSyncService.roomListService()
|
||||
private val innerSpaceService = innerClient.spaceService()
|
||||
|
||||
private val roomMembershipObserver = RoomMembershipObserver()
|
||||
|
||||
private val rustSyncService = RustSyncService(
|
||||
inner = innerSyncService,
|
||||
dispatcher = sessionDispatcher,
|
||||
@@ -189,6 +191,7 @@ class RustMatrixClient(
|
||||
|
||||
override val spaceService: SpaceService = RustSpaceService(
|
||||
innerSpaceService = innerSpaceService,
|
||||
roomMembershipObserver = roomMembershipObserver,
|
||||
sessionCoroutineScope = sessionCoroutineScope,
|
||||
sessionDispatcher = sessionDispatcher,
|
||||
)
|
||||
@@ -200,7 +203,7 @@ class RustMatrixClient(
|
||||
)
|
||||
|
||||
private val roomInfoMapper = RoomInfoMapper()
|
||||
private val roomMembershipObserver = RoomMembershipObserver()
|
||||
|
||||
private val roomFactory = RustRoomFactory(
|
||||
roomListService = roomListService,
|
||||
innerRoomListService = innerRoomListService,
|
||||
|
||||
@@ -20,6 +20,9 @@ fun Throwable.mapRecoveryException(): RecoveryException {
|
||||
message = errorMessage
|
||||
)
|
||||
is RustRecoveryException.BackupExistsOnServer -> RecoveryException.BackupExistsOnServer
|
||||
is RustRecoveryException.Import -> RecoveryException.Import(
|
||||
message = errorMessage
|
||||
)
|
||||
is RustRecoveryException.Client -> RecoveryException.Client(
|
||||
source.mapClientException()
|
||||
)
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2025 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.spaces
|
||||
|
||||
import io.element.android.libraries.core.extensions.runCatchingExceptions
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.room.CurrentUserMembership
|
||||
import io.element.android.libraries.matrix.api.room.RoomMembershipObserver
|
||||
import io.element.android.libraries.matrix.api.spaces.LeaveSpaceHandle
|
||||
import io.element.android.libraries.matrix.api.spaces.LeaveSpaceRoom
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import org.matrix.rustcomponents.sdk.LeaveSpaceHandle as RustLeaveSpaceHandle
|
||||
|
||||
class RustLeaveSpaceHandle(
|
||||
override val id: RoomId,
|
||||
private val spaceRoomMapper: SpaceRoomMapper,
|
||||
private val roomMembershipObserver: RoomMembershipObserver,
|
||||
sessionCoroutineScope: CoroutineScope,
|
||||
private val innerProvider: suspend () -> RustLeaveSpaceHandle,
|
||||
) : LeaveSpaceHandle {
|
||||
private val inner = CompletableDeferred<RustLeaveSpaceHandle>()
|
||||
|
||||
init {
|
||||
sessionCoroutineScope.launch {
|
||||
inner.complete(innerProvider())
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun rooms(): Result<List<LeaveSpaceRoom>> = runCatchingExceptions {
|
||||
inner.await().rooms().map { leaveSpaceRoom ->
|
||||
LeaveSpaceRoom(
|
||||
spaceRoom = spaceRoomMapper.map(leaveSpaceRoom.spaceRoom),
|
||||
isLastAdmin = leaveSpaceRoom.isLastAdmin,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun leave(roomIds: List<RoomId>): Result<Unit> = runCatchingExceptions {
|
||||
// Ensure the space is included and is the last room to be left
|
||||
val roomToLeave = roomIds - id + id
|
||||
inner.await().leave(roomToLeave.map { it.value })
|
||||
}.onSuccess {
|
||||
roomMembershipObserver.notifyUserLeftRoom(
|
||||
roomId = id,
|
||||
isSpace = true,
|
||||
membershipBeforeLeft = CurrentUserMembership.JOINED,
|
||||
)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override fun close() {
|
||||
Timber.d("Destroying LeaveSpaceHandle $id")
|
||||
try {
|
||||
inner.getCompleted().destroy()
|
||||
} catch (_: Exception) {
|
||||
// Ignore, we just want to make sure it's completed
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ package io.element.android.libraries.matrix.impl.spaces
|
||||
import io.element.android.libraries.core.coroutine.childScope
|
||||
import io.element.android.libraries.core.extensions.runCatchingExceptions
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.room.RoomMembershipObserver
|
||||
import io.element.android.libraries.matrix.api.spaces.LeaveSpaceHandle
|
||||
import io.element.android.libraries.matrix.api.spaces.SpaceRoom
|
||||
import io.element.android.libraries.matrix.api.spaces.SpaceRoomList
|
||||
import io.element.android.libraries.matrix.api.spaces.SpaceService
|
||||
@@ -37,6 +39,7 @@ class RustSpaceService(
|
||||
private val innerSpaceService: ClientSpaceService,
|
||||
private val sessionCoroutineScope: CoroutineScope,
|
||||
private val sessionDispatcher: CoroutineDispatcher,
|
||||
private val roomMembershipObserver: RoomMembershipObserver,
|
||||
) : SpaceService {
|
||||
private val spaceRoomMapper = SpaceRoomMapper()
|
||||
override val spaceRoomsFlow = MutableSharedFlow<List<SpaceRoom>>(replay = 1, extraBufferCapacity = 1)
|
||||
@@ -64,6 +67,17 @@ class RustSpaceService(
|
||||
)
|
||||
}
|
||||
|
||||
override fun getLeaveSpaceHandle(spaceId: RoomId): LeaveSpaceHandle {
|
||||
return RustLeaveSpaceHandle(
|
||||
id = spaceId,
|
||||
spaceRoomMapper = spaceRoomMapper,
|
||||
roomMembershipObserver = roomMembershipObserver,
|
||||
sessionCoroutineScope = sessionCoroutineScope,
|
||||
) {
|
||||
innerSpaceService.leaveSpace(spaceId.value)
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
innerSpaceService
|
||||
.spaceListUpdate()
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2025 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.test.spaces
|
||||
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.spaces.LeaveSpaceHandle
|
||||
import io.element.android.libraries.matrix.api.spaces.LeaveSpaceRoom
|
||||
import io.element.android.libraries.matrix.test.A_SPACE_ID
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
import io.element.android.tests.testutils.simulateLongTask
|
||||
|
||||
class FakeLeaveSpaceHandle(
|
||||
override val id: RoomId = A_SPACE_ID,
|
||||
private val roomsResult: () -> Result<List<LeaveSpaceRoom>> = { lambdaError() },
|
||||
private val leaveResult: (List<RoomId>) -> Result<Unit> = { lambdaError() },
|
||||
private val closeResult: () -> Unit = { lambdaError() },
|
||||
) : LeaveSpaceHandle {
|
||||
override suspend fun rooms(): Result<List<LeaveSpaceRoom>> = simulateLongTask {
|
||||
roomsResult()
|
||||
}
|
||||
|
||||
override suspend fun leave(roomIds: List<RoomId>): Result<Unit> = simulateLongTask {
|
||||
leaveResult(roomIds)
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
return closeResult()
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
package io.element.android.libraries.matrix.test.spaces
|
||||
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.spaces.LeaveSpaceHandle
|
||||
import io.element.android.libraries.matrix.api.spaces.SpaceRoom
|
||||
import io.element.android.libraries.matrix.api.spaces.SpaceRoomList
|
||||
import io.element.android.libraries.matrix.api.spaces.SpaceService
|
||||
@@ -20,6 +21,7 @@ import kotlinx.coroutines.flow.asSharedFlow
|
||||
class FakeSpaceService(
|
||||
private val joinedSpacesResult: () -> Result<List<SpaceRoom>> = { lambdaError() },
|
||||
private val spaceRoomListResult: (RoomId) -> SpaceRoomList = { lambdaError() },
|
||||
private val leaveSpaceHandleResult: (RoomId) -> LeaveSpaceHandle = { lambdaError() },
|
||||
) : SpaceService {
|
||||
private val _spaceRoomsFlow = MutableSharedFlow<List<SpaceRoom>>()
|
||||
override val spaceRoomsFlow: SharedFlow<List<SpaceRoom>>
|
||||
@@ -36,4 +38,8 @@ class FakeSpaceService(
|
||||
override fun spaceRoomList(id: RoomId): SpaceRoomList {
|
||||
return spaceRoomListResult(id)
|
||||
}
|
||||
|
||||
override fun getLeaveSpaceHandle(spaceId: RoomId): LeaveSpaceHandle {
|
||||
return leaveSpaceHandleResult(spaceId)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user