Skip to content

Commit

Permalink
Merge branch 'develop' into detached
Browse files Browse the repository at this point in the history
  • Loading branch information
kl3jvi authored Nov 28, 2024
2 parents 38420f6 + bee4d00 commit 86583a0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -258,11 +260,23 @@ data class Conversation(
fun toLogMap(): Map<String, Any?>
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, String> = when (this) {
is NotStarted -> mutableMapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<Any> {
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" }
}
}

0 comments on commit 86583a0

Please sign in to comment.