Simplify ClosedGroupControlMessage#fromProto

This commit is contained in:
Andrew 2024-02-21 01:17:32 +10:30
parent 39df560f0c
commit 3cd5ca4a6d
2 changed files with 32 additions and 48 deletions

View File

@ -239,8 +239,7 @@ object MockDataGenerator {
// Add the group created message // Add the group created message
if (userSessionId == adminUserId) { if (userSessionId == adminUserId) {
storage.insertOutgoingInfoMessage(context, groupId, SignalServiceGroup.Type.CREATION, groupName, members, listOf(adminUserId), threadId, (timestampNow - (numMessages * 5000))) storage.insertOutgoingInfoMessage(context, groupId, SignalServiceGroup.Type.CREATION, groupName, members, listOf(adminUserId), threadId, (timestampNow - (numMessages * 5000)))
} } else {
else {
storage.insertIncomingInfoMessage(context, adminUserId, groupId, SignalServiceGroup.Type.CREATION, groupName, members, listOf(adminUserId), (timestampNow - (numMessages * 5000))) storage.insertIncomingInfoMessage(context, adminUserId, groupId, SignalServiceGroup.Type.CREATION, groupName, members, listOf(adminUserId), (timestampNow - (numMessages * 5000)))
} }

View File

@ -7,6 +7,12 @@ import org.session.libsignal.crypto.ecc.DjbECPublicKey
import org.session.libsignal.crypto.ecc.ECKeyPair import org.session.libsignal.crypto.ecc.ECKeyPair
import org.session.libsignal.protos.SignalServiceProtos import org.session.libsignal.protos.SignalServiceProtos
import org.session.libsignal.protos.SignalServiceProtos.DataMessage import org.session.libsignal.protos.SignalServiceProtos.DataMessage
import org.session.libsignal.protos.SignalServiceProtos.DataMessage.ClosedGroupControlMessage.Type.ENCRYPTION_KEY_PAIR
import org.session.libsignal.protos.SignalServiceProtos.DataMessage.ClosedGroupControlMessage.Type.MEMBERS_ADDED
import org.session.libsignal.protos.SignalServiceProtos.DataMessage.ClosedGroupControlMessage.Type.MEMBERS_REMOVED
import org.session.libsignal.protos.SignalServiceProtos.DataMessage.ClosedGroupControlMessage.Type.MEMBER_LEFT
import org.session.libsignal.protos.SignalServiceProtos.DataMessage.ClosedGroupControlMessage.Type.NAME_CHANGE
import org.session.libsignal.protos.SignalServiceProtos.DataMessage.ClosedGroupControlMessage.Type.NEW
import org.session.libsignal.utilities.Hex import org.session.libsignal.utilities.Hex
import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.removingIdPrefixIfNeeded import org.session.libsignal.utilities.removingIdPrefixIfNeeded
@ -78,46 +84,25 @@ class ClosedGroupControlMessage() : ControlMessage() {
companion object { companion object {
const val TAG = "ClosedGroupControlMessage" const val TAG = "ClosedGroupControlMessage"
fun fromProto(proto: SignalServiceProtos.Content): ClosedGroupControlMessage? { fun fromProto(proto: SignalServiceProtos.Content): ClosedGroupControlMessage? =
if (!proto.hasDataMessage() || !proto.dataMessage.hasClosedGroupControlMessage()) return null proto.takeIf { it.hasDataMessage() }?.dataMessage
val closedGroupControlMessageProto = proto.dataMessage!!.closedGroupControlMessage!! ?.takeIf { it.hasClosedGroupControlMessage() }?.closedGroupControlMessage
val kind: Kind ?.run {
when (closedGroupControlMessageProto.type!!) { when (type) {
DataMessage.ClosedGroupControlMessage.Type.NEW -> { NEW -> takeIf { it.hasPublicKey() && it.hasEncryptionKeyPair() && it.hasName() }?.let {
val publicKey = closedGroupControlMessageProto.publicKey ?: return null ECKeyPair(
val name = closedGroupControlMessageProto.name ?: return null DjbECPublicKey(publicKey.toByteArray()),
val encryptionKeyPairAsProto = closedGroupControlMessageProto.encryptionKeyPair ?: return null DjbECPrivateKey(encryptionKeyPair.privateKey.toByteArray())
val expirationTimer = closedGroupControlMessageProto.expirationTimer ).let { Kind.New(publicKey, name, it, membersList, adminsList, expirationTimer) }
try { }
val encryptionKeyPair = ECKeyPair(DjbECPublicKey(encryptionKeyPairAsProto.publicKey.toByteArray()), ENCRYPTION_KEY_PAIR -> Kind.EncryptionKeyPair(publicKey, wrappersList.mapNotNull(KeyPairWrapper::fromProto))
DjbECPrivateKey(encryptionKeyPairAsProto.privateKey.toByteArray())) NAME_CHANGE -> takeIf { it.hasName() }?.let { Kind.NameChange(name) }
kind = Kind.New(publicKey, name, encryptionKeyPair, closedGroupControlMessageProto.membersList, closedGroupControlMessageProto.adminsList, expirationTimer) MEMBERS_ADDED -> Kind.MembersAdded(membersList)
} catch (e: Exception) { MEMBERS_REMOVED -> Kind.MembersRemoved(membersList)
Log.w(TAG, "Couldn't parse key pair from proto: $encryptionKeyPairAsProto.") MEMBER_LEFT -> Kind.MemberLeft()
return null else -> null
} }?.let(::ClosedGroupControlMessage)
} }
DataMessage.ClosedGroupControlMessage.Type.ENCRYPTION_KEY_PAIR -> {
val publicKey = closedGroupControlMessageProto.publicKey
val wrappers = closedGroupControlMessageProto.wrappersList.mapNotNull { KeyPairWrapper.fromProto(it) }
kind = Kind.EncryptionKeyPair(publicKey, wrappers)
}
DataMessage.ClosedGroupControlMessage.Type.NAME_CHANGE -> {
val name = closedGroupControlMessageProto.name ?: return null
kind = Kind.NameChange(name)
}
DataMessage.ClosedGroupControlMessage.Type.MEMBERS_ADDED -> {
kind = Kind.MembersAdded(closedGroupControlMessageProto.membersList)
}
DataMessage.ClosedGroupControlMessage.Type.MEMBERS_REMOVED -> {
kind = Kind.MembersRemoved(closedGroupControlMessageProto.membersList)
}
DataMessage.ClosedGroupControlMessage.Type.MEMBER_LEFT -> {
kind = Kind.MemberLeft()
}
}
return ClosedGroupControlMessage(kind)
}
} }
internal constructor(kind: Kind?, groupID: String? = null) : this() { internal constructor(kind: Kind?, groupID: String? = null) : this() {
@ -135,7 +120,7 @@ class ClosedGroupControlMessage() : ControlMessage() {
val closedGroupControlMessage: DataMessage.ClosedGroupControlMessage.Builder = DataMessage.ClosedGroupControlMessage.newBuilder() val closedGroupControlMessage: DataMessage.ClosedGroupControlMessage.Builder = DataMessage.ClosedGroupControlMessage.newBuilder()
when (kind) { when (kind) {
is Kind.New -> { is Kind.New -> {
closedGroupControlMessage.type = DataMessage.ClosedGroupControlMessage.Type.NEW closedGroupControlMessage.type = NEW
closedGroupControlMessage.publicKey = kind.publicKey closedGroupControlMessage.publicKey = kind.publicKey
closedGroupControlMessage.name = kind.name closedGroupControlMessage.name = kind.name
closedGroupControlMessage.encryptionKeyPair = SignalServiceProtos.KeyPair.newBuilder().also { closedGroupControlMessage.encryptionKeyPair = SignalServiceProtos.KeyPair.newBuilder().also {
@ -147,24 +132,24 @@ class ClosedGroupControlMessage() : ControlMessage() {
closedGroupControlMessage.expirationTimer = kind.expirationTimer closedGroupControlMessage.expirationTimer = kind.expirationTimer
} }
is Kind.EncryptionKeyPair -> { is Kind.EncryptionKeyPair -> {
closedGroupControlMessage.type = DataMessage.ClosedGroupControlMessage.Type.ENCRYPTION_KEY_PAIR closedGroupControlMessage.type = ENCRYPTION_KEY_PAIR
closedGroupControlMessage.publicKey = kind.publicKey ?: ByteString.EMPTY closedGroupControlMessage.publicKey = kind.publicKey ?: ByteString.EMPTY
closedGroupControlMessage.addAllWrappers(kind.wrappers.map { it.toProto() }) closedGroupControlMessage.addAllWrappers(kind.wrappers.map { it.toProto() })
} }
is Kind.NameChange -> { is Kind.NameChange -> {
closedGroupControlMessage.type = DataMessage.ClosedGroupControlMessage.Type.NAME_CHANGE closedGroupControlMessage.type = NAME_CHANGE
closedGroupControlMessage.name = kind.name closedGroupControlMessage.name = kind.name
} }
is Kind.MembersAdded -> { is Kind.MembersAdded -> {
closedGroupControlMessage.type = DataMessage.ClosedGroupControlMessage.Type.MEMBERS_ADDED closedGroupControlMessage.type = MEMBERS_ADDED
closedGroupControlMessage.addAllMembers(kind.members) closedGroupControlMessage.addAllMembers(kind.members)
} }
is Kind.MembersRemoved -> { is Kind.MembersRemoved -> {
closedGroupControlMessage.type = DataMessage.ClosedGroupControlMessage.Type.MEMBERS_REMOVED closedGroupControlMessage.type = MEMBERS_REMOVED
closedGroupControlMessage.addAllMembers(kind.members) closedGroupControlMessage.addAllMembers(kind.members)
} }
is Kind.MemberLeft -> { is Kind.MemberLeft -> {
closedGroupControlMessage.type = DataMessage.ClosedGroupControlMessage.Type.MEMBER_LEFT closedGroupControlMessage.type = MEMBER_LEFT
} }
} }
return SignalServiceProtos.Content.newBuilder().apply { return SignalServiceProtos.Content.newBuilder().apply {