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

Message refactor and cleanup #360

Merged
merged 16 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.xmtp.android.example.extension.flowWhileShared
import org.xmtp.android.example.extension.stateFlow
import org.xmtp.android.example.pushnotifications.PushNotificationTokenManager
import org.xmtp.android.library.Conversation
import org.xmtp.android.library.DecodedMessage
import org.xmtp.android.library.libxmtp.Message
import org.xmtp.android.library.messages.Topic
import org.xmtp.android.library.push.Service

Expand Down Expand Up @@ -84,7 +84,7 @@ class MainViewModel : ViewModel() {
}

@WorkerThread
private fun fetchMostRecentMessage(conversation: Conversation): DecodedMessage? {
private fun fetchMostRecentMessage(conversation: Conversation): Message? {
return runBlocking { conversation.messages(limit = 1).firstOrNull() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import org.xmtp.android.example.ClientManager
import org.xmtp.android.example.extension.flowWhileShared
import org.xmtp.android.example.extension.stateFlow
import org.xmtp.android.library.Conversation
import org.xmtp.android.library.DecodedMessage
import org.xmtp.android.library.Group

class ConversationDetailViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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.Message
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 @@ -98,38 +99,38 @@ class ConversationsTest {
runBlocking { boClient.conversations.newGroup(listOf(caro.walletAddress)) }
val group2 =
runBlocking { boClient.conversations.newGroup(listOf(caro.walletAddress)) }
runBlocking { dm.send("Howdy") }
runBlocking { group2.send("Howdy") }
val dmMessage = runBlocking { dm.send("Howdy") }
val groupMessage = runBlocking { group2.send("Howdy") }
runBlocking { boClient.conversations.syncAllConversations() }
val conversations = runBlocking { boClient.conversations.list() }
val conversationsOrdered =
runBlocking { boClient.conversations.list(order = Conversations.ConversationOrder.LAST_MESSAGE) }
assertEquals(conversations.size, 3)
assertEquals(conversationsOrdered.size, 3)
assertEquals(conversations.map { it.id }, listOf(dm.id, group1.id, group2.id))
assertEquals(conversationsOrdered.map { it.id }, listOf(group2.id, dm.id, group1.id))
assertEquals(conversations.map { it.id }, listOf(group2.id, dm.id, group1.id))
runBlocking {
assertEquals(group2.lastMessage()!!.id, groupMessage)
assertEquals(dm.lastMessage()!!.id, dmMessage)
}
}

@Test
fun testsCanSyncAllConversationsFiltered() {
runBlocking { boClient.conversations.findOrCreateDm(caro.walletAddress) }
val group =
runBlocking { boClient.conversations.newGroup(listOf(caro.walletAddress)) }
assertEquals(runBlocking { boClient.conversations.syncAllConversations() }.toInt(), 3)
assertEquals(
runBlocking { boClient.conversations.syncAllConversations(consentState = ConsentState.ALLOWED) }.toInt(),
2
assert(runBlocking { boClient.conversations.syncAllConversations() }.toInt() >= 2)
assert(
runBlocking { boClient.conversations.syncAllConversations(consentState = ConsentState.ALLOWED) }.toInt() >= 2
)
assert(
runBlocking { boClient.conversations.syncAllConversations(consentState = ConsentState.DENIED) }.toInt() <= 1
)
runBlocking { group.updateConsentState(ConsentState.DENIED) }
assertEquals(
runBlocking { boClient.conversations.syncAllConversations(consentState = ConsentState.ALLOWED) }.toInt(),
1
assert(
runBlocking { boClient.conversations.syncAllConversations(consentState = ConsentState.ALLOWED) }.toInt() <= 2
)
assertEquals(
runBlocking { boClient.conversations.syncAllConversations(consentState = ConsentState.DENIED) }.toInt(),
1
assert(
runBlocking { boClient.conversations.syncAllConversations(consentState = ConsentState.DENIED) }.toInt() <= 2
)
assertEquals(runBlocking { boClient.conversations.syncAllConversations() }.toInt(), 2)
assert(runBlocking { boClient.conversations.syncAllConversations() }.toInt() >= 2)
}

@Test
Expand All @@ -140,7 +141,7 @@ class ConversationsTest {
runBlocking { boClient.conversations.findOrCreateDm(caro.walletAddress) }
runBlocking { boClient.conversations.sync() }

val allMessages = mutableListOf<DecodedMessage>()
val allMessages = mutableListOf<Message>()

val job = CoroutineScope(Dispatchers.IO).launch {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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.Message
import org.xmtp.android.library.libxmtp.Message.MessageDeliveryStatus
import org.xmtp.android.library.messages.PrivateKey
import org.xmtp.android.library.messages.PrivateKeyBuilder
Expand Down Expand Up @@ -181,12 +182,8 @@ class DmTest {
runBlocking { group.send("Howdy") }
runBlocking { boClient.conversations.syncAllConversations() }
val conversations = runBlocking { boClient.conversations.listDms() }
val conversationsOrdered =
runBlocking { boClient.conversations.listDms(order = Conversations.ConversationOrder.LAST_MESSAGE) }
assertEquals(conversations.size, 2)
assertEquals(conversationsOrdered.size, 2)
assertEquals(conversations.map { it.id }, listOf(dm1.id, dm2.id))
assertEquals(conversationsOrdered.map { it.id }, listOf(dm2.id, dm1.id))
}

@Test
Expand Down Expand Up @@ -294,7 +291,7 @@ class DmTest {
val boDm = runBlocking { boClient.conversations.findOrCreateDm(alix.walletAddress) }
runBlocking { alixClient.conversations.sync() }

val allMessages = mutableListOf<DecodedMessage>()
val allMessages = mutableListOf<Message>()

val job = CoroutineScope(Dispatchers.IO).launch {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import org.junit.Assert.assertThrows
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.xmtp.android.library.libxmtp.GroupPermissionPreconfiguration
import org.xmtp.android.library.libxmtp.PermissionLevel
import org.xmtp.android.library.libxmtp.PermissionOption
import org.xmtp.android.library.libxmtp.PermissionPolicySet
import org.xmtp.android.library.messages.PrivateKey
import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.walletAddress
import uniffi.xmtpv3.GenericException
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.GroupPermissionPreconfiguration
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionOption
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionPolicySet
import java.security.SecureRandom

@RunWith(AndroidJUnit4::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ 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.GroupPermissionPreconfiguration
import org.xmtp.android.library.libxmtp.Message
import org.xmtp.android.library.libxmtp.Message.MessageDeliveryStatus
import org.xmtp.android.library.libxmtp.PermissionOption
import org.xmtp.android.library.messages.PrivateKey
import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.mls.message.contents.TranscriptMessages
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.GroupPermissionPreconfiguration
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionOption

@RunWith(AndroidJUnit4::class)
class GroupTest {
Expand Down Expand Up @@ -325,7 +326,7 @@ class GroupTest {
runBlocking { alixGroup.sync() }
val message3 = runBlocking { alixGroup.messages().last() }
assertEquals(message3.id, message2.id)
assertEquals(message3.sent.time, message2.sent.time)
assertEquals(message3.sentAtNs, message2.sentAtNs)
}

@Test
Expand Down Expand Up @@ -513,12 +514,8 @@ class GroupTest {
runBlocking { group2.send("Howdy") }
runBlocking { boClient.conversations.syncAllConversations() }
val conversations = runBlocking { boClient.conversations.listGroups() }
val conversationsOrdered =
runBlocking { boClient.conversations.listGroups(order = Conversations.ConversationOrder.LAST_MESSAGE) }
assertEquals(conversations.size, 2)
assertEquals(conversationsOrdered.size, 2)
assertEquals(conversations.map { it.id }, listOf(group1.id, group2.id))
assertEquals(conversationsOrdered.map { it.id }, listOf(group2.id, group1.id))
}

@Test
Expand Down Expand Up @@ -660,7 +657,7 @@ class GroupTest {

runBlocking { alixClient.conversations.sync() }

val allMessages = mutableListOf<DecodedMessage>()
val allMessages = mutableListOf<Message>()

val job = CoroutineScope(Dispatchers.IO).launch {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class SmartContractWalletTest {
val dm2 = runBlocking { boEOAClient.conversations.findOrCreateDm(davonSCW.walletAddress) }
runBlocking { davonSCWClient.conversations.sync() }

val allMessages = mutableListOf<DecodedMessage>()
val allMessages = mutableListOf<Message>()

val job = CoroutineScope(Dispatchers.IO).launch {
try {
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/java/libxmtp-version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 7d863bde
Version: ab9bb055
Branch: main
Date: 2024-12-20 22:46:38 +0000
Date: 2025-01-07 00:08:33 +0000
4 changes: 2 additions & 2 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import kotlinx.coroutines.runBlocking
import org.xmtp.android.library.codecs.ContentCodec
import org.xmtp.android.library.codecs.TextCodec
import org.xmtp.android.library.libxmtp.InboxState
import org.xmtp.android.library.libxmtp.Message
import org.xmtp.android.library.messages.rawData
import uniffi.xmtpv3.FfiConversationType
Expand All @@ -16,7 +17,6 @@ import uniffi.xmtpv3.createClient
import uniffi.xmtpv3.generateInboxId
import uniffi.xmtpv3.getInboxIdForAddress
import uniffi.xmtpv3.getVersionInfo
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.InboxState
import java.io.File

typealias PreEventCallback = suspend () -> Unit
Expand Down Expand Up @@ -356,7 +356,7 @@ class Client() {

fun findMessage(messageId: String): Message? {
return try {
Message(this, ffiClient.message(messageId.hexToByteArray()))
Message.create(ffiClient.message(messageId.hexToByteArray()))
} catch (e: Exception) {
null
}
Expand Down
13 changes: 10 additions & 3 deletions library/src/main/java/org/xmtp/android/library/Conversation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ sealed class Conversation {
}
}

suspend fun lastMessage(): Message? {
return when (this) {
is Group -> group.lastMessage()
is Dm -> dm.lastMessage()
}
}

suspend fun members(): List<Member> {
return when (this) {
is Group -> group.members()
Expand Down Expand Up @@ -113,14 +120,14 @@ sealed class Conversation {
afterNs: Long? = null,
direction: Message.SortDirection = Message.SortDirection.DESCENDING,
deliveryStatus: Message.MessageDeliveryStatus = Message.MessageDeliveryStatus.ALL,
): List<DecodedMessage> {
): List<Message> {
return when (this) {
is Group -> group.messages(limit, beforeNs, afterNs, direction, deliveryStatus)
is Dm -> dm.messages(limit, beforeNs, afterNs, direction, deliveryStatus)
}
}

suspend fun processMessage(messageBytes: ByteArray): Message {
suspend fun processMessage(messageBytes: ByteArray): Message? {
return when (this) {
is Group -> group.processMessage(messageBytes)
is Dm -> dm.processMessage(messageBytes)
Expand All @@ -142,7 +149,7 @@ sealed class Conversation {
}
}

fun streamMessages(): Flow<DecodedMessage> {
fun streamMessages(): Flow<Message> {
return when (this) {
is Group -> group.streamMessages()
is Dm -> dm.streamMessages()
Expand Down
Loading
Loading