diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt index 9ff406e9e7..9b0ba71cf6 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt @@ -7,6 +7,16 @@ import org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSende class ClosedGroupUpdate() : ControlMessage() { + // Kind enum + sealed class Kind { + class New(val groupPublicKey: ByteArray, val name: String, val groupPrivateKey: ByteArray, val senderKeys: Collection, val members: Collection, val admins: Collection) : Kind() + class Info(val groupPublicKey: ByteArray, val name: String, val senderKeys: Collection, val members: Collection, val admins: Collection) : Kind() + class SenderKeyRequest(val groupPublicKey: ByteArray) : Kind() + class SenderKey(val groupPublicKey: ByteArray, val senderKey: org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey) : Kind() + } + + var kind: Kind? = null + companion object { const val TAG = "ClosedGroupUpdate" @@ -54,16 +64,6 @@ class ClosedGroupUpdate() : ControlMessage() { } } - // Kind enum - sealed class Kind { - class New(val groupPublicKey: ByteArray, val name: String, val groupPrivateKey: ByteArray, val senderKeys: Collection, val members: Collection, val admins: Collection) : Kind() - class Info(val groupPublicKey: ByteArray, val name: String, val senderKeys: Collection, val members: Collection, val admins: Collection) : Kind() - class SenderKeyRequest(val groupPublicKey: ByteArray) : Kind() - class SenderKey(val groupPublicKey: ByteArray, val senderKey: org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey) : Kind() - } - - var kind: Kind? = null - // constructor internal constructor(kind: Kind?) : this() { this.kind = kind diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ReadReceipt.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ReadReceipt.kt index c735820b13..960b5aeaf9 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ReadReceipt.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ReadReceipt.kt @@ -33,7 +33,7 @@ class ReadReceipt() : ControlMessage() { } override fun toProto(): SignalServiceProtos.Content? { - val timestamps = timestamps ?: return null + val timestamps = timestamps if (timestamps == null) { Log.w(ExpirationTimerUpdate.TAG, "Couldn't construct read receipt proto from: $this") return null diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/TypingIndicator.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/TypingIndicator.kt index 41e9498a60..005b11d155 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/TypingIndicator.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/TypingIndicator.kt @@ -1,16 +1,73 @@ package org.session.libsession.messaging.messages.control +import org.session.libsignal.libsignal.logging.Log import org.session.libsignal.service.internal.push.SignalServiceProtos -class TypingIndicator : ControlMessage() { +class TypingIndicator() : ControlMessage() { companion object { + const val TAG = "TypingIndicator" + fun fromProto(proto: SignalServiceProtos.Content): TypingIndicator? { - TODO("Not yet implemented") + val typingIndicatorProto = proto.typingMessage ?: return null + val kind = Kind.fromProto(typingIndicatorProto.action) + return TypingIndicator(kind = kind) } } + // Kind enum + enum class Kind { + STARTED, + STOPPED, + ; + + companion object { + @JvmStatic + fun fromProto(proto: SignalServiceProtos.TypingMessage.Action): Kind = + when (proto) { + SignalServiceProtos.TypingMessage.Action.STARTED -> STARTED + SignalServiceProtos.TypingMessage.Action.STOPPED -> STOPPED + } + } + + fun toProto(): SignalServiceProtos.TypingMessage.Action { + when (this) { + STARTED -> return SignalServiceProtos.TypingMessage.Action.STARTED + STOPPED -> return SignalServiceProtos.TypingMessage.Action.STOPPED + } + } + } + + var kind: Kind? = null + + //constructor + internal constructor(kind: Kind) : this() { + this.kind = kind + } + + // validation + override fun isValid(): Boolean { + if (!super.isValid()) return false + return kind != null + } + override fun toProto(): SignalServiceProtos.Content? { - TODO("Not yet implemented") + val timestamp = sentTimestamp + val kind = kind + if (timestamp == null || kind == null) { + Log.w(TAG, "Couldn't construct typing indicator proto from: $this") + return null + } + val typingIndicatorProto = SignalServiceProtos.TypingMessage.newBuilder() + typingIndicatorProto.timestamp = timestamp + typingIndicatorProto.action = kind.toProto() + val contentProto = SignalServiceProtos.Content.newBuilder() + try { + contentProto.typingMessage = typingIndicatorProto.build() + return contentProto.build() + } catch (e: Exception) { + Log.w(TAG, "Couldn't construct typing indicator proto from: $this") + return null + } } } \ No newline at end of file