diff --git a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/Conversation.kt b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/Conversation.kt index a74f1903193..aff93e9240e 100644 --- a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/Conversation.kt +++ b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/Conversation.kt @@ -38,6 +38,8 @@ import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.data.user.type.UserType import com.wire.kalium.util.serialization.toJsonElement import kotlinx.datetime.Instant +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable import kotlin.time.Duration /** @@ -258,11 +260,23 @@ data class Conversation( fun toLogMap(): Map } - data class Member(val id: UserId, val role: Role) { + @Serializable + data class Member( + @SerialName("id") val id: UserId, + @SerialName("role") val role: Role + ) { + + @Serializable sealed class Role { + + @Serializable data object Member : Role() + + @Serializable data object Admin : Role() - data class Unknown(val name: String) : Role() + + @Serializable + data class Unknown(@SerialName("name") val name: String) : Role() override fun toString(): String = when (this) { diff --git a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationStatus.kt b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationStatus.kt index f8b451f6eee..445bec974c1 100644 --- a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationStatus.kt +++ b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationStatus.kt @@ -18,22 +18,29 @@ package com.wire.kalium.logic.data.conversation +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + /** * Conversation muting settings type */ -sealed class MutedConversationStatus(open val status: Int = 0) { +@Serializable +sealed class MutedConversationStatus(@SerialName("status") open val status: Int = 0) { /** * 0 -> All notifications are displayed */ + @Serializable data object AllAllowed : MutedConversationStatus(0) /** * 1 -> Only mentions and replies are displayed (normal messages muted) */ + @Serializable data object OnlyMentionsAndRepliesAllowed : MutedConversationStatus(1) /** * 3 -> No notifications are displayed */ + @Serializable data object AllMuted : MutedConversationStatus(3) } diff --git a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/message/Message.kt b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/message/Message.kt index a188c0dabe5..5ea75254462 100644 --- a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/message/Message.kt +++ b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/message/Message.kt @@ -29,6 +29,8 @@ import com.wire.kalium.util.DateTimeUtil.toIsoDateTimeString import com.wire.kalium.util.serialization.toJsonElement import kotlinx.datetime.Clock import kotlinx.datetime.Instant +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import kotlin.time.Duration @@ -473,15 +475,20 @@ sealed interface Message { } } + @Serializable data class ExpirationData( - val expireAfter: Duration, - val selfDeletionStatus: SelfDeletionStatus = SelfDeletionStatus.NotStarted + @SerialName("expire_after") val expireAfter: Duration, + @SerialName("self_deletion_status") val selfDeletionStatus: SelfDeletionStatus = SelfDeletionStatus.NotStarted ) { + @Serializable sealed class SelfDeletionStatus { + + @Serializable data object NotStarted : SelfDeletionStatus() - data class Started(val selfDeletionEndDate: Instant) : SelfDeletionStatus() + @Serializable + data class Started(@SerialName("self_deletion_end_date") val selfDeletionEndDate: Instant) : SelfDeletionStatus() fun toLogMap(): Map = when (this) { is NotStarted -> mutableMapOf( diff --git a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/message/mention/MessageMention.kt b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/message/mention/MessageMention.kt index 504fac6903c..bb0b8069452 100644 --- a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/message/mention/MessageMention.kt +++ b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/message/mention/MessageMention.kt @@ -19,10 +19,13 @@ package com.wire.kalium.logic.data.message.mention import com.wire.kalium.logic.data.user.UserId +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +@Serializable data class MessageMention( - val start: Int, - val length: Int, - val userId: UserId, - val isSelfMention: Boolean + @SerialName("start") val start: Int, + @SerialName("length") val length: Int, + @SerialName("userId") val userId: UserId, + @SerialName("isSelfMention") val isSelfMention: Boolean ) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt index 58e95922ffa..9bbffa82e2c 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt @@ -456,7 +456,7 @@ class EventMapper( } @Suppress("MagicNumber") - private fun mapConversationMutedStatus(status: Int?) = when (status) { + private fun mapConversationMutedStatus(status: Int?): MutedConversationStatus = when (status) { 0 -> MutedConversationStatus.AllAllowed 1 -> MutedConversationStatus.OnlyMentionsAndRepliesAllowed 3 -> MutedConversationStatus.AllMuted diff --git a/util/src/commonMain/kotlin/com.wire.kalium.util/serialization/SerializationUtils.kt b/util/src/commonMain/kotlin/com.wire.kalium.util/serialization/SerializationUtils.kt index 67f1ab54c8f..39c4def02b8 100644 --- a/util/src/commonMain/kotlin/com.wire.kalium.util/serialization/SerializationUtils.kt +++ b/util/src/commonMain/kotlin/com.wire.kalium.util/serialization/SerializationUtils.kt @@ -18,11 +18,20 @@ package com.wire.kalium.util.serialization +import kotlinx.serialization.KSerializer +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.booleanOrNull +import kotlinx.serialization.json.contentOrNull +import kotlinx.serialization.json.doubleOrNull +import kotlinx.serialization.json.floatOrNull +import kotlinx.serialization.json.intOrNull +import kotlinx.serialization.json.longOrNull // See: https://github.com/Kotlin/kotlinx.serialization/issues/746#issuecomment-737000705 @@ -60,3 +69,29 @@ fun Map<*, *>.toJsonObject(): JsonObject { } return JsonObject(map) } + +private fun JsonElement.toAnyOrNull(): Any? { + return when (this) { + is JsonNull -> null + is JsonPrimitive -> toAnyValue() + is JsonObject -> this.map { it.key to it.value.toAnyOrNull() }.toMap() + is JsonArray -> this.map { it.toAnyOrNull() } + } +} + +private fun JsonPrimitive.toAnyValue(): Any? { + return this.booleanOrNull ?: this.intOrNull ?: this.longOrNull ?: this.floatOrNull ?: this.doubleOrNull ?: this.contentOrNull +} + +object AnyPrimitiveValueSerializer : KSerializer { + private val delegateSerializer = JsonElement.serializer() + override val descriptor = delegateSerializer.descriptor + override fun serialize(encoder: Encoder, value: Any) { + encoder.encodeSerializableValue(delegateSerializer, value.toJsonElement()) + } + + override fun deserialize(decoder: Decoder): Any { + val jsonPrimitive = decoder.decodeSerializableValue(delegateSerializer) + return requireNotNull(jsonPrimitive.toAnyOrNull()) { "value cannot be null" } + } +}