Make CreateRoomParams a regular data class

This commit is contained in:
Benoit Marty 2020-01-29 14:17:05 +01:00
parent 70b04dbaea
commit e1ddde5501
5 changed files with 138 additions and 130 deletions

View file

@ -37,8 +37,12 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.Assert.*
import java.util.*
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import java.util.Arrays
import java.util.HashMap
import java.util.concurrent.CountDownLatch
class CryptoTestHelper(val mTestHelper: CommonTestHelper) {
@ -57,7 +61,7 @@ class CryptoTestHelper(val mTestHelper: CommonTestHelper) {
var roomId: String? = null
val lock1 = CountDownLatch(1)
aliceSession.createRoom(CreateRoomParams().apply { name = "MyRoom" }, object : TestMatrixCallback<String>(lock1) {
aliceSession.createRoom(CreateRoomParams(name = "MyRoom"), object : TestMatrixCallback<String>(lock1) {
override fun onSuccess(data: String) {
roomId = data
super.onSuccess(data)

View file

@ -35,15 +35,15 @@ import timber.log.Timber
* Parameter to create a room, with facilities functions to configure it
*/
@JsonClass(generateAdapter = true)
class CreateRoomParams {
data class CreateRoomParams(
/**
* A public visibility indicates that the room will be shown in the published room list.
* A private visibility will hide the room from the published room list.
* Rooms default to private visibility if this key is not included.
* NB: This should not be confused with join_rules which also uses the word public. One of: ["public", "private"]
*/
var visibility: RoomDirectoryVisibility? = null
@Json(name = "visibility")
val visibility: RoomDirectoryVisibility? = null,
/**
* The desired room alias local part. If this is included, a room alias will be created and mapped to the newly created room.
@ -51,32 +51,34 @@ class CreateRoomParams {
* For example, if this was set to "foo" and sent to the homeserver "example.com" the complete room alias would be #foo:example.com.
*/
@Json(name = "room_alias_name")
var roomAliasName: String? = null
val roomAliasName: String? = null,
/**
* If this is included, an m.room.name event will be sent into the room to indicate the name of the room.
* See Room Events for more information on m.room.name.
*/
var name: String? = null
@Json(name = "name")
val name: String? = null,
/**
* If this is included, an m.room.topic event will be sent into the room to indicate the topic for the room.
* See Room Events for more information on m.room.topic.
*/
var topic: String? = null
@Json(name = "topic")
val topic: String? = null,
/**
* A list of user IDs to invite to the room.
* This will tell the server to invite everyone in the list to the newly created room.
*/
@Json(name = "invite")
var invitedUserIds: MutableList<String>? = null
val invitedUserIds: List<String>? = null,
/**
* A list of objects representing third party IDs to invite into the room.
*/
@Json(name = "invite_3pid")
var invite3pids: MutableList<Invite3Pid>? = null
val invite3pids: List<Invite3Pid>? = null,
/**
* Extra keys to be added to the content of the m.room.create.
@ -84,7 +86,7 @@ class CreateRoomParams {
* Future versions of the specification may allow the server to clobber other keys.
*/
@Json(name = "creation_content")
var creationContent: Any? = null
val creationContent: Any? = null,
/**
* A list of state events to set in the new room.
@ -93,7 +95,7 @@ class CreateRoomParams {
* Takes precedence over events set by presets, but gets overridden by name and topic keys.
*/
@Json(name = "initial_state")
var initialStates: MutableList<Event>? = null
val initialStates: List<Event>? = null,
/**
* Convenience parameter for setting various default state events based on a preset. Must be either:
@ -102,28 +104,29 @@ class CreateRoomParams {
* room creator.
* public_chat: => join_rules is set to public. history_visibility is set to shared.
*/
var preset: CreateRoomPreset? = null
@Json(name = "preset")
val preset: CreateRoomPreset? = null,
/**
* This flag makes the server set the is_direct flag on the m.room.member events sent to the users in invite and invite_3pid.
* See Direct Messaging for more information.
*/
@Json(name = "is_direct")
var isDirect: Boolean? = null
val isDirect: Boolean? = null,
/**
* The power level content to override in the default power level event
*/
@Json(name = "power_level_content_override")
var powerLevelContentOverride: PowerLevelsContent? = null
val powerLevelContentOverride: PowerLevelsContent? = null
) {
/**
* Add the crypto algorithm to the room creation parameters.
*
* @param algorithm the algorithm
*/
fun enableEncryptionWithAlgorithm(algorithm: String) {
if (algorithm == MXCRYPTO_ALGORITHM_MEGOLM) {
fun enableEncryptionWithAlgorithm(algorithm: String): CreateRoomParams {
return if (algorithm == MXCRYPTO_ALGORITHM_MEGOLM) {
val contentMap = mapOf("algorithm" to algorithm)
val algoEvent = Event(
@ -132,13 +135,12 @@ class CreateRoomParams {
content = contentMap.toContent()
)
if (null == initialStates) {
initialStates = mutableListOf(algoEvent)
} else {
initialStates!!.add(algoEvent)
}
copy(
initialStates = initialStates.orEmpty().filter { it.type != EventType.STATE_ROOM_ENCRYPTION } + algoEvent
)
} else {
Timber.e("Unsupported algorithm: $algorithm")
this
}
}
@ -147,9 +149,10 @@ class CreateRoomParams {
*
* @param historyVisibility the expected history visibility, set null to remove any existing value.
*/
fun setHistoryVisibility(historyVisibility: RoomHistoryVisibility?) {
fun setHistoryVisibility(historyVisibility: RoomHistoryVisibility?): CreateRoomParams {
// Remove the existing value if any.
initialStates?.removeAll { it.type == EventType.STATE_ROOM_HISTORY_VISIBILITY }
val newInitialStates = initialStates
?.filter { it.type != EventType.STATE_ROOM_HISTORY_VISIBILITY }
if (historyVisibility != null) {
val contentMap = mapOf("history_visibility" to historyVisibility)
@ -159,20 +162,24 @@ class CreateRoomParams {
stateKey = "",
content = contentMap.toContent())
if (null == initialStates) {
initialStates = mutableListOf(historyVisibilityEvent)
return copy(
initialStates = newInitialStates.orEmpty() + historyVisibilityEvent
)
} else {
initialStates!!.add(historyVisibilityEvent)
}
return copy(
initialStates = newInitialStates
)
}
}
/**
* Mark as a direct message room.
*/
fun setDirectMessage() {
preset = CreateRoomPreset.PRESET_TRUSTED_PRIVATE_CHAT
fun setDirectMessage(): CreateRoomParams {
return copy(
preset = CreateRoomPreset.PRESET_TRUSTED_PRIVATE_CHAT,
isDirect = true
)
}
/**
@ -215,28 +222,26 @@ class CreateRoomParams {
*/
fun addParticipantIds(hsConfig: HomeServerConnectionConfig,
userId: String,
ids: List<String>) {
for (id in ids) {
if (Patterns.EMAIL_ADDRESS.matcher(id).matches() && hsConfig.identityServerUri != null) {
if (null == invite3pids) {
invite3pids = ArrayList()
}
val pid = Invite3Pid(idServer = hsConfig.identityServerUri.host!!,
ids: List<String>): CreateRoomParams {
return copy(
invite3pids = (invite3pids.orEmpty() + ids
.takeIf { hsConfig.identityServerUri != null }
?.filter { id -> Patterns.EMAIL_ADDRESS.matcher(id).matches() }
?.map { id ->
Invite3Pid(
idServer = hsConfig.identityServerUri!!.host!!,
medium = ThreePidMedium.EMAIL,
address = id)
invite3pids!!.add(pid)
} else if (isUserId(id)) {
address = id
)
}
.orEmpty())
.distinct(),
invitedUserIds = (invitedUserIds.orEmpty() + ids
.filter { id -> isUserId(id) }
// do not invite oneself
if (userId != id) {
if (null == invitedUserIds) {
invitedUserIds = ArrayList()
}
invitedUserIds!!.add(id)
}
}
.filter { id -> id != userId })
.distinct()
)
// TODO add phonenumbers when it will be available
}
}
}

View file

@ -93,8 +93,9 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
private fun createRoomAndInviteSelectedUsers() = withState { currentState ->
val isDirect = currentState.selectedUsers.size == 1
val roomParams = CreateRoomParams().apply {
val roomParams = CreateRoomParams(
invitedUserIds = ArrayList(currentState.selectedUsers.map { it.userId })
).apply {
if (isDirect) {
setDirectMessage()
}

View file

@ -127,10 +127,10 @@ class VerificationBottomSheetViewModel @AssistedInject constructor(@Assisted ini
pendingRequest = Loading()
)
}
val roomParams = CreateRoomParams().apply {
val roomParams = CreateRoomParams(
invitedUserIds = listOf(otherUserId).toMutableList()
setDirectMessage()
}
)
.setDirectMessage()
session.createRoom(roomParams, object : MatrixCallback<String> {
override fun onSuccess(data: String) {
setState {

View file

@ -81,15 +81,13 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr
copy(asyncCreateRoomRequest = Loading())
}
val createRoomParams = CreateRoomParams().apply {
name = state.roomName.takeIf { it.isNotBlank() }
val createRoomParams = CreateRoomParams(
name = state.roomName.takeIf { it.isNotBlank() },
// Directory visibility
visibility = if (state.isInRoomDirectory) RoomDirectoryVisibility.PUBLIC else RoomDirectoryVisibility.PRIVATE
visibility = if (state.isInRoomDirectory) RoomDirectoryVisibility.PUBLIC else RoomDirectoryVisibility.PRIVATE,
// Public room
preset = if (state.isPublic) CreateRoomPreset.PRESET_PUBLIC_CHAT else CreateRoomPreset.PRESET_PRIVATE_CHAT
}
)
session.createRoom(createRoomParams, object : MatrixCallback<String> {
override fun onSuccess(data: String) {