Skip to content

Commit 6edea60

Browse files
authored
feat: Move fallback content definition to content codecs (#114)
* feat: add fallback to codec * add a test * update the copy to the approved copy
1 parent 0976446 commit 6edea60

File tree

12 files changed

+50
-7
lines changed

12 files changed

+50
-7
lines changed

library/src/androidTest/java/org/xmtp/android/library/CodecTest.kt

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ data class NumberCodec(
3636

3737
override fun decode(content: EncodedContent): Double =
3838
content.content.toStringUtf8().filter { it.isDigit() || it == '.' }.toDouble()
39+
40+
override fun fallback(content: Double): String? {
41+
return "Error: This app does not support numbers."
42+
}
3943
}
4044
@RunWith(AndroidJUnit4::class)
4145
class CodecTest {
@@ -56,6 +60,7 @@ class CodecTest {
5660
if (messages.size == 1) {
5761
val content: Double? = messages[0].content()
5862
assertEquals(3.14, content)
63+
assertEquals("Error: This app does not support numbers.", messages[0].fallbackContent)
5964
}
6065
}
6166

library/src/main/java/org/xmtp/android/library/ConversationV1.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,13 @@ data class ConversationV1(
119119
}
120120

121121
var encoded = encode(codec = codec as ContentCodec<T>, content = content)
122-
encoded = encoded.toBuilder().also {
123-
it.fallback = options?.contentFallback ?: ""
124-
}.build()
122+
123+
val fallback = codec.fallback(content)
124+
if (!fallback.isNullOrBlank()) {
125+
encoded = encoded.toBuilder().also {
126+
it.fallback = fallback
127+
}.build()
128+
}
125129
val compression = options?.compression
126130
if (compression != null) {
127131
encoded = encoded.compress(compression)

library/src/main/java/org/xmtp/android/library/ConversationV2.kt

+6-3
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@ data class ConversationV2(
147147
}
148148

149149
var encoded = encode(codec = codec as ContentCodec<T>, content = content)
150-
encoded = encoded.toBuilder().also {
151-
it.fallback = options?.contentFallback ?: ""
152-
}.build()
150+
val fallback = codec.fallback(content)
151+
if (!fallback.isNullOrBlank()) {
152+
encoded = encoded.toBuilder().also {
153+
it.fallback = fallback
154+
}.build()
155+
}
153156
val compression = options?.compression
154157
if (compression != null) {
155158
encoded = encoded.compress(compression)

library/src/main/java/org/xmtp/android/library/SendOptions.kt

-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ import org.xmtp.proto.message.contents.Content
55
data class SendOptions(
66
var compression: EncodedContentCompression? = null,
77
var contentType: Content.ContentTypeId? = null,
8-
var contentFallback: String? = null,
98
var ephemeral: Boolean = false
109
)

library/src/main/java/org/xmtp/android/library/codecs/AttachmentCodec.kt

+4
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ data class AttachmentCodec(override var contentType: ContentTypeId = ContentType
3232
data = encodedContent,
3333
)
3434
}
35+
36+
override fun fallback(content: Attachment): String? {
37+
return "Can’t display \"${content.filename}”. This app doesn’t support attachments."
38+
}
3539
}

library/src/main/java/org/xmtp/android/library/codecs/Composite.kt

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class CompositeCodec : ContentCodec<DecodedComposite> {
4848
return fromComposite(composite = composite)
4949
}
5050

51+
override fun fallback(content: DecodedComposite): String? {
52+
return null
53+
}
54+
5155
private fun toComposite(decodedComposite: DecodedComposite): Composite {
5256
return Composite.newBuilder().also {
5357
val content = decodedComposite.encodedContent

library/src/main/java/org/xmtp/android/library/codecs/ContentCodec.kt

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ interface ContentCodec<T> {
6666
val contentType: ContentTypeId
6767
fun encode(content: T): EncodedContent
6868
fun decode(content: EncodedContent): T
69+
fun fallback(content: T): String?
6970
}
7071

7172
val id: String

library/src/main/java/org/xmtp/android/library/codecs/ReactionCodec.kt

+7
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ data class ReactionCodec(override var contentType: ContentTypeId = ContentTypeRe
5353
content = text,
5454
)
5555
}
56+
57+
override fun fallback(content: Reaction): String? {
58+
return when (content.action) {
59+
ReactionAction.added -> "Reacted “${content.content}” to an earlier message"
60+
ReactionAction.removed -> "Removed “${content.content}” from an earlier message"
61+
}
62+
}
5663
}

library/src/main/java/org/xmtp/android/library/codecs/ReadReceiptCodec.kt

+4
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ data class ReadReceiptCodec(override var contentType: ContentTypeId = ContentTyp
3131

3232
return ReadReceipt(timestamp = timestamp)
3333
}
34+
35+
override fun fallback(content: ReadReceipt): String? {
36+
return null
37+
}
3438
}

library/src/main/java/org/xmtp/android/library/codecs/RemoteAttachmentCodec.kt

+4
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,8 @@ data class RemoteAttachmentCodec(override var contentType: ContentTypeId = Conte
167167
filename = filename,
168168
)
169169
}
170+
171+
override fun fallback(content: RemoteAttachment): String? {
172+
return "Can’t display \"${content.filename}”. This app doesn’t support attachments."
173+
}
170174
}

library/src/main/java/org/xmtp/android/library/codecs/ReplyCodec.kt

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ data class ReplyCodec(override var contentType: ContentTypeId = ContentTypeReply
4545
)
4646
}
4747

48+
override fun fallback(content: Reply): String? {
49+
return "Replied with “${content.content}” to an earlier message"
50+
}
51+
4852
private fun <Codec : ContentCodec<T>, T> encodeReply(
4953
codec: Codec,
5054
content: Any,

library/src/main/java/org/xmtp/android/library/codecs/TextCodec.kt

+4
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ data class TextCodec(override var contentType: ContentTypeId = ContentTypeText)
3232
throw XMTPException("Unknown decoding")
3333
}
3434
}
35+
36+
override fun fallback(content: String): String? {
37+
return null
38+
}
3539
}

0 commit comments

Comments
 (0)