File tree 12 files changed +50
-7
lines changed
androidTest/java/org/xmtp/android/library
main/java/org/xmtp/android/library
12 files changed +50
-7
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,10 @@ data class NumberCodec(
36
36
37
37
override fun decode (content : EncodedContent ): Double =
38
38
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
+ }
39
43
}
40
44
@RunWith(AndroidJUnit4 ::class )
41
45
class CodecTest {
@@ -56,6 +60,7 @@ class CodecTest {
56
60
if (messages.size == 1 ) {
57
61
val content: Double? = messages[0 ].content()
58
62
assertEquals(3.14 , content)
63
+ assertEquals(" Error: This app does not support numbers." , messages[0 ].fallbackContent)
59
64
}
60
65
}
61
66
Original file line number Diff line number Diff line change @@ -119,9 +119,13 @@ data class ConversationV1(
119
119
}
120
120
121
121
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
+ }
125
129
val compression = options?.compression
126
130
if (compression != null ) {
127
131
encoded = encoded.compress(compression)
Original file line number Diff line number Diff line change @@ -147,9 +147,12 @@ data class ConversationV2(
147
147
}
148
148
149
149
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
+ }
153
156
val compression = options?.compression
154
157
if (compression != null ) {
155
158
encoded = encoded.compress(compression)
Original file line number Diff line number Diff line change @@ -5,6 +5,5 @@ import org.xmtp.proto.message.contents.Content
5
5
data class SendOptions (
6
6
var compression : EncodedContentCompression ? = null ,
7
7
var contentType : Content .ContentTypeId ? = null ,
8
- var contentFallback : String? = null ,
9
8
var ephemeral : Boolean = false
10
9
)
Original file line number Diff line number Diff line change @@ -32,4 +32,8 @@ data class AttachmentCodec(override var contentType: ContentTypeId = ContentType
32
32
data = encodedContent,
33
33
)
34
34
}
35
+
36
+ override fun fallback (content : Attachment ): String? {
37
+ return " Can’t display \" ${content.filename} ”. This app doesn’t support attachments."
38
+ }
35
39
}
Original file line number Diff line number Diff line change @@ -48,6 +48,10 @@ class CompositeCodec : ContentCodec<DecodedComposite> {
48
48
return fromComposite(composite = composite)
49
49
}
50
50
51
+ override fun fallback (content : DecodedComposite ): String? {
52
+ return null
53
+ }
54
+
51
55
private fun toComposite (decodedComposite : DecodedComposite ): Composite {
52
56
return Composite .newBuilder().also {
53
57
val content = decodedComposite.encodedContent
Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ interface ContentCodec<T> {
66
66
val contentType: ContentTypeId
67
67
fun encode (content : T ): EncodedContent
68
68
fun decode (content : EncodedContent ): T
69
+ fun fallback (content : T ): String?
69
70
}
70
71
71
72
val id: String
Original file line number Diff line number Diff line change @@ -53,4 +53,11 @@ data class ReactionCodec(override var contentType: ContentTypeId = ContentTypeRe
53
53
content = text,
54
54
)
55
55
}
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
+ }
56
63
}
Original file line number Diff line number Diff line change @@ -31,4 +31,8 @@ data class ReadReceiptCodec(override var contentType: ContentTypeId = ContentTyp
31
31
32
32
return ReadReceipt (timestamp = timestamp)
33
33
}
34
+
35
+ override fun fallback (content : ReadReceipt ): String? {
36
+ return null
37
+ }
34
38
}
Original file line number Diff line number Diff line change @@ -167,4 +167,8 @@ data class RemoteAttachmentCodec(override var contentType: ContentTypeId = Conte
167
167
filename = filename,
168
168
)
169
169
}
170
+
171
+ override fun fallback (content : RemoteAttachment ): String? {
172
+ return " Can’t display \" ${content.filename} ”. This app doesn’t support attachments."
173
+ }
170
174
}
Original file line number Diff line number Diff line change @@ -45,6 +45,10 @@ data class ReplyCodec(override var contentType: ContentTypeId = ContentTypeReply
45
45
)
46
46
}
47
47
48
+ override fun fallback (content : Reply ): String? {
49
+ return " Replied with “${content.content} ” to an earlier message"
50
+ }
51
+
48
52
private fun <Codec : ContentCodec <T >, T > encodeReply (
49
53
codec : Codec ,
50
54
content : Any ,
Original file line number Diff line number Diff line change @@ -32,4 +32,8 @@ data class TextCodec(override var contentType: ContentTypeId = ContentTypeText)
32
32
throw XMTPException (" Unknown decoding" )
33
33
}
34
34
}
35
+
36
+ override fun fallback (content : String ): String? {
37
+ return null
38
+ }
35
39
}
You can’t perform that action at this time.
0 commit comments