Merge pull request #3715 from vector-im/feature/bma/initialState

Add initialState support to CreateRoomParams
This commit is contained in:
Benoit Marty 2021-07-22 14:11:49 +02:00 committed by GitHub
commit 19f9f5a6ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 4 deletions

1
changelog.d/3713.removal Normal file
View File

@ -0,0 +1 @@
Add initialState support to CreateRoomParams (#3713)

View File

@ -25,7 +25,6 @@ import org.matrix.android.sdk.api.session.room.model.RoomHistoryVisibility
import org.matrix.android.sdk.api.session.room.model.RoomJoinRulesAllowEntry import org.matrix.android.sdk.api.session.room.model.RoomJoinRulesAllowEntry
import org.matrix.android.sdk.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM import org.matrix.android.sdk.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM
// TODO Give a way to include other initial states
open class CreateRoomParams { open class CreateRoomParams {
/** /**
* A public visibility indicates that the room will be shown in the published room list. * A public visibility indicates that the room will be shown in the published room list.
@ -103,6 +102,13 @@ open class CreateRoomParams {
*/ */
val creationContent = mutableMapOf<String, Any>() val creationContent = mutableMapOf<String, Any>()
/**
* A list of state events to set in the new room. This allows the user to override the default state events
* set in the new room. The expected format of the state events are an object with type, state_key and content keys set.
* Takes precedence over events set by preset, but gets overridden by name and topic keys.
*/
val initialStates = mutableListOf<CreateRoomStateEvent>()
/** /**
* Set to true to disable federation of this room. * Set to true to disable federation of this room.
* Default: false * Default: false

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2021 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.room.model.create
import org.matrix.android.sdk.api.session.events.model.Content
data class CreateRoomStateEvent(
/**
* Required. The type of event to send.
*/
val type: String,
/**
* Required. The content of the event.
*/
val content: Content,
/**
* The state_key of the state event. Defaults to an empty string.
*/
val stateKey: String = ""
)

View File

@ -81,13 +81,14 @@ internal class CreateRoomBodyBuilder @Inject constructor(
params.historyVisibility = params.historyVisibility ?: RoomHistoryVisibility.SHARED params.historyVisibility = params.historyVisibility ?: RoomHistoryVisibility.SHARED
params.guestAccess = params.guestAccess ?: GuestAccess.Forbidden params.guestAccess = params.guestAccess ?: GuestAccess.Forbidden
} }
val initialStates = listOfNotNull( val initialStates = (listOfNotNull(
buildEncryptionWithAlgorithmEvent(params), buildEncryptionWithAlgorithmEvent(params),
buildHistoryVisibilityEvent(params), buildHistoryVisibilityEvent(params),
buildAvatarEvent(params), buildAvatarEvent(params),
buildGuestAccess(params), buildGuestAccess(params),
buildJoinRulesRestricted(params) buildJoinRulesRestricted(params)
) )
+ buildCustomInitialStates(params))
.takeIf { it.isNotEmpty() } .takeIf { it.isNotEmpty() }
return CreateRoomBody( return CreateRoomBody(
@ -95,7 +96,7 @@ internal class CreateRoomBodyBuilder @Inject constructor(
roomAliasName = params.roomAliasName, roomAliasName = params.roomAliasName,
name = params.name, name = params.name,
topic = params.topic, topic = params.topic,
invitedUserIds = params.invitedUserIds.filter { it != userId }, invitedUserIds = params.invitedUserIds.filter { it != userId }.takeIf { it.isNotEmpty() },
invite3pids = invite3pids, invite3pids = invite3pids,
creationContent = params.creationContent.takeIf { it.isNotEmpty() }, creationContent = params.creationContent.takeIf { it.isNotEmpty() },
initialStates = initialStates, initialStates = initialStates,
@ -103,10 +104,19 @@ internal class CreateRoomBodyBuilder @Inject constructor(
isDirect = params.isDirect, isDirect = params.isDirect,
powerLevelContentOverride = params.powerLevelContentOverride, powerLevelContentOverride = params.powerLevelContentOverride,
roomVersion = params.roomVersion roomVersion = params.roomVersion
) )
} }
private fun buildCustomInitialStates(params: CreateRoomParams): List<Event> {
return params.initialStates.map {
Event(
type = it.type,
stateKey = it.stateKey,
content = it.content
)
}
}
private suspend fun buildAvatarEvent(params: CreateRoomParams): Event? { private suspend fun buildAvatarEvent(params: CreateRoomParams): Event? {
return params.avatarUri?.let { avatarUri -> return params.avatarUri?.let { avatarUri ->
// First upload the image, ignoring any error // First upload the image, ignoring any error