From 46290f1ed490bfa1aaa1c391ef44bebb31e34e79 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 29 Mar 2021 15:00:02 +0200 Subject: [PATCH] Benoit Review: cleanup --- .../sdk/session/space/SpaceCreationTest.kt | 8 ++--- .../sdk/session/space/SpaceHierarchyTest.kt | 4 +-- .../android/sdk/api/session/room/Room.kt | 3 ++ .../session/room/RoomSummaryQueryParams.kt | 10 ++++--- .../android/sdk/api/session/space/Space.kt | 7 +++++ .../sdk/internal/session/room/SpaceGetter.kt | 10 ++----- .../room/summary/RoomSummaryDataSource.kt | 5 ++-- .../internal/session/space/DefaultSpace.kt | 29 +++++++++++++------ .../features/spaces/SpacesListViewModel.kt | 2 +- .../spaces/create/CreateSpaceViewModelTask.kt | 2 +- 10 files changed, 48 insertions(+), 32 deletions(-) diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/space/SpaceCreationTest.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/space/SpaceCreationTest.kt index 4900ac9f56..be8bde0721 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/space/SpaceCreationTest.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/space/SpaceCreationTest.kt @@ -60,7 +60,7 @@ class SpaceCreationTest : InstrumentedTest { val spaceId: String runBlocking { spaceId = session.spaceService().createSpace(roomName, topic, null, true) - // wait a bit to let the summry update it self :/ + // wait a bit to let the summary update it self :/ delay(400) } @@ -100,14 +100,14 @@ class SpaceCreationTest : InstrumentedTest { @Test fun testJoinSimplePublicSpace() { val aliceSession = commonTestHelper.createAccount("alice", SessionTestParams(true)) - val bobSession = commonTestHelper.createAccount("alice", SessionTestParams(true)) + val bobSession = commonTestHelper.createAccount("bob", SessionTestParams(true)) val roomName = "My Space" val topic = "A public space for test" val spaceId: String runBlocking { spaceId = aliceSession.spaceService().createSpace(roomName, topic, null, true) - // wait a bit to let the summry update it self :/ + // wait a bit to let the summary update it self :/ delay(400) } @@ -131,7 +131,7 @@ class SpaceCreationTest : InstrumentedTest { @Test fun testSimplePublicSpaceWithChildren() { val aliceSession = commonTestHelper.createAccount("alice", SessionTestParams(true)) - val bobSession = commonTestHelper.createAccount("alice", SessionTestParams(true)) + val bobSession = commonTestHelper.createAccount("bob", SessionTestParams(true)) val roomName = "My Space" val topic = "A public space for test" diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/space/SpaceHierarchyTest.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/space/SpaceHierarchyTest.kt index b3396fed6f..4e0284469b 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/space/SpaceHierarchyTest.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/space/SpaceHierarchyTest.kt @@ -48,13 +48,13 @@ class SpaceHierarchyTest : InstrumentedTest { @Test fun createCanonicalChildRelation() { - val session = commonTestHelper.createAccount("Jhon", SessionTestParams(true)) + val session = commonTestHelper.createAccount("John", SessionTestParams(true)) val spaceName = "My Space" val topic = "A public space for test" val spaceId: String runBlocking { spaceId = session.spaceService().createSpace(spaceName, topic, null, true) - // wait a bit to let the summry update it self :/ + // wait a bit to let the summary update it self :/ delay(400) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt index 1bbc090ef0..6784df565f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt @@ -94,5 +94,8 @@ interface Room : afterLimit: Int, includeProfile: Boolean): SearchResult + /** + * Use this room as a Space, if the type is correct. + */ fun asSpace(): Space? } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomSummaryQueryParams.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomSummaryQueryParams.kt index 47618e31c9..f50d8f76ee 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomSummaryQueryParams.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomSummaryQueryParams.kt @@ -31,10 +31,11 @@ fun spaceSummaryQueryParams(init: (RoomSummaryQueryParams.Builder.() -> Unit) = return RoomSummaryQueryParams.Builder() .apply(init) .apply { - this.includeType = listOf(RoomType.SPACE) - this.excludeType = null - this.roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS - }.build() + includeType = listOf(RoomType.SPACE) + excludeType = null + roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS + } + .build() } enum class RoomCategoryFilter { @@ -42,6 +43,7 @@ enum class RoomCategoryFilter { ONLY_ROOMS, ALL } + /** * This class can be used to filter room summaries to use with: * [org.matrix.android.sdk.api.session.room.Room] and [org.matrix.android.sdk.api.session.room.RoomService] diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/space/Space.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/space/Space.kt index ba76276fde..fda1465ac8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/space/Space.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/space/Space.kt @@ -16,13 +16,20 @@ package org.matrix.android.sdk.api.session.space +import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.session.room.Room import org.matrix.android.sdk.api.session.room.model.RoomSummary +import org.matrix.android.sdk.api.util.Cancelable interface Space { fun asRoom(): Room + val spaceId: String + + fun leave(reason: String? = null, + callback: MatrixCallback): Cancelable + /** * A current snapshot of [RoomSummary] associated with the space */ diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/SpaceGetter.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/SpaceGetter.kt index 0f64bb60ad..fed3ff542b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/SpaceGetter.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/SpaceGetter.kt @@ -16,10 +16,7 @@ package org.matrix.android.sdk.internal.session.room -import org.matrix.android.sdk.api.session.room.model.RoomType import org.matrix.android.sdk.api.session.space.Space -import org.matrix.android.sdk.internal.session.room.summary.RoomSummaryDataSource -import org.matrix.android.sdk.internal.session.space.DefaultSpace import javax.inject.Inject internal interface SpaceGetter { @@ -27,13 +24,10 @@ internal interface SpaceGetter { } internal class DefaultSpaceGetter @Inject constructor( - private val roomGetter: RoomGetter, - private val spaceSummaryDataSource: RoomSummaryDataSource + private val roomGetter: RoomGetter ) : SpaceGetter { override fun get(spaceId: String): Space? { - return roomGetter.getRoom(spaceId) - ?.takeIf { it.roomSummary()?.roomType == RoomType.SPACE } - ?.let { DefaultSpace(it, spaceSummaryDataSource) } + return roomGetter.getRoom(spaceId)?.asSpace() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt index c2d4e318cb..d91f3acfb1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryDataSource.kt @@ -98,9 +98,8 @@ internal class RoomSummaryDataSource @Inject constructor(@SessionDatabase privat } fun getSpaceSummary(roomIdOrAlias: String): RoomSummary? { - return getRoomSummary(roomIdOrAlias).let { - it?.takeIf { it.roomType == RoomType.SPACE } - } + return getRoomSummary(roomIdOrAlias) + ?.takeIf { it.roomType == RoomType.SPACE } } fun getSpaceSummaryLive(roomId: String): LiveData> { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/space/DefaultSpace.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/space/DefaultSpace.kt index 76496769ec..6bcdf63bd8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/space/DefaultSpace.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/space/DefaultSpace.kt @@ -16,6 +16,7 @@ package org.matrix.android.sdk.internal.session.space +import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.toContent @@ -24,23 +25,33 @@ import org.matrix.android.sdk.api.session.room.Room import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.space.Space import org.matrix.android.sdk.api.session.space.model.SpaceChildContent +import org.matrix.android.sdk.api.util.Cancelable import org.matrix.android.sdk.internal.session.room.summary.RoomSummaryDataSource -internal class DefaultSpace(private val room: Room, private val spaceSummaryDataSource: RoomSummaryDataSource) : Space { +internal class DefaultSpace( + private val room: Room, + private val spaceSummaryDataSource: RoomSummaryDataSource +) : Space { override fun asRoom(): Room { return room } + override val spaceId = room.roomId + + override fun leave(reason: String?, callback: MatrixCallback): Cancelable { + return room.leave(reason, callback) + } + override fun spaceSummary(): RoomSummary? { - return spaceSummaryDataSource.getSpaceSummary(asRoom().roomId) + return spaceSummaryDataSource.getSpaceSummary(room.roomId) } override suspend fun addChildren(roomId: String, viaServers: List, order: String?, autoJoin: Boolean, suggested: Boolean?) { - asRoom().sendStateEvent( + room.sendStateEvent( eventType = EventType.STATE_SPACE_CHILD, stateKey = roomId, body = SpaceChildContent( @@ -53,14 +64,14 @@ internal class DefaultSpace(private val room: Room, private val spaceSummaryData } override suspend fun removeRoom(roomId: String) { - val existing = asRoom().getStateEvents(setOf(EventType.STATE_SPACE_CHILD), QueryStringValue.Equals(roomId)) + val existing = room.getStateEvents(setOf(EventType.STATE_SPACE_CHILD), QueryStringValue.Equals(roomId)) .firstOrNull() ?.content.toModel() ?: // should we throw here? return // edit state event and set via to null - asRoom().sendStateEvent( + room.sendStateEvent( eventType = EventType.STATE_SPACE_CHILD, stateKey = roomId, body = SpaceChildContent( @@ -72,13 +83,13 @@ internal class DefaultSpace(private val room: Room, private val spaceSummaryData } override suspend fun setChildrenOrder(roomId: String, order: String?) { - val existing = asRoom().getStateEvents(setOf(EventType.STATE_SPACE_CHILD), QueryStringValue.Equals(roomId)) + val existing = room.getStateEvents(setOf(EventType.STATE_SPACE_CHILD), QueryStringValue.Equals(roomId)) .firstOrNull() ?.content.toModel() ?: throw IllegalArgumentException("$roomId is not a child of this space") // edit state event and set via to null - asRoom().sendStateEvent( + room.sendStateEvent( eventType = EventType.STATE_SPACE_CHILD, stateKey = roomId, body = SpaceChildContent( @@ -90,13 +101,13 @@ internal class DefaultSpace(private val room: Room, private val spaceSummaryData } override suspend fun setChildrenAutoJoin(roomId: String, autoJoin: Boolean) { - val existing = asRoom().getStateEvents(setOf(EventType.STATE_SPACE_CHILD), QueryStringValue.Equals(roomId)) + val existing = room.getStateEvents(setOf(EventType.STATE_SPACE_CHILD), QueryStringValue.Equals(roomId)) .firstOrNull() ?.content.toModel() ?: throw IllegalArgumentException("$roomId is not a child of this space") // edit state event and set via to null - asRoom().sendStateEvent( + room.sendStateEvent( eventType = EventType.STATE_SPACE_CHILD, stateKey = roomId, body = SpaceChildContent( diff --git a/vector/src/main/java/im/vector/app/features/spaces/SpacesListViewModel.kt b/vector/src/main/java/im/vector/app/features/spaces/SpacesListViewModel.kt index 1f15d76a80..8c13584b43 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/SpacesListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/SpacesListViewModel.kt @@ -176,7 +176,7 @@ class SpacesListViewModel @AssistedInject constructor(@Assisted initialState: Sp viewModelScope.launch { awaitCallback { tryOrNull("Failed to leave space ${action.spaceSummary.roomId}") { - session.spaceService().getSpace(action.spaceSummary.roomId)?.asRoom()?.leave(null, it) + session.spaceService().getSpace(action.spaceSummary.roomId)?.leave(null, it) } } } diff --git a/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModelTask.kt b/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModelTask.kt index 154c75ad82..df4b8a8cfe 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModelTask.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModelTask.kt @@ -80,7 +80,7 @@ class CreateSpaceViewModelTask @Inject constructor( // set canonical session.spaceService().setSpaceParent( roomId, - createdSpace.asRoom().roomId, + createdSpace.spaceId, true, via )