From 64352707d00f8b8dfd8373f3035206ee2e2b3a6a Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Wed, 17 Feb 2021 16:31:43 +1100 Subject: [PATCH] clean up close group update message in new message pipeline --- .../control/ClosedGroupControlMessage.kt | 2 +- .../messages/control/ClosedGroupUpdate.kt | 154 ------------------ .../sending_receiving/MessageReceiver.kt | 2 +- .../sending_receiving/MessageSender.kt | 3 +- 4 files changed, 3 insertions(+), 158 deletions(-) delete mode 100644 libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupControlMessage.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupControlMessage.kt index 41c9a92e8b..c751e8b57b 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupControlMessage.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupControlMessage.kt @@ -185,7 +185,7 @@ class ClosedGroupControlMessage() : ControlMessage() { contentProto.dataMessage = dataMessageProto.build() return contentProto.build() } catch (e: Exception) { - Log.w(ClosedGroupUpdate.TAG, "Couldn't construct closed group update proto from: $this") + Log.w(TAG, "Couldn't construct closed group update proto from: $this") return null } } 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 deleted file mode 100644 index c2fe8094f4..0000000000 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt +++ /dev/null @@ -1,154 +0,0 @@ -package org.session.libsession.messaging.messages.control - -import com.google.protobuf.ByteString -import org.session.libsignal.utilities.logging.Log -import org.session.libsignal.service.internal.push.SignalServiceProtos -import org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey - -class ClosedGroupUpdate() : ControlMessage() { - - var kind: Kind? = null - - // 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() - } - - companion object { - const val TAG = "ClosedGroupUpdate" - - fun fromProto(proto: SignalServiceProtos.Content): ClosedGroupUpdate? { - val closedGroupUpdateProto = proto.dataMessage?.closedGroupUpdate ?: return null - val groupPublicKey = closedGroupUpdateProto.groupPublicKey - var kind: Kind - when(closedGroupUpdateProto.type) { - SignalServiceProtos.ClosedGroupUpdate.Type.NEW -> { - val name = closedGroupUpdateProto.name ?: return null - val groupPrivateKey = closedGroupUpdateProto.groupPrivateKey ?: return null - val senderKeys = closedGroupUpdateProto.senderKeysList.map { ClosedGroupSenderKey.fromProto(it) } - kind = Kind.New( - groupPublicKey = groupPublicKey.toByteArray(), - name = name, - groupPrivateKey = groupPrivateKey.toByteArray(), - senderKeys = senderKeys, - members = closedGroupUpdateProto.membersList.map { it.toByteArray() }, - admins = closedGroupUpdateProto.adminsList.map { it.toByteArray() } - ) - } - SignalServiceProtos.ClosedGroupUpdate.Type.INFO -> { - val name = closedGroupUpdateProto.name ?: return null - val senderKeys = closedGroupUpdateProto.senderKeysList.map { ClosedGroupSenderKey.fromProto(it) } - kind = Kind.Info( - groupPublicKey = groupPublicKey.toByteArray(), - name = name, - senderKeys = senderKeys, - members = closedGroupUpdateProto.membersList.map { it.toByteArray() }, - admins = closedGroupUpdateProto.adminsList.map { it.toByteArray() } - ) - } - SignalServiceProtos.ClosedGroupUpdate.Type.SENDER_KEY_REQUEST -> { - kind = Kind.SenderKeyRequest(groupPublicKey = groupPublicKey.toByteArray()) - } - SignalServiceProtos.ClosedGroupUpdate.Type.SENDER_KEY -> { - val senderKeyProto = closedGroupUpdateProto.senderKeysList?.first() ?: return null - kind = Kind.SenderKey( - groupPublicKey = groupPublicKey.toByteArray(), - senderKey = ClosedGroupSenderKey.fromProto(senderKeyProto) - ) - } - } - return ClosedGroupUpdate(kind) - } - } - - // constructor - internal constructor(kind: Kind?) : this() { - this.kind = kind - } - - // validation - override fun isValid(): Boolean { - if (!super.isValid()) return false - val kind = kind ?: return false - when(kind) { - is Kind.New -> { - return !kind.groupPublicKey.isEmpty() && !kind.name.isEmpty() && !kind.groupPrivateKey.isEmpty() && !kind.members.isEmpty() && !kind.admins.isEmpty() - } - is Kind.Info -> { - return !kind.groupPublicKey.isEmpty() && !kind.name.isEmpty() && !kind.members.isEmpty() && !kind.admins.isEmpty() - } - is Kind.SenderKeyRequest -> { - return !kind.groupPublicKey.isEmpty() - } - is Kind.SenderKey -> { - return !kind.groupPublicKey.isEmpty() - } - } - } - - override fun toProto(): SignalServiceProtos.Content? { - val kind = kind - if (kind == null) { - Log.w(TAG, "Couldn't construct closed group update proto from: $this") - return null - } - try { - val closedGroupUpdate: SignalServiceProtos.ClosedGroupUpdate.Builder = SignalServiceProtos.ClosedGroupUpdate.newBuilder() - when (kind) { - is Kind.New -> { - closedGroupUpdate.groupPublicKey = ByteString.copyFrom(kind.groupPublicKey) - closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdate.Type.NEW - closedGroupUpdate.name = kind.name - closedGroupUpdate.groupPrivateKey = ByteString.copyFrom(kind.groupPrivateKey) - closedGroupUpdate.addAllSenderKeys(kind.senderKeys.map { it.toProto() }) - closedGroupUpdate.addAllMembers(kind.members.map { ByteString.copyFrom(it) }) - closedGroupUpdate.addAllAdmins(kind.admins.map { ByteString.copyFrom(it) }) - } - is Kind.Info -> { - closedGroupUpdate.groupPublicKey = ByteString.copyFrom(kind.groupPublicKey) - closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdate.Type.INFO - closedGroupUpdate.name = kind.name - closedGroupUpdate.addAllSenderKeys(kind.senderKeys.map { it.toProto() }) - closedGroupUpdate.addAllMembers(kind.members.map { ByteString.copyFrom(it) }) - closedGroupUpdate.addAllAdmins(kind.admins.map { ByteString.copyFrom(it) }) - } - is Kind.SenderKeyRequest -> { - closedGroupUpdate.groupPublicKey = ByteString.copyFrom(kind.groupPublicKey) - closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdate.Type.SENDER_KEY_REQUEST - } - is Kind.SenderKey -> { - closedGroupUpdate.groupPublicKey = ByteString.copyFrom(kind.groupPublicKey) - closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdate.Type.SENDER_KEY - closedGroupUpdate.addAllSenderKeys(listOf( kind.senderKey.toProto() )) - } - } - val contentProto = SignalServiceProtos.Content.newBuilder() - val dataMessageProto = SignalServiceProtos.DataMessage.newBuilder() - dataMessageProto.closedGroupUpdate = closedGroupUpdate.build() - contentProto.dataMessage = dataMessageProto.build() - return contentProto.build() - } catch (e: Exception) { - Log.w(TAG, "Couldn't construct closed group update proto from: $this") - return null - } - return null - } - -} - -// extension functions to class ClosedGroupSenderKey - -private fun ClosedGroupSenderKey.Companion.fromProto(proto: SignalServiceProtos.ClosedGroupUpdate.SenderKey): ClosedGroupSenderKey { - return ClosedGroupSenderKey(chainKey = proto.chainKey.toByteArray(), keyIndex = proto.keyIndex, publicKey = proto.publicKey.toByteArray()) -} - -private fun ClosedGroupSenderKey.toProto(): SignalServiceProtos.ClosedGroupUpdate.SenderKey { - val proto = SignalServiceProtos.ClosedGroupUpdate.SenderKey.newBuilder() - proto.chainKey = ByteString.copyFrom(chainKey) - proto.keyIndex = keyIndex - proto.publicKey = ByteString.copyFrom(publicKey) - return proto.build() -} \ No newline at end of file diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiver.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiver.kt index 6ec3d4e1a3..5b661327cb 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiver.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiver.kt @@ -119,7 +119,7 @@ object MessageReceiver { // Parse the message val message: Message = ReadReceipt.fromProto(proto) ?: TypingIndicator.fromProto(proto) ?: - ClosedGroupUpdate.fromProto(proto) ?: + ClosedGroupControlMessage.fromProto(proto) ?: ExpirationTimerUpdate.fromProto(proto) ?: ConfigurationMessage.fromProto(proto) ?: VisibleMessage.fromProto(proto) ?: throw Error.UnknownMessage diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt index f8840356ba..044683f155 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt @@ -9,7 +9,6 @@ import org.session.libsession.messaging.jobs.NotifyPNServerJob import org.session.libsession.messaging.messages.Destination import org.session.libsession.messaging.messages.Message import org.session.libsession.messaging.messages.control.ClosedGroupControlMessage -import org.session.libsession.messaging.messages.control.ClosedGroupUpdate import org.session.libsession.messaging.messages.control.ConfigurationMessage import org.session.libsession.messaging.messages.visible.Attachment import org.session.libsession.messaging.messages.visible.Profile @@ -192,7 +191,7 @@ object MessageSender { } handleSuccessfulMessageSend(message, destination, isSyncMessage) var shouldNotify = (message is VisibleMessage && !isSyncMessage) - if (message is ClosedGroupUpdate && message.kind is ClosedGroupUpdate.Kind.New) { + if (message is ClosedGroupControlMessage && message.kind is ClosedGroupControlMessage.Kind.New) { shouldNotify = true } if (shouldNotify) {