From ee265f71e68b6acec22935c6d9663e754aa6b892 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 26 Mar 2021 12:22:11 +0100 Subject: [PATCH] Be more robust when parsing some enums - RoomJoinRules --- .../session/room/model/RoomJoinRulesContent.kt | 18 +++++++++++++++--- .../session/room/state/DefaultStateService.kt | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomJoinRulesContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomJoinRulesContent.kt index 3be2d38be7..8078951ad0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomJoinRulesContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomJoinRulesContent.kt @@ -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 + } + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/state/DefaultStateService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/state/DefaultStateService.kt index 3c42b77ae8..ebccd84f39 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/state/DefaultStateService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/state/DefaultStateService.kt @@ -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 ) }