clean up close group update message in new message pipeline

This commit is contained in:
Ryan ZHAO 2021-02-17 16:31:43 +11:00
parent 20ec889730
commit 64352707d0
4 changed files with 3 additions and 158 deletions

View File

@ -185,7 +185,7 @@ class ClosedGroupControlMessage() : ControlMessage() {
contentProto.dataMessage = dataMessageProto.build() contentProto.dataMessage = dataMessageProto.build()
return contentProto.build() return contentProto.build()
} catch (e: Exception) { } 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 return null
} }
} }

View File

@ -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<org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey>, val members: Collection<ByteArray>, val admins: Collection<ByteArray>) : Kind()
class Info(val groupPublicKey: ByteArray, val name: String, val senderKeys: Collection<org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey>, val members: Collection<ByteArray>, val admins: Collection<ByteArray>) : 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()
}

View File

@ -119,7 +119,7 @@ object MessageReceiver {
// Parse the message // Parse the message
val message: Message = ReadReceipt.fromProto(proto) ?: val message: Message = ReadReceipt.fromProto(proto) ?:
TypingIndicator.fromProto(proto) ?: TypingIndicator.fromProto(proto) ?:
ClosedGroupUpdate.fromProto(proto) ?: ClosedGroupControlMessage.fromProto(proto) ?:
ExpirationTimerUpdate.fromProto(proto) ?: ExpirationTimerUpdate.fromProto(proto) ?:
ConfigurationMessage.fromProto(proto) ?: ConfigurationMessage.fromProto(proto) ?:
VisibleMessage.fromProto(proto) ?: throw Error.UnknownMessage VisibleMessage.fromProto(proto) ?: throw Error.UnknownMessage

View File

@ -9,7 +9,6 @@ import org.session.libsession.messaging.jobs.NotifyPNServerJob
import org.session.libsession.messaging.messages.Destination import org.session.libsession.messaging.messages.Destination
import org.session.libsession.messaging.messages.Message import org.session.libsession.messaging.messages.Message
import org.session.libsession.messaging.messages.control.ClosedGroupControlMessage 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.control.ConfigurationMessage
import org.session.libsession.messaging.messages.visible.Attachment import org.session.libsession.messaging.messages.visible.Attachment
import org.session.libsession.messaging.messages.visible.Profile import org.session.libsession.messaging.messages.visible.Profile
@ -192,7 +191,7 @@ object MessageSender {
} }
handleSuccessfulMessageSend(message, destination, isSyncMessage) handleSuccessfulMessageSend(message, destination, isSyncMessage)
var shouldNotify = (message is VisibleMessage && !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 shouldNotify = true
} }
if (shouldNotify) { if (shouldNotify) {