From a7539d0f9508cfe40347cdccd73147ffee3cdda3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 26 Mar 2021 12:15:06 +0100 Subject: [PATCH 1/6] Be more robust when parsing some enums --- CHANGES.md | 1 + .../room/model/RoomHistoryVisibilityContent.kt | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 68b6194734..53383c14de 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ Improvements 🙌: - Improve message with Emoji only detection (#3017) - Picture preview when replying. Also add the image preview in the message detail bottomsheet (#2916) - Api interceptor to allow app developers peek responses (#2986) + - Be more robust when parsing some enums Bugfix 🐛: - Fix bad theme change for the MainActivity diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomHistoryVisibilityContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomHistoryVisibilityContent.kt index 31493be7ea..b16d81dbaa 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomHistoryVisibilityContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomHistoryVisibilityContent.kt @@ -18,8 +18,20 @@ package org.matrix.android.sdk.api.session.room.model import com.squareup.moshi.Json import com.squareup.moshi.JsonClass +import timber.log.Timber @JsonClass(generateAdapter = true) data class RoomHistoryVisibilityContent( - @Json(name = "history_visibility") val historyVisibility: RoomHistoryVisibility? = null -) + @Json(name = "history_visibility") private val _historyVisibility: String? = null +) { + val historyVisibility: RoomHistoryVisibility? = when (_historyVisibility) { + "world_readable" -> RoomHistoryVisibility.WORLD_READABLE + "shared" -> RoomHistoryVisibility.SHARED + "invited" -> RoomHistoryVisibility.INVITED + "joined" -> RoomHistoryVisibility.JOINED + else -> { + Timber.w("Invalid value for RoomHistoryVisibility: `$_historyVisibility`") + null + } + } +} From 5364d7fd03d1252785eb89c80afa9c2dee623b32 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 26 Mar 2021 12:18:36 +0100 Subject: [PATCH 2/6] 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 ) } From ee265f71e68b6acec22935c6d9663e754aa6b892 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 26 Mar 2021 12:22:11 +0100 Subject: [PATCH 3/6] 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 ) } From 2e23fec82baf3d1ec85f6ef458031a53b574cb5d Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 26 Mar 2021 12:36:33 +0100 Subject: [PATCH 4/6] Sadly fields cannot be private --- .../sdk/api/session/room/model/RoomGuestAccessContent.kt | 2 +- .../api/session/room/model/RoomHistoryVisibilityContent.kt | 2 +- .../sdk/api/session/room/model/RoomJoinRulesContent.kt | 4 ++-- 3 files changed, 4 insertions(+), 4 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 8eea0d3d06..24a56b6b56 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 @@ -27,7 +27,7 @@ import timber.log.Timber @JsonClass(generateAdapter = true) data class RoomGuestAccessContent( // Required. Whether guests can join the room. One of: ["can_join", "forbidden"] - @Json(name = "guest_access") private val _guestAccess: String? = null + @Json(name = "guest_access") val _guestAccess: String? = null ) { val guestAccess: GuestAccess? = when (_guestAccess) { "can_join" -> GuestAccess.CanJoin diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomHistoryVisibilityContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomHistoryVisibilityContent.kt index b16d81dbaa..3ac14e48de 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomHistoryVisibilityContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomHistoryVisibilityContent.kt @@ -22,7 +22,7 @@ import timber.log.Timber @JsonClass(generateAdapter = true) data class RoomHistoryVisibilityContent( - @Json(name = "history_visibility") private val _historyVisibility: String? = null + @Json(name = "history_visibility") val _historyVisibility: String? = null ) { val historyVisibility: RoomHistoryVisibility? = when (_historyVisibility) { "world_readable" -> RoomHistoryVisibility.WORLD_READABLE 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 8078951ad0..8082486b22 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 @@ -25,8 +25,8 @@ import timber.log.Timber * Class representing the EventType.STATE_ROOM_JOIN_RULES state event content */ @JsonClass(generateAdapter = true) -data class RoomJoinRulesContent constructor( - @Json(name = "join_rule") private val _joinRules: String? = null +data class RoomJoinRulesContent( + @Json(name = "join_rule") val _joinRules: String? = null ) { val joinRules: RoomJoinRules? = when (_joinRules) { "public" -> RoomJoinRules.PUBLIC From 297fff1394b201596c2d76dcc40209cf82e2aead Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 26 Mar 2021 13:41:10 +0100 Subject: [PATCH 5/6] Cleanup some Enums --- .../sdk/api/session/room/model/Membership.kt | 25 ++++++------------- .../room/model/RoomGuestAccessContent.kt | 9 +++---- .../api/session/room/model/RoomJoinRules.kt | 18 ++++--------- .../sdk/internal/session/room/RoomAPI.kt | 5 ++-- .../room/membership/LoadRoomMembersTask.kt | 2 +- 5 files changed, 19 insertions(+), 40 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/Membership.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/Membership.kt index 5844aead8d..a5d0f63722 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/Membership.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/Membership.kt @@ -23,24 +23,13 @@ import com.squareup.moshi.JsonClass * Represents the membership of a user on a room */ @JsonClass(generateAdapter = false) -enum class Membership(val value: String) { - - NONE("none"), - - @Json(name = "invite") - INVITE("invite"), - - @Json(name = "join") - JOIN("join"), - - @Json(name = "knock") - KNOCK("knock"), - - @Json(name = "leave") - LEAVE("leave"), - - @Json(name = "ban") - BAN("ban"); +enum class Membership { + NONE, + @Json(name = "invite") INVITE, + @Json(name = "join") JOIN, + @Json(name = "knock") KNOCK, + @Json(name = "leave") LEAVE, + @Json(name = "ban") BAN; fun isLeft(): Boolean { return this == KNOCK || this == LEAVE || this == BAN 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 24a56b6b56..0760c6f1b4 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 @@ -40,10 +40,7 @@ data class RoomGuestAccessContent( } @JsonClass(generateAdapter = false) -enum class GuestAccess(val value: String) { - @Json(name = "can_join") - CanJoin("can_join"), - - @Json(name = "forbidden") - Forbidden("forbidden") +enum class GuestAccess { + @Json(name = "can_join") CanJoin, + @Json(name = "forbidden") Forbidden } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomJoinRules.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomJoinRules.kt index 09aacfabbe..f3e8d357f3 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomJoinRules.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomJoinRules.kt @@ -24,17 +24,9 @@ import com.squareup.moshi.JsonClass * Enum for [RoomJoinRulesContent] : https://matrix.org/docs/spec/client_server/r0.4.0#m-room-join-rules */ @JsonClass(generateAdapter = false) -enum class RoomJoinRules(val value: String) { - - @Json(name = "public") - PUBLIC("public"), - - @Json(name = "invite") - INVITE("invite"), - - @Json(name = "knock") - KNOCK("knock"), - - @Json(name = "private") - PRIVATE("private") +enum class RoomJoinRules { + @Json(name = "public") PUBLIC, + @Json(name = "invite") INVITE, + @Json(name = "knock") KNOCK, + @Json(name = "private") PRIVATE } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomAPI.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomAPI.kt index 20cb49ee8a..b065a30fc9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomAPI.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomAPI.kt @@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.session.room import org.matrix.android.sdk.api.session.events.model.Content import org.matrix.android.sdk.api.session.events.model.Event +import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsParams import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsResponse import org.matrix.android.sdk.api.util.JsonDict @@ -100,8 +101,8 @@ internal interface RoomAPI { @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "rooms/{roomId}/members") fun getMembers(@Path("roomId") roomId: String, @Query("at") syncToken: String?, - @Query("membership") membership: String?, - @Query("not_membership") notMembership: String? + @Query("membership") membership: Membership?, + @Query("not_membership") notMembership: Membership? ): Call /** diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/LoadRoomMembersTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/LoadRoomMembersTask.kt index cc491d1cd9..6adf3c59d1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/LoadRoomMembersTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/LoadRoomMembersTask.kt @@ -91,7 +91,7 @@ internal class DefaultLoadRoomMembersTask @Inject constructor( val lastToken = syncTokenStore.getLastToken() val response = try { executeRequest(globalErrorReceiver) { - apiCall = roomAPI.getMembers(params.roomId, lastToken, null, params.excludeMembership?.value) + apiCall = roomAPI.getMembers(params.roomId, lastToken, null, params.excludeMembership) } } catch (throwable: Throwable) { // Revert status to NONE From 94220a24d147ce1d6cea3b873d94f64a8b72d527 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 26 Mar 2021 13:58:34 +0100 Subject: [PATCH 6/6] cleanup --- .../sdk/internal/session/room/state/DefaultStateService.kt | 2 -- 1 file changed, 2 deletions(-) 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 ebccd84f39..615bc99096 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 @@ -27,10 +27,8 @@ import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.toContent import org.matrix.android.sdk.api.session.room.model.GuestAccess import org.matrix.android.sdk.api.session.room.model.RoomCanonicalAliasContent -import org.matrix.android.sdk.api.session.room.model.RoomGuestAccessContent import org.matrix.android.sdk.api.session.room.model.RoomHistoryVisibility import org.matrix.android.sdk.api.session.room.model.RoomJoinRules -import org.matrix.android.sdk.api.session.room.model.RoomJoinRulesContent import org.matrix.android.sdk.api.session.room.state.StateService import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.api.util.MimeTypes