mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-02-17 04:20:00 +03:00
Improve CreateRoomParams API: update some API for better chaining of builder like methods (#1070)
This commit is contained in:
parent
e73f138151
commit
fc95bf4926
2 changed files with 30 additions and 18 deletions
|
@ -128,33 +128,46 @@ data class CreateRoomParams(
|
||||||
/**
|
/**
|
||||||
* After calling this method, when the room will be created, if cross-signing is enabled and we can get keys for every invited users,
|
* After calling this method, when the room will be created, if cross-signing is enabled and we can get keys for every invited users,
|
||||||
* the encryption will be enabled on the created room
|
* the encryption will be enabled on the created room
|
||||||
|
* @param value true to activate this behavior.
|
||||||
* @return this, to allow chaining methods
|
* @return this, to allow chaining methods
|
||||||
*/
|
*/
|
||||||
fun enableEncryptionIfInvitedUsersSupportIt(): CreateRoomParams {
|
fun enableEncryptionIfInvitedUsersSupportIt(value: Boolean = true): CreateRoomParams {
|
||||||
enableEncryptionIfInvitedUsersSupportIt = true
|
enableEncryptionIfInvitedUsersSupportIt = value
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the crypto algorithm to the room creation parameters.
|
* Add the crypto algorithm to the room creation parameters.
|
||||||
*
|
*
|
||||||
* @param algorithm the algorithm
|
* @param enable true to enable encryption.
|
||||||
|
* @param algorithm the algorithm, default to [MXCRYPTO_ALGORITHM_MEGOLM], which is actually the only supported algorithm for the moment
|
||||||
* @return a modified copy of the CreateRoomParams object, or this if there is no modification
|
* @return a modified copy of the CreateRoomParams object, or this if there is no modification
|
||||||
*/
|
*/
|
||||||
@CheckResult
|
@CheckResult
|
||||||
fun enableEncryptionWithAlgorithm(algorithm: String = MXCRYPTO_ALGORITHM_MEGOLM): CreateRoomParams {
|
fun enableEncryptionWithAlgorithm(enable: Boolean = true,
|
||||||
|
algorithm: String = MXCRYPTO_ALGORITHM_MEGOLM): CreateRoomParams {
|
||||||
|
// Remove the existing value if any.
|
||||||
|
val newInitialStates = initialStates
|
||||||
|
?.filter { it.type != EventType.STATE_ROOM_ENCRYPTION }
|
||||||
|
|
||||||
return if (algorithm == MXCRYPTO_ALGORITHM_MEGOLM) {
|
return if (algorithm == MXCRYPTO_ALGORITHM_MEGOLM) {
|
||||||
val contentMap = mapOf("algorithm" to algorithm)
|
if (enable) {
|
||||||
|
val contentMap = mapOf("algorithm" to algorithm)
|
||||||
|
|
||||||
val algoEvent = Event(
|
val algoEvent = Event(
|
||||||
type = EventType.STATE_ROOM_ENCRYPTION,
|
type = EventType.STATE_ROOM_ENCRYPTION,
|
||||||
stateKey = "",
|
stateKey = "",
|
||||||
content = contentMap.toContent()
|
content = contentMap.toContent()
|
||||||
)
|
)
|
||||||
|
|
||||||
copy(
|
copy(
|
||||||
initialStates = initialStates.orEmpty().filter { it.type != EventType.STATE_ROOM_ENCRYPTION } + algoEvent
|
initialStates = newInitialStates.orEmpty() + algoEvent
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
return copy(
|
||||||
|
initialStates = newInitialStates
|
||||||
|
)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Timber.e("Unsupported algorithm: $algorithm")
|
Timber.e("Unsupported algorithm: $algorithm")
|
||||||
this
|
this
|
||||||
|
|
|
@ -30,7 +30,6 @@ import im.vector.matrix.android.api.session.Session
|
||||||
import im.vector.matrix.android.api.session.room.model.RoomDirectoryVisibility
|
import im.vector.matrix.android.api.session.room.model.RoomDirectoryVisibility
|
||||||
import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams
|
import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams
|
||||||
import im.vector.matrix.android.api.session.room.model.create.CreateRoomPreset
|
import im.vector.matrix.android.api.session.room.model.create.CreateRoomPreset
|
||||||
import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM
|
|
||||||
import im.vector.riotx.core.platform.EmptyViewEvents
|
import im.vector.riotx.core.platform.EmptyViewEvents
|
||||||
import im.vector.riotx.core.platform.VectorViewModel
|
import im.vector.riotx.core.platform.VectorViewModel
|
||||||
import im.vector.riotx.features.roomdirectory.RoomDirectoryActivity
|
import im.vector.riotx.features.roomdirectory.RoomDirectoryActivity
|
||||||
|
@ -91,10 +90,10 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr
|
||||||
visibility = if (state.isInRoomDirectory) RoomDirectoryVisibility.PUBLIC else RoomDirectoryVisibility.PRIVATE,
|
visibility = if (state.isInRoomDirectory) RoomDirectoryVisibility.PUBLIC else RoomDirectoryVisibility.PRIVATE,
|
||||||
// Public room
|
// Public room
|
||||||
preset = if (state.isPublic) CreateRoomPreset.PRESET_PUBLIC_CHAT else CreateRoomPreset.PRESET_PRIVATE_CHAT
|
preset = if (state.isPublic) CreateRoomPreset.PRESET_PUBLIC_CHAT else CreateRoomPreset.PRESET_PRIVATE_CHAT
|
||||||
).let {
|
)
|
||||||
// Encryption
|
// Encryption
|
||||||
if (state.isEncrypted) it.enableEncryptionWithAlgorithm(MXCRYPTO_ALGORITHM_MEGOLM) else it
|
.enableEncryptionWithAlgorithm(state.isEncrypted)
|
||||||
}
|
|
||||||
|
|
||||||
session.createRoom(createRoomParams, object : MatrixCallback<String> {
|
session.createRoom(createRoomParams, object : MatrixCallback<String> {
|
||||||
override fun onSuccess(data: String) {
|
override fun onSuccess(data: String) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue