From db38dca93b50039a4fc8a8083da4b5e74a8f0b4d Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 11 Apr 2024 14:30:40 -0700 Subject: [PATCH] complete remove the composite codec (#221) --- .../org/xmtp/android/library/CodecTest.kt | 54 ---------- .../xmtp/android/library/codecs/Composite.kt | 99 ------------------- .../library/codecs/DecodedComposite.kt | 9 -- 3 files changed, 162 deletions(-) delete mode 100644 library/src/main/java/org/xmtp/android/library/codecs/Composite.kt delete mode 100644 library/src/main/java/org/xmtp/android/library/codecs/DecodedComposite.kt diff --git a/library/src/androidTest/java/org/xmtp/android/library/CodecTest.kt b/library/src/androidTest/java/org/xmtp/android/library/CodecTest.kt index 4b0234130..d977d64ae 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/CodecTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/CodecTest.kt @@ -8,11 +8,9 @@ import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith import org.xmtp.android.library.Crypto.Companion.verifyHmacSignature -import org.xmtp.android.library.codecs.CompositeCodec import org.xmtp.android.library.codecs.ContentCodec import org.xmtp.android.library.codecs.ContentTypeId import org.xmtp.android.library.codecs.ContentTypeIdBuilder -import org.xmtp.android.library.codecs.DecodedComposite import org.xmtp.android.library.codecs.EncodedContent import org.xmtp.android.library.codecs.TextCodec import org.xmtp.android.library.messages.InvitationV1ContextBuilder @@ -77,58 +75,6 @@ class CodecTest { } } - @Test - fun testCompositeCodecOnePart() { - Client.register(codec = CompositeCodec()) - val fixtures = fixtures() - val aliceClient = fixtures.aliceClient - val aliceConversation = runBlocking { - aliceClient.conversations.newConversation(fixtures.bob.walletAddress) - } - val textContent = TextCodec().encode(content = "hiya") - val source = DecodedComposite(encodedContent = textContent) - runBlocking { - aliceConversation.send( - content = source, - options = SendOptions(contentType = CompositeCodec().contentType), - ) - } - val messages = runBlocking { aliceConversation.messages() } - val decoded: DecodedComposite? = messages[0].content() - assertEquals("hiya", decoded?.content()) - } - - @Test - fun testCompositeCodecCanHaveParts() { - Client.register(codec = CompositeCodec()) - Client.register(codec = NumberCodec()) - val fixtures = fixtures() - val aliceClient = fixtures.aliceClient!! - val aliceConversation = runBlocking { - aliceClient.conversations.newConversation(fixtures.bob.walletAddress) - } - val textContent = TextCodec().encode(content = "sup") - val numberContent = NumberCodec().encode(content = 3.14) - val source = DecodedComposite( - parts = listOf( - DecodedComposite(encodedContent = textContent), - DecodedComposite(parts = listOf(DecodedComposite(encodedContent = numberContent))), - ), - ) - runBlocking { - aliceConversation.send( - content = source, - options = SendOptions(contentType = CompositeCodec().contentType), - ) - } - val messages = runBlocking { aliceConversation.messages() } - val decoded: DecodedComposite? = messages[0].content() - val part1 = decoded!!.parts[0] - val part2 = decoded.parts[1].parts[0] - assertEquals("sup", part1.content()) - assertEquals(3.14, part2.content()) - } - @Test fun testCanGetPushInfoBeforeDecoded() { val codec = NumberCodec() diff --git a/library/src/main/java/org/xmtp/android/library/codecs/Composite.kt b/library/src/main/java/org/xmtp/android/library/codecs/Composite.kt deleted file mode 100644 index da233e5bc..000000000 --- a/library/src/main/java/org/xmtp/android/library/codecs/Composite.kt +++ /dev/null @@ -1,99 +0,0 @@ -package org.xmtp.android.library.codecs - -import org.xmtp.proto.message.contents.CompositeOuterClass -import org.xmtp.proto.message.contents.CompositeOuterClass.Composite.Part - -typealias Composite = org.xmtp.proto.message.contents.CompositeOuterClass.Composite - -val ContentTypeComposite = ContentTypeIdBuilder.builderFromAuthorityId( - authorityId = "xmtp.org", - typeId = "composite", - versionMajor = 1, - versionMinor = 0 -) - -class CompositePartBuilder { - companion object { - fun buildFromEncodedContent(encodedContent: EncodedContent): CompositeOuterClass.Composite.Part { - return CompositeOuterClass.Composite.Part.newBuilder().also { - it.part = encodedContent - }.build() - } - - fun buildFromComosite(composite: Composite): CompositeOuterClass.Composite.Part { - return CompositeOuterClass.Composite.Part.newBuilder().also { - it.composite = composite - }.build() - } - } -} - -@Deprecated( - message = "This content type will be removed in future versions (see here for more info -> https://community.xmtp.org/t/xip-19-deprecate-the-composite-codec/525). Consider using a custom content type (https://github.com/xmtp/xmtp-android?tab=readme-ov-file#handle-custom-content-types) instead" -) -class CompositeCodec : ContentCodec { - override val contentType: ContentTypeId - get() = ContentTypeComposite - - override fun encode(content: DecodedComposite): EncodedContent { - val composite = toComposite(content) - return EncodedContent.newBuilder().also { - it.type = ContentTypeComposite - it.content = composite.toByteString() - }.build() - } - - override fun decode(content: EncodedContent): DecodedComposite { - val composite = Composite.parseFrom(content.content) - return fromComposite(composite = composite) - } - - override fun fallback(content: DecodedComposite): String? { - return null - } - - override fun shouldPush(content: DecodedComposite): Boolean = false - - private fun toComposite(decodedComposite: DecodedComposite): Composite { - return Composite.newBuilder().also { - val content = decodedComposite.encodedContent - if (content != null) { - it.addParts(CompositePartBuilder.buildFromEncodedContent(content)) - return it.build() - } - for (part in decodedComposite.parts) { - val encodedContent = part.encodedContent - if (encodedContent != null) { - it.addParts((CompositePartBuilder.buildFromEncodedContent(encodedContent))) - } else { - it.addParts((CompositePartBuilder.buildFromComosite(toComposite(part)))) - } - } - }.build() - } - - private fun fromComposite(composite: Composite): DecodedComposite { - val decodedComposite = DecodedComposite() - - if (composite.partsList.size == 1 && composite.partsList.first().elementCase == Part.ElementCase.PART) { - decodedComposite.encodedContent = composite.partsList.first().part - return decodedComposite - } - decodedComposite.parts = composite.partsList.map { fromCompositePart(part = it) } - return decodedComposite - } - - private fun fromCompositePart(part: Part): DecodedComposite { - return when (part.elementCase) { - Part.ElementCase.PART -> { - DecodedComposite(emptyList(), part.part) - } - - Part.ElementCase.COMPOSITE -> { - DecodedComposite(part.composite.partsList.map { fromCompositePart(it) }) - } - - else -> DecodedComposite() - } - } -} diff --git a/library/src/main/java/org/xmtp/android/library/codecs/DecodedComposite.kt b/library/src/main/java/org/xmtp/android/library/codecs/DecodedComposite.kt deleted file mode 100644 index 9d5803e55..000000000 --- a/library/src/main/java/org/xmtp/android/library/codecs/DecodedComposite.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.xmtp.android.library.codecs - -data class DecodedComposite( - var parts: List = listOf(), - var encodedContent: EncodedContent? = null -) { - fun content(): T? = - encodedContent?.decoded() -}