Merge pull request #3062 from vector-im/feature/bma/enum

Be more robust when parsing some enums
This commit is contained in:
Benoit Marty 2021-03-26 15:29:51 +01:00 committed by GitHub
commit ebc2017fe0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 62 additions and 50 deletions

View file

@ -11,6 +11,7 @@ Improvements 🙌:
- Improve message with Emoji only detection (#3017) - Improve message with Emoji only detection (#3017)
- Picture preview when replying. Also add the image preview in the message detail bottomsheet (#2916) - Picture preview when replying. Also add the image preview in the message detail bottomsheet (#2916)
- Api interceptor to allow app developers peek responses (#2986) - Api interceptor to allow app developers peek responses (#2986)
- Be more robust when parsing some enums
Bugfix 🐛: Bugfix 🐛:
- Fix bad theme change for the MainActivity - Fix bad theme change for the MainActivity

View file

@ -23,24 +23,13 @@ import com.squareup.moshi.JsonClass
* Represents the membership of a user on a room * Represents the membership of a user on a room
*/ */
@JsonClass(generateAdapter = false) @JsonClass(generateAdapter = false)
enum class Membership(val value: String) { enum class Membership {
NONE,
NONE("none"), @Json(name = "invite") INVITE,
@Json(name = "join") JOIN,
@Json(name = "invite") @Json(name = "knock") KNOCK,
INVITE("invite"), @Json(name = "leave") LEAVE,
@Json(name = "ban") BAN;
@Json(name = "join")
JOIN("join"),
@Json(name = "knock")
KNOCK("knock"),
@Json(name = "leave")
LEAVE("leave"),
@Json(name = "ban")
BAN("ban");
fun isLeft(): Boolean { fun isLeft(): Boolean {
return this == KNOCK || this == LEAVE || this == BAN return this == KNOCK || this == LEAVE || this == BAN

View file

@ -18,6 +18,7 @@ package org.matrix.android.sdk.api.session.room.model
import com.squareup.moshi.Json import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
import timber.log.Timber
/** /**
* Class representing the EventType.STATE_ROOM_GUEST_ACCESS state event content * Class representing the EventType.STATE_ROOM_GUEST_ACCESS state event content
@ -26,14 +27,20 @@ import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true) @JsonClass(generateAdapter = true)
data class RoomGuestAccessContent( data class RoomGuestAccessContent(
// Required. Whether guests can join the room. One of: ["can_join", "forbidden"] // Required. Whether guests can join the room. One of: ["can_join", "forbidden"]
@Json(name = "guest_access") val guestAccess: GuestAccess? = null @Json(name = "guest_access") 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) @JsonClass(generateAdapter = false)
enum class GuestAccess(val value: String) { enum class GuestAccess {
@Json(name = "can_join") @Json(name = "can_join") CanJoin,
CanJoin("can_join"), @Json(name = "forbidden") Forbidden
@Json(name = "forbidden")
Forbidden("forbidden")
} }

View file

@ -18,8 +18,20 @@ package org.matrix.android.sdk.api.session.room.model
import com.squareup.moshi.Json import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
import timber.log.Timber
@JsonClass(generateAdapter = true) @JsonClass(generateAdapter = true)
data class RoomHistoryVisibilityContent( data class RoomHistoryVisibilityContent(
@Json(name = "history_visibility") val historyVisibility: RoomHistoryVisibility? = null @Json(name = "history_visibility") 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
}
}
}

View file

@ -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 * Enum for [RoomJoinRulesContent] : https://matrix.org/docs/spec/client_server/r0.4.0#m-room-join-rules
*/ */
@JsonClass(generateAdapter = false) @JsonClass(generateAdapter = false)
enum class RoomJoinRules(val value: String) { enum class RoomJoinRules {
@Json(name = "public") PUBLIC,
@Json(name = "public") @Json(name = "invite") INVITE,
PUBLIC("public"), @Json(name = "knock") KNOCK,
@Json(name = "private") PRIVATE
@Json(name = "invite")
INVITE("invite"),
@Json(name = "knock")
KNOCK("knock"),
@Json(name = "private")
PRIVATE("private")
} }

View file

@ -19,11 +19,23 @@ package org.matrix.android.sdk.api.session.room.model
import com.squareup.moshi.Json import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
import timber.log.Timber
/** /**
* Class representing the EventType.STATE_ROOM_JOIN_RULES state event content * Class representing the EventType.STATE_ROOM_JOIN_RULES state event content
*/ */
@JsonClass(generateAdapter = true) @JsonClass(generateAdapter = true)
data class RoomJoinRulesContent( data class RoomJoinRulesContent(
@Json(name = "join_rule") val joinRules: RoomJoinRules? = null @Json(name = "join_rule") 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

@ -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.Content
import org.matrix.android.sdk.api.session.events.model.Event 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.PublicRoomsParams
import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsResponse import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsResponse
import org.matrix.android.sdk.api.util.JsonDict 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") @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "rooms/{roomId}/members")
fun getMembers(@Path("roomId") roomId: String, fun getMembers(@Path("roomId") roomId: String,
@Query("at") syncToken: String?, @Query("at") syncToken: String?,
@Query("membership") membership: String?, @Query("membership") membership: Membership?,
@Query("not_membership") notMembership: String? @Query("not_membership") notMembership: Membership?
): Call<RoomMembersResponse> ): Call<RoomMembersResponse>
/** /**

View file

@ -91,7 +91,7 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(
val lastToken = syncTokenStore.getLastToken() val lastToken = syncTokenStore.getLastToken()
val response = try { val response = try {
executeRequest<RoomMembersResponse>(globalErrorReceiver) { executeRequest<RoomMembersResponse>(globalErrorReceiver) {
apiCall = roomAPI.getMembers(params.roomId, lastToken, null, params.excludeMembership?.value) apiCall = roomAPI.getMembers(params.roomId, lastToken, null, params.excludeMembership)
} }
} catch (throwable: Throwable) { } catch (throwable: Throwable) {
// Revert status to NONE // Revert status to NONE

View file

@ -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.events.model.toContent
import org.matrix.android.sdk.api.session.room.model.GuestAccess 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.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.RoomHistoryVisibility
import org.matrix.android.sdk.api.session.room.model.RoomJoinRules 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.session.room.state.StateService
import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.api.util.JsonDict
import org.matrix.android.sdk.api.util.MimeTypes import org.matrix.android.sdk.api.util.MimeTypes
@ -131,14 +129,14 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
if (joinRules != null) { if (joinRules != null) {
sendStateEvent( sendStateEvent(
eventType = EventType.STATE_ROOM_JOIN_RULES, eventType = EventType.STATE_ROOM_JOIN_RULES,
body = RoomJoinRulesContent(joinRules).toContent(), body = mapOf("join_rule" to joinRules),
stateKey = null stateKey = null
) )
} }
if (guestAccess != null) { if (guestAccess != null) {
sendStateEvent( sendStateEvent(
eventType = EventType.STATE_ROOM_GUEST_ACCESS, eventType = EventType.STATE_ROOM_GUEST_ACCESS,
body = RoomGuestAccessContent(guestAccess).toContent(), body = mapOf("guest_access" to guestAccess),
stateKey = null stateKey = null
) )
} }