Benoit Review: cleanup

This commit is contained in:
Benoit Marty 2021-03-29 15:00:02 +02:00 committed by Valere
parent b380fa0653
commit 46290f1ed4
10 changed files with 48 additions and 32 deletions

View File

@ -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"

View File

@ -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)
}

View File

@ -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?
}

View File

@ -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]

View File

@ -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<Unit>): Cancelable
/**
* A current snapshot of [RoomSummary] associated with the space
*/

View File

@ -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()
}
}

View File

@ -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<Optional<RoomSummary>> {

View File

@ -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<Unit>): 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<String>,
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<SpaceChildContent>()
?: // 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<SpaceChildContent>()
?: 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<SpaceChildContent>()
?: 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(

View File

@ -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)
}
}
}

View File

@ -80,7 +80,7 @@ class CreateSpaceViewModelTask @Inject constructor(
// set canonical
session.spaceService().setSpaceParent(
roomId,
createdSpace.asRoom().roomId,
createdSpace.spaceId,
true,
via
)