From 5364d7fd03d1252785eb89c80afa9c2dee623b32 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 26 Mar 2021 12:18:36 +0100 Subject: [PATCH] Be more robust when parsing some enums - Guest Access --- .../session/room/model/RoomGuestAccessContent.kt | 14 ++++++++++++-- .../session/room/state/DefaultStateService.kt | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomGuestAccessContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomGuestAccessContent.kt index 99b035d30e..8eea0d3d06 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomGuestAccessContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomGuestAccessContent.kt @@ -18,6 +18,7 @@ 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_GUEST_ACCESS state event content @@ -26,8 +27,17 @@ import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) data class RoomGuestAccessContent( // Required. Whether guests can join the room. One of: ["can_join", "forbidden"] - @Json(name = "guest_access") val guestAccess: GuestAccess? = null -) + @Json(name = "guest_access") private val _guestAccess: String? = null +) { + val guestAccess: GuestAccess? = when (_guestAccess) { + "can_join" -> GuestAccess.CanJoin + "forbidden" -> GuestAccess.Forbidden + else -> { + Timber.w("Invalid value for GuestAccess: `$_guestAccess`") + null + } + } +} @JsonClass(generateAdapter = false) enum class GuestAccess(val value: String) { 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 f2640fd1e7..3c42b77ae8 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 @@ -138,7 +138,7 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private if (guestAccess != null) { sendStateEvent( eventType = EventType.STATE_ROOM_GUEST_ACCESS, - body = RoomGuestAccessContent(guestAccess).toContent(), + body = mapOf("guest_access" to guestAccess), stateKey = null ) }