Cleanup closedGroupMessage serialisation

This commit is contained in:
Andrew 2024-02-17 16:30:39 +10:30
parent 54b7e116ca
commit 7c4060a8c4
4 changed files with 19 additions and 23 deletions

View File

@ -53,8 +53,8 @@ abstract class Message {
abstract fun toProto(): SignalServiceProtos.Content?
fun setGroupContext(dataMessage: SignalServiceProtos.DataMessage.Builder) {
dataMessage.group = SignalServiceProtos.GroupContext.newBuilder().apply {
fun SignalServiceProtos.DataMessage.Builder.setGroupContext() {
group = SignalServiceProtos.GroupContext.newBuilder().apply {
id = GroupUtil.doubleEncodeGroupID(recipient!!).let(GroupUtil::getDecodedGroupIDAsData).let(ByteString::copyFrom)
type = SignalServiceProtos.GroupContext.Type.DELIVER
}.build()

View File

@ -141,10 +141,10 @@ class ClosedGroupControlMessage() : ControlMessage() {
closedGroupControlMessage.type = DataMessage.ClosedGroupControlMessage.Type.NEW
closedGroupControlMessage.publicKey = kind.publicKey
closedGroupControlMessage.name = kind.name
val encryptionKeyPair = SignalServiceProtos.KeyPair.newBuilder()
encryptionKeyPair.publicKey = ByteString.copyFrom(kind.encryptionKeyPair!!.publicKey.serialize().removingIdPrefixIfNeeded())
encryptionKeyPair.privateKey = ByteString.copyFrom(kind.encryptionKeyPair!!.privateKey.serialize())
closedGroupControlMessage.encryptionKeyPair = encryptionKeyPair.build()
closedGroupControlMessage.encryptionKeyPair = SignalServiceProtos.KeyPair.newBuilder().also {
it.publicKey = ByteString.copyFrom(kind.encryptionKeyPair!!.publicKey.serialize().removingIdPrefixIfNeeded())
it.privateKey = ByteString.copyFrom(kind.encryptionKeyPair!!.privateKey.serialize())
}.build()
closedGroupControlMessage.addAllMembers(kind.members)
closedGroupControlMessage.addAllAdmins(kind.admins)
closedGroupControlMessage.expirationTimer = kind.expirationTimer
@ -170,16 +170,14 @@ class ClosedGroupControlMessage() : ControlMessage() {
closedGroupControlMessage.type = DataMessage.ClosedGroupControlMessage.Type.MEMBER_LEFT
}
}
val contentProto = SignalServiceProtos.Content.newBuilder()
val dataMessageProto = DataMessage.newBuilder()
dataMessageProto.closedGroupControlMessage = closedGroupControlMessage.build()
// Group context
setGroupContext(dataMessageProto)
contentProto.dataMessage = dataMessageProto.build()
// Expiration timer
val threadId = groupID?.let { MessagingModuleConfiguration.shared.storage.getOrCreateThreadIdFor(Address.fromSerialized(it)) }
contentProto.applyExpiryMode()
return contentProto.build()
return SignalServiceProtos.Content.newBuilder().apply {
dataMessage = DataMessage.newBuilder().also {
it.closedGroupControlMessage = closedGroupControlMessage.build()
it.setGroupContext()
}.build()
// Expiration timer
applyExpiryMode()
}.build()
} catch (e: Exception) {
Log.w(TAG, "Couldn't construct closed group control message proto from: $this.")
return null
@ -200,11 +198,9 @@ class ClosedGroupControlMessage() : ControlMessage() {
}
fun toProto(): DataMessage.ClosedGroupControlMessage.KeyPairWrapper? {
val publicKey = publicKey ?: return null
val encryptedKeyPair = encryptedKeyPair ?: return null
val result = DataMessage.ClosedGroupControlMessage.KeyPairWrapper.newBuilder()
result.publicKey = ByteString.copyFrom(Hex.fromStringCondensed(publicKey))
result.encryptedKeyPair = encryptedKeyPair
result.publicKey = ByteString.copyFrom(Hex.fromStringCondensed(publicKey ?: return null))
result.encryptedKeyPair = encryptedKeyPair ?: return null
return try {
result.build()
} catch (e: Exception) {

View File

@ -33,7 +33,7 @@ data class ExpirationTimerUpdate(var syncTarget: String? = null, val isGroup: Bo
// Group context
if (storage.isClosedGroup(recipient!!)) {
try {
setGroupContext(dataMessageProto)
dataMessageProto.setGroupContext()
} catch(e: Exception) {
Log.w(TAG, "Couldn't construct visible message proto from: $this", e)
return null

View File

@ -103,7 +103,7 @@ data class VisibleMessage(
val storage = MessagingModuleConfiguration.shared.storage
if (storage.isClosedGroup(recipient!!)) {
try {
setGroupContext(dataMessage)
dataMessage.setGroupContext()
} catch (e: Exception) {
Log.w(TAG, "Couldn't construct visible message proto from: $this")
return null