Be more robust when parsing some enums - RoomJoinRules

This commit is contained in:
Benoit Marty 2021-03-26 12:22:11 +01:00
parent 5364d7fd03
commit ee265f71e6
2 changed files with 16 additions and 4 deletions

View file

@ -19,11 +19,23 @@ package org.matrix.android.sdk.api.session.room.model
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import timber.log.Timber
/**
* Class representing the EventType.STATE_ROOM_JOIN_RULES state event content
*/
@JsonClass(generateAdapter = true)
data class RoomJoinRulesContent(
@Json(name = "join_rule") val joinRules: RoomJoinRules? = null
)
data class RoomJoinRulesContent constructor(
@Json(name = "join_rule") private val _joinRules: String? = null
) {
val joinRules: RoomJoinRules? = when (_joinRules) {
"public" -> RoomJoinRules.PUBLIC
"invite" -> RoomJoinRules.INVITE
"knock" -> RoomJoinRules.KNOCK
"private" -> RoomJoinRules.PRIVATE
else -> {
Timber.w("Invalid value for RoomJoinRules: `$_joinRules`")
null
}
}
}

View file

@ -131,7 +131,7 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
if (joinRules != null) {
sendStateEvent(
eventType = EventType.STATE_ROOM_JOIN_RULES,
body = RoomJoinRulesContent(joinRules).toContent(),
body = mapOf("join_rule" to joinRules),
stateKey = null
)
}