Merge pull request #7035 from vector-im/feature/bma/space_response_model
Space response model
This commit is contained in:
commit
ec0770434d
1
changelog.d/7035.misc
Normal file
1
changelog.d/7035.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Ensure that we do not expect all the Event fields when requesting `rooms/{roomId}/hierarchy` endpoint.
|
@ -34,5 +34,4 @@ data class SpaceChildInfo(
|
|||||||
val canonicalAlias: String?,
|
val canonicalAlias: String?,
|
||||||
val aliases: List<String>?,
|
val aliases: List<String>?,
|
||||||
val worldReadable: Boolean
|
val worldReadable: Boolean
|
||||||
|
|
||||||
)
|
)
|
||||||
|
@ -16,13 +16,13 @@
|
|||||||
|
|
||||||
package org.matrix.android.sdk.api.session.space
|
package org.matrix.android.sdk.api.session.space
|
||||||
|
|
||||||
import org.matrix.android.sdk.api.session.events.model.Event
|
|
||||||
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
||||||
import org.matrix.android.sdk.api.session.room.model.SpaceChildInfo
|
import org.matrix.android.sdk.api.session.room.model.SpaceChildInfo
|
||||||
|
import org.matrix.android.sdk.api.session.space.model.SpaceChildSummaryEvent
|
||||||
|
|
||||||
data class SpaceHierarchyData(
|
data class SpaceHierarchyData(
|
||||||
val rootSummary: RoomSummary,
|
val rootSummary: RoomSummary,
|
||||||
val children: List<SpaceChildInfo>,
|
val children: List<SpaceChildInfo>,
|
||||||
val childrenState: List<Event>,
|
val childrenState: List<SpaceChildSummaryEvent>,
|
||||||
val nextToken: String? = null
|
val nextToken: String? = null
|
||||||
)
|
)
|
||||||
|
@ -18,10 +18,10 @@ package org.matrix.android.sdk.api.session.space
|
|||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import org.matrix.android.sdk.api.session.events.model.Event
|
|
||||||
import org.matrix.android.sdk.api.session.room.RoomSortOrder
|
import org.matrix.android.sdk.api.session.room.RoomSortOrder
|
||||||
import org.matrix.android.sdk.api.session.room.RoomSummaryQueryParams
|
import org.matrix.android.sdk.api.session.room.RoomSummaryQueryParams
|
||||||
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
||||||
|
import org.matrix.android.sdk.api.session.space.model.SpaceChildSummaryEvent
|
||||||
import org.matrix.android.sdk.api.session.space.peeking.SpacePeekResult
|
import org.matrix.android.sdk.api.session.space.peeking.SpacePeekResult
|
||||||
|
|
||||||
typealias SpaceSummaryQueryParams = RoomSummaryQueryParams
|
typealias SpaceSummaryQueryParams = RoomSummaryQueryParams
|
||||||
@ -75,12 +75,12 @@ interface SpaceService {
|
|||||||
suggestedOnly: Boolean? = null,
|
suggestedOnly: Boolean? = null,
|
||||||
limit: Int? = null,
|
limit: Int? = null,
|
||||||
from: String? = null,
|
from: String? = null,
|
||||||
knownStateList: List<Event>? = null
|
knownStateList: List<SpaceChildSummaryEvent>? = null
|
||||||
): SpaceHierarchyData
|
): SpaceHierarchyData
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a live list of space summaries. This list is refreshed as soon as the data changes.
|
* Get a live list of space summaries. This list is refreshed as soon as the data changes.
|
||||||
* @return the [LiveData] of List[SpaceSummary]
|
* @return the [LiveData] of List[RoomSummary]
|
||||||
*/
|
*/
|
||||||
fun getSpaceSummariesLive(
|
fun getSpaceSummariesLive(
|
||||||
queryParams: SpaceSummaryQueryParams,
|
queryParams: SpaceSummaryQueryParams,
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.matrix.android.sdk.api.session.space.model
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Content
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class SpaceChildSummaryEvent(
|
||||||
|
@Json(name = "type") val type: String? = null,
|
||||||
|
@Json(name = "state_key") val stateKey: String? = null,
|
||||||
|
@Json(name = "content") val content: Content? = null,
|
||||||
|
@Json(name = "sender") val senderId: String? = null,
|
||||||
|
@Json(name = "origin_server_ts") val originServerTs: Long? = null,
|
||||||
|
)
|
@ -21,7 +21,6 @@ import androidx.lifecycle.LiveData
|
|||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
|
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
|
||||||
import org.matrix.android.sdk.api.query.QueryStringValue
|
import org.matrix.android.sdk.api.query.QueryStringValue
|
||||||
import org.matrix.android.sdk.api.session.events.model.Event
|
|
||||||
import org.matrix.android.sdk.api.session.events.model.EventType
|
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||||
import org.matrix.android.sdk.api.session.events.model.toContent
|
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||||
import org.matrix.android.sdk.api.session.events.model.toModel
|
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||||
@ -45,6 +44,7 @@ import org.matrix.android.sdk.api.session.space.SpaceHierarchyData
|
|||||||
import org.matrix.android.sdk.api.session.space.SpaceService
|
import org.matrix.android.sdk.api.session.space.SpaceService
|
||||||
import org.matrix.android.sdk.api.session.space.SpaceSummaryQueryParams
|
import org.matrix.android.sdk.api.session.space.SpaceSummaryQueryParams
|
||||||
import org.matrix.android.sdk.api.session.space.model.SpaceChildContent
|
import org.matrix.android.sdk.api.session.space.model.SpaceChildContent
|
||||||
|
import org.matrix.android.sdk.api.session.space.model.SpaceChildSummaryEvent
|
||||||
import org.matrix.android.sdk.api.session.space.model.SpaceParentContent
|
import org.matrix.android.sdk.api.session.space.model.SpaceParentContent
|
||||||
import org.matrix.android.sdk.api.session.space.peeking.SpacePeekResult
|
import org.matrix.android.sdk.api.session.space.peeking.SpacePeekResult
|
||||||
import org.matrix.android.sdk.internal.di.UserId
|
import org.matrix.android.sdk.internal.di.UserId
|
||||||
@ -128,7 +128,7 @@ internal class DefaultSpaceService @Inject constructor(
|
|||||||
suggestedOnly: Boolean?,
|
suggestedOnly: Boolean?,
|
||||||
limit: Int?,
|
limit: Int?,
|
||||||
from: String?,
|
from: String?,
|
||||||
knownStateList: List<Event>?
|
knownStateList: List<SpaceChildSummaryEvent>?
|
||||||
): SpaceHierarchyData {
|
): SpaceHierarchyData {
|
||||||
val spacesResponse = getSpacesResponse(spaceId, suggestedOnly, limit, from)
|
val spacesResponse = getSpacesResponse(spaceId, suggestedOnly, limit, from)
|
||||||
val spaceRootResponse = spacesResponse.getRoot(spaceId)
|
val spaceRootResponse = spacesResponse.getRoot(spaceId)
|
||||||
@ -180,7 +180,7 @@ internal class DefaultSpaceService @Inject constructor(
|
|||||||
private fun List<SpaceChildSummaryResponse>?.mapSpaceChildren(
|
private fun List<SpaceChildSummaryResponse>?.mapSpaceChildren(
|
||||||
spaceId: String,
|
spaceId: String,
|
||||||
spaceRootResponse: SpaceChildSummaryResponse?,
|
spaceRootResponse: SpaceChildSummaryResponse?,
|
||||||
knownStateList: List<Event>?,
|
knownStateList: List<SpaceChildSummaryEvent>?,
|
||||||
) = this?.filterIdIsNot(spaceId)
|
) = this?.filterIdIsNot(spaceId)
|
||||||
?.toSpaceChildInfoList(spaceId, spaceRootResponse, knownStateList)
|
?.toSpaceChildInfoList(spaceId, spaceRootResponse, knownStateList)
|
||||||
.orEmpty()
|
.orEmpty()
|
||||||
@ -190,7 +190,7 @@ internal class DefaultSpaceService @Inject constructor(
|
|||||||
private fun List<SpaceChildSummaryResponse>.toSpaceChildInfoList(
|
private fun List<SpaceChildSummaryResponse>.toSpaceChildInfoList(
|
||||||
spaceId: String,
|
spaceId: String,
|
||||||
rootRoomResponse: SpaceChildSummaryResponse?,
|
rootRoomResponse: SpaceChildSummaryResponse?,
|
||||||
knownStateList: List<Event>?,
|
knownStateList: List<SpaceChildSummaryEvent>?,
|
||||||
) = flatMap { spaceChildSummary ->
|
) = flatMap { spaceChildSummary ->
|
||||||
(rootRoomResponse?.childrenState ?: knownStateList)
|
(rootRoomResponse?.childrenState ?: knownStateList)
|
||||||
?.filter { it.isChildOf(spaceChildSummary) }
|
?.filter { it.isChildOf(spaceChildSummary) }
|
||||||
@ -198,11 +198,15 @@ internal class DefaultSpaceService @Inject constructor(
|
|||||||
.orEmpty()
|
.orEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Event.isChildOf(space: SpaceChildSummaryResponse) = stateKey == space.roomId && type == EventType.STATE_SPACE_CHILD
|
private fun SpaceChildSummaryEvent.isChildOf(space: SpaceChildSummaryResponse): Boolean {
|
||||||
|
return stateKey == space.roomId && type == EventType.STATE_SPACE_CHILD
|
||||||
|
}
|
||||||
|
|
||||||
private fun Event.toSpaceChildInfo(spaceId: String, summary: SpaceChildSummaryResponse) = content.toModel<SpaceChildContent>()?.let { content ->
|
private fun SpaceChildSummaryEvent.toSpaceChildInfo(spaceId: String, summary: SpaceChildSummaryResponse): SpaceChildInfo? {
|
||||||
|
return content.toModel<SpaceChildContent>()?.let { content ->
|
||||||
createSpaceChildInfo(spaceId, summary, content)
|
createSpaceChildInfo(spaceId, summary, content)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun createSpaceChildInfo(
|
private fun createSpaceChildInfo(
|
||||||
spaceId: String,
|
spaceId: String,
|
||||||
@ -255,7 +259,7 @@ internal class DefaultSpaceService @Inject constructor(
|
|||||||
stateKey = QueryStringValue.IsEmpty
|
stateKey = QueryStringValue.IsEmpty
|
||||||
)
|
)
|
||||||
val powerLevelsContent = powerLevelsEvent?.content?.toModel<PowerLevelsContent>()
|
val powerLevelsContent = powerLevelsEvent?.content?.toModel<PowerLevelsContent>()
|
||||||
?: throw UnsupportedOperationException("Cannot add canonical child, missing powerlevel")
|
?: throw UnsupportedOperationException("Cannot add canonical child, missing power level")
|
||||||
val powerLevelsHelper = PowerLevelsHelper(powerLevelsContent)
|
val powerLevelsHelper = PowerLevelsHelper(powerLevelsContent)
|
||||||
if (!powerLevelsHelper.isUserAllowedToSend(userId, true, EventType.STATE_SPACE_CHILD)) {
|
if (!powerLevelsHelper.isUserAllowedToSend(userId, true, EventType.STATE_SPACE_CHILD)) {
|
||||||
throw UnsupportedOperationException("Cannot add canonical child, not enough power level")
|
throw UnsupportedOperationException("Cannot add canonical child, not enough power level")
|
||||||
|
@ -18,7 +18,7 @@ package org.matrix.android.sdk.internal.session.space
|
|||||||
|
|
||||||
import com.squareup.moshi.Json
|
import com.squareup.moshi.Json
|
||||||
import com.squareup.moshi.JsonClass
|
import com.squareup.moshi.JsonClass
|
||||||
import org.matrix.android.sdk.api.session.events.model.Event
|
import org.matrix.android.sdk.api.session.space.model.SpaceChildSummaryEvent
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The fields are the same as those returned by /publicRooms (see spec), with the addition of:
|
* The fields are the same as those returned by /publicRooms (see spec), with the addition of:
|
||||||
@ -36,10 +36,11 @@ internal data class SpaceChildSummaryResponse(
|
|||||||
*/
|
*/
|
||||||
@Json(name = "room_type") val roomType: String? = null,
|
@Json(name = "room_type") val roomType: String? = null,
|
||||||
|
|
||||||
/** The m.space.child events of the room. For each event, only the following fields are included:
|
/**
|
||||||
* type, state_key, content, room_id, sender, with the addition of origin_server_ts.
|
* The m.space.child events of the room. For each event, only the following fields are included:
|
||||||
|
* type, state_key, content, sender, and of origin_server_ts.
|
||||||
*/
|
*/
|
||||||
@Json(name = "children_state") val childrenState: List<Event>? = null,
|
@Json(name = "children_state") val childrenState: List<SpaceChildSummaryEvent>? = null,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aliases of the room. May be empty.
|
* Aliases of the room. May be empty.
|
||||||
|
@ -189,7 +189,7 @@ class SpaceManageRoomsViewModel @AssistedInject constructor(
|
|||||||
val apiResult = session.spaceService().querySpaceChildren(
|
val apiResult = session.spaceService().querySpaceChildren(
|
||||||
spaceId = initialState.spaceId,
|
spaceId = initialState.spaceId,
|
||||||
from = nextToken,
|
from = nextToken,
|
||||||
knownStateList = knownResults.childrenState.orEmpty(),
|
knownStateList = knownResults.childrenState,
|
||||||
limit = paginationLimit
|
limit = paginationLimit
|
||||||
)
|
)
|
||||||
val newKnown = apiResult.children.mapNotNull { session.getRoomSummary(it.childRoomId) }
|
val newKnown = apiResult.children.mapNotNull { session.getRoomSummary(it.childRoomId) }
|
||||||
|
Loading…
Reference in New Issue
Block a user