Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group Message Kind #217

Merged
merged 15 commits into from
Apr 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.xmtp.android.library.libxmtp.MessageV3
import org.xmtp.android.library.messages.PrivateKey
import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.walletAddress
Expand Down Expand Up @@ -69,6 +70,7 @@ class GroupMembershipChangeTest {
content?.membersAddedList?.map { it.accountAddress.lowercase() }?.sorted()
)
assert(content?.membersRemovedList.isNullOrEmpty())
assertEquals(messages.first().kind, MessageV3.MessageKind.MEMBERSHIP_CHANGE)
}

@Test
Expand Down Expand Up @@ -97,6 +99,7 @@ class GroupMembershipChangeTest {
content?.membersRemovedList?.map { it.accountAddress.lowercase() }?.sorted()
)
assert(content?.membersAddedList.isNullOrEmpty())
assertEquals(updatedMessages.first().kind, MessageV3.MessageKind.MEMBERSHIP_CHANGE)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.xmtp.android.library.codecs.Reaction
import org.xmtp.android.library.codecs.ReactionAction
import org.xmtp.android.library.codecs.ReactionCodec
import org.xmtp.android.library.codecs.ReactionSchema
import org.xmtp.android.library.libxmtp.MessageV3
import org.xmtp.android.library.messages.PrivateKey
import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.walletAddress
Expand Down Expand Up @@ -323,6 +324,7 @@ class GroupTest {
runBlocking { sameGroup.sync() }
assertEquals(sameGroup.messages().size, 2)
assertEquals(sameGroup.messages().first().body, "gm")
assertEquals(sameGroup.messages().first().kind, MessageV3.MessageKind.APPLICATION)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.util.Log
import com.google.protobuf.kotlin.toByteString
import kotlinx.coroutines.flow.Flow
import org.xmtp.android.library.codecs.EncodedContent
import org.xmtp.android.library.libxmtp.Message
import org.xmtp.android.library.libxmtp.MessageV3
import org.xmtp.android.library.messages.DecryptedMessage
import org.xmtp.android.library.messages.Envelope
import org.xmtp.android.library.messages.PagingInfoSortDirection
Expand Down Expand Up @@ -121,7 +121,7 @@ sealed class Conversation {
}
}

fun decode(envelope: Envelope, message: Message? = null): DecodedMessage {
fun decode(envelope: Envelope, message: MessageV3? = null): DecodedMessage {
return when (this) {
is V1 -> conversationV1.decode(envelope)
is V2 -> conversationV2.decodeEnvelope(envelope)
Expand Down Expand Up @@ -277,7 +277,7 @@ sealed class Conversation {

fun decrypt(
envelope: Envelope,
message: Message? = null,
message: MessageV3? = null,
): DecryptedMessage {
return when (this) {
is V1 -> conversationV1.decrypt(envelope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.merge
import org.xmtp.android.library.GRPCApiClient.Companion.makeQueryRequest
import org.xmtp.android.library.GRPCApiClient.Companion.makeSubscribeRequest
import org.xmtp.android.library.libxmtp.Message
import org.xmtp.android.library.libxmtp.MessageV3
import org.xmtp.android.library.messages.DecryptedMessage
import org.xmtp.android.library.messages.Envelope
import org.xmtp.android.library.messages.EnvelopeBuilder
Expand Down Expand Up @@ -576,7 +576,7 @@ data class Conversations(
fun streamAllGroupMessages(): Flow<DecodedMessage> = callbackFlow {
val messageCallback = object : FfiMessageCallback {
override fun onMessage(message: FfiMessage) {
trySend(Message(client, message).decode())
trySend(MessageV3(client, message).decode())
}
}
val stream = libXMTPConversations?.streamAllMessages(messageCallback)
Expand All @@ -587,7 +587,7 @@ data class Conversations(
fun streamAllGroupDecryptedMessages(): Flow<DecryptedMessage> = callbackFlow {
val messageCallback = object : FfiMessageCallback {
override fun onMessage(message: FfiMessage) {
trySend(Message(client, message).decrypt())
trySend(MessageV3(client, message).decrypt())
}
}
val stream = libXMTPConversations?.streamAllMessages(messageCallback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.xmtp.android.library

import org.xmtp.android.library.codecs.TextCodec
import org.xmtp.android.library.codecs.decoded
import org.xmtp.android.library.libxmtp.MessageV3
import org.xmtp.proto.message.contents.Content
import java.util.Date

Expand All @@ -11,7 +12,8 @@ data class DecodedMessage(
var topic: String,
var encodedContent: Content.EncodedContent,
var senderAddress: String,
var sent: Date
var sent: Date,
var kind: MessageV3.MessageKind = MessageV3.MessageKind.APPLICATION
) {
companion object {
fun preview(client: Client, topic: String, body: String, senderAddress: String, sent: Date): DecodedMessage {
Expand Down
14 changes: 7 additions & 7 deletions library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.callbackFlow
import org.xmtp.android.library.codecs.ContentCodec
import org.xmtp.android.library.codecs.EncodedContent
import org.xmtp.android.library.codecs.compress
import org.xmtp.android.library.libxmtp.Message
import org.xmtp.android.library.libxmtp.MessageV3
import org.xmtp.android.library.messages.DecryptedMessage
import org.xmtp.android.library.messages.PagingInfoSortDirection
import org.xmtp.android.library.messages.Topic
Expand Down Expand Up @@ -94,7 +94,7 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
limit = limit?.toLong()
)
).map {
Message(client, it).decode()
MessageV3(client, it).decode()
}
return when (direction) {
MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING -> messages
Expand All @@ -115,17 +115,17 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
limit = limit?.toLong()
)
).map {
Message(client, it).decrypt()
MessageV3(client, it).decrypt()
}
return when (direction) {
MessageApiOuterClass.SortDirection.SORT_DIRECTION_ASCENDING -> messages
else -> messages.reversed()
}
}

suspend fun processMessage(envelopeBytes: ByteArray): Message {
suspend fun processMessage(envelopeBytes: ByteArray): MessageV3 {
val message = libXMTPGroup.processStreamedGroupMessage(envelopeBytes)
return Message(client, message)
return MessageV3(client, message)
}

fun isActive(): Boolean {
Expand Down Expand Up @@ -173,7 +173,7 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
fun streamMessages(): Flow<DecodedMessage> = callbackFlow {
val messageCallback = object : FfiMessageCallback {
override fun onMessage(message: FfiMessage) {
trySend(Message(client, message).decode())
trySend(MessageV3(client, message).decode())
}
}

Expand All @@ -184,7 +184,7 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
fun streamDecryptedMessages(): Flow<DecryptedMessage> = callbackFlow {
val messageCallback = object : FfiMessageCallback {
override fun onMessage(message: FfiMessage) {
trySend(Message(client, message).decrypt())
trySend(MessageV3(client, message).decrypt())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ import org.xmtp.android.library.codecs.EncodedContent
import org.xmtp.android.library.messages.DecryptedMessage
import org.xmtp.android.library.messages.Topic
import org.xmtp.android.library.toHex
import uniffi.xmtpv3.FfiGroupMessageKind
import uniffi.xmtpv3.FfiMessage
import java.util.Date

data class Message(val client: Client, private val libXMTPMessage: FfiMessage) {
data class MessageV3(val client: Client, private val libXMTPMessage: FfiMessage) {

enum class MessageKind {
APPLICATION, MEMBERSHIP_CHANGE
}
val id: ByteArray
get() = libXMTPMessage.id

Expand All @@ -23,6 +28,12 @@ data class Message(val client: Client, private val libXMTPMessage: FfiMessage) {
val sentAt: Date
get() = Date(libXMTPMessage.sentAtNs / 1_000_000)

val kind: MessageKind
get() = when (libXMTPMessage.kind) {
FfiGroupMessageKind.APPLICATION -> MessageKind.APPLICATION
FfiGroupMessageKind.MEMBERSHIP_CHANGE -> MessageKind.MEMBERSHIP_CHANGE
}

fun decode(): DecodedMessage {
try {
return DecodedMessage(
Expand All @@ -31,7 +42,8 @@ data class Message(val client: Client, private val libXMTPMessage: FfiMessage) {
topic = Topic.groupMessage(convoId.toHex()).description,
encodedContent = EncodedContent.parseFrom(libXMTPMessage.content),
senderAddress = senderAddress,
sent = sentAt
sent = sentAt,
kind = kind
)
} catch (e: Exception) {
throw XMTPException("Error decoding message", e)
Expand All @@ -44,7 +56,8 @@ data class Message(val client: Client, private val libXMTPMessage: FfiMessage) {
topic = Topic.groupMessage(convoId.toHex()).description,
encodedContent = decode().encodedContent,
senderAddress = senderAddress,
sentAt = Date()
sentAt = Date(),
kind = kind
)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.xmtp.android.library.messages

import org.xmtp.android.library.codecs.EncodedContent
import org.xmtp.android.library.libxmtp.MessageV3
import java.util.Date

data class DecryptedMessage(
var id: String,
var encodedContent: EncodedContent,
var senderAddress: String,
var sentAt: Date,
var topic: String = ""
var topic: String = "",
var kind: MessageV3.MessageKind = MessageV3.MessageKind.APPLICATION
)
32 changes: 30 additions & 2 deletions library/src/main/java/xmtpv3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2500,7 +2500,8 @@ data class FfiMessage (
var `sentAtNs`: Long,
var `convoId`: ByteArray,
var `addrFrom`: String,
var `content`: ByteArray
var `content`: ByteArray,
var `kind`: FfiGroupMessageKind
) {

companion object
Expand All @@ -2514,6 +2515,7 @@ public object FfiConverterTypeFfiMessage: FfiConverterRustBuffer<FfiMessage> {
FfiConverterByteArray.read(buf),
FfiConverterString.read(buf),
FfiConverterByteArray.read(buf),
FfiConverterTypeFfiGroupMessageKind.read(buf),
)
}

Expand All @@ -2522,7 +2524,8 @@ public object FfiConverterTypeFfiMessage: FfiConverterRustBuffer<FfiMessage> {
FfiConverterLong.allocationSize(value.`sentAtNs`) +
FfiConverterByteArray.allocationSize(value.`convoId`) +
FfiConverterString.allocationSize(value.`addrFrom`) +
FfiConverterByteArray.allocationSize(value.`content`)
FfiConverterByteArray.allocationSize(value.`content`) +
FfiConverterTypeFfiGroupMessageKind.allocationSize(value.`kind`)
)

override fun write(value: FfiMessage, buf: ByteBuffer) {
Expand All @@ -2531,6 +2534,7 @@ public object FfiConverterTypeFfiMessage: FfiConverterRustBuffer<FfiMessage> {
FfiConverterByteArray.write(value.`convoId`, buf)
FfiConverterString.write(value.`addrFrom`, buf)
FfiConverterByteArray.write(value.`content`, buf)
FfiConverterTypeFfiGroupMessageKind.write(value.`kind`, buf)
}
}

Expand Down Expand Up @@ -2743,6 +2747,30 @@ public object FfiConverterTypeFfiV2SubscribeRequest: FfiConverterRustBuffer<FfiV



enum class FfiGroupMessageKind {
APPLICATION,MEMBERSHIP_CHANGE;
companion object
}

public object FfiConverterTypeFfiGroupMessageKind: FfiConverterRustBuffer<FfiGroupMessageKind> {
override fun read(buf: ByteBuffer) = try {
FfiGroupMessageKind.values()[buf.getInt() - 1]
} catch (e: IndexOutOfBoundsException) {
throw RuntimeException("invalid enum value, something is very wrong!!", e)
}

override fun allocationSize(value: FfiGroupMessageKind) = 4

override fun write(value: FfiGroupMessageKind, buf: ByteBuffer) {
buf.putInt(value.ordinal + 1)
}
}






enum class FfiSortDirection {
UNSPECIFIED,ASCENDING,DESCENDING;
companion object
Expand Down
Binary file modified library/src/main/jniLibs/arm64-v8a/libuniffi_xmtpv3.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/armeabi-v7a/libuniffi_xmtpv3.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/x86/libuniffi_xmtpv3.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/x86_64/libuniffi_xmtpv3.so
Binary file not shown.
Loading