Skip to content

Commit

Permalink
Remove client from serializable objects (#365)
Browse files Browse the repository at this point in the history
* remove client from serializable objects

* dont require context for static methods
  • Loading branch information
nplasterer authored Jan 14, 2025
1 parent b34ebb9 commit 4b9d152
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class ClientTest {
notOnNetwork.address,
fixtures.bo.walletAddress
),
context,
ClientOptions.Api(XMTPEnvironment.LOCAL, false)
)
}
Expand All @@ -109,7 +108,6 @@ class ClientTest {
val states = runBlocking {
Client.inboxStatesForInboxIds(
listOf(fixtures.boClient.inboxId, fixtures.caroClient.inboxId),
context,
ClientOptions.Api(XMTPEnvironment.LOCAL, false)
)
}
Expand Down
35 changes: 11 additions & 24 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,16 @@ class Client() {
codecRegistry.register(codec = codec)
}

suspend fun <T> withFfiClient(
appContext: Context,
private suspend fun <T> withFfiClient(
api: ClientOptions.Api,
useClient: suspend (ffiClient: FfiXmtpClient) -> T,
): T {
val accountAddress = "0x0000000000000000000000000000000000000000"
val inboxId = getOrCreateInboxId(api, accountAddress)
val alias = "xmtp-${api.env}-$inboxId"

val directoryFile = File(appContext.filesDir.absolutePath, "xmtp_db")
directoryFile.mkdir()
val dbPath = directoryFile.absolutePath + "/$alias.db3"

val ffiClient = createClient(
api = connectToApiBackend(api),
db = dbPath,
db = null,
encryptionKey = null,
accountAddress = accountAddress.lowercase(),
inboxId = inboxId,
Expand All @@ -115,30 +109,23 @@ class Client() {
historySyncUrl = null
)

return try {
useClient(ffiClient)
} finally {
ffiClient.releaseDbConnection()
File(dbPath).delete()
}
return useClient(ffiClient)
}

suspend fun inboxStatesForInboxIds(
inboxIds: List<String>,
appContext: Context,
api: ClientOptions.Api,
): List<InboxState> {
return withFfiClient(appContext, api) { ffiClient ->
return withFfiClient(api) { ffiClient ->
ffiClient.addressesFromInboxId(true, inboxIds).map { InboxState(it) }
}
}

suspend fun canMessage(
accountAddresses: List<String>,
appContext: Context,
api: ClientOptions.Api,
): Map<String, Boolean> {
return withFfiClient(appContext, api) { ffiClient ->
return withFfiClient(api) { ffiClient ->
ffiClient.canMessage(accountAddresses)
}
}
Expand Down Expand Up @@ -332,7 +319,7 @@ class Client() {

fun findGroup(groupId: String): Group? {
return try {
Group(this, ffiClient.conversation(groupId.hexToByteArray()))
Group(this.inboxId, ffiClient.conversation(groupId.hexToByteArray()))
} catch (e: Exception) {
null
}
Expand All @@ -342,8 +329,8 @@ class Client() {
return try {
val conversation = ffiClient.conversation(conversationId.hexToByteArray())
when (conversation.conversationType()) {
FfiConversationType.GROUP -> Conversation.Group(Group(this, conversation))
FfiConversationType.DM -> Conversation.Dm(Dm(this, conversation))
FfiConversationType.GROUP -> Conversation.Group(Group(this.inboxId, conversation))
FfiConversationType.DM -> Conversation.Dm(Dm(this.inboxId, conversation))
else -> null
}
} catch (e: Exception) {
Expand All @@ -358,8 +345,8 @@ class Client() {
return try {
val conversation = ffiClient.conversation(conversationId.hexToByteArray())
when (conversation.conversationType()) {
FfiConversationType.GROUP -> Conversation.Group(Group(this, conversation))
FfiConversationType.DM -> Conversation.Dm(Dm(this, conversation))
FfiConversationType.GROUP -> Conversation.Group(Group(this.inboxId, conversation))
FfiConversationType.DM -> Conversation.Dm(Dm(this.inboxId, conversation))
else -> null
}
} catch (e: Exception) {
Expand All @@ -369,7 +356,7 @@ class Client() {

fun findDmByInboxId(inboxId: String): Dm? {
return try {
Dm(this, ffiClient.dmConversation(inboxId))
Dm(this.inboxId, ffiClient.dmConversation(inboxId))
} catch (e: Exception) {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,6 @@ sealed class Conversation {
}
}

val client: Client
get() {
return when (this) {
is Group -> group.client
is Dm -> dm.client
}
}

fun streamMessages(): Flow<Message> {
return when (this) {
is Group -> group.streamMessages()
Expand Down
20 changes: 10 additions & 10 deletions library/src/main/java/org/xmtp/android/library/Conversations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ data class Conversations(
suspend fun fromWelcome(envelopeBytes: ByteArray): Conversation {
val conversation = ffiConversations.processStreamedWelcomeMessage(envelopeBytes)
return when (conversation.conversationType()) {
FfiConversationType.DM -> Conversation.Dm(Dm(client, conversation))
else -> Conversation.Group(Group(client, conversation))
FfiConversationType.DM -> Conversation.Dm(Dm(client.inboxId, conversation))
else -> Conversation.Group(Group(client.inboxId, conversation))
}
}

Expand Down Expand Up @@ -118,7 +118,7 @@ data class Conversations(
customPermissionPolicySet = permissionsPolicySet
)
)
return Group(client, group)
return Group(client.inboxId, group)
}

// Sync from the network the latest list of conversations
Expand Down Expand Up @@ -152,7 +152,7 @@ data class Conversations(
var dm = client.findDmByAddress(peerAddress)
if (dm == null) {
val dmConversation = ffiConversations.createDm(peerAddress.lowercase())
dm = Dm(client, dmConversation)
dm = Dm(client.inboxId, dmConversation)
}
return dm
}
Expand All @@ -174,7 +174,7 @@ data class Conversations(
)

return ffiGroups.map {
Group(client, it.conversation(), it.lastMessage())
Group(client.inboxId, it.conversation(), it.lastMessage())
}
}

Expand All @@ -195,7 +195,7 @@ data class Conversations(
)

return ffiDms.map {
Dm(client, it.conversation(), it.lastMessage())
Dm(client.inboxId, it.conversation(), it.lastMessage())
}
}

Expand All @@ -220,8 +220,8 @@ data class Conversations(

private suspend fun FfiConversationListItem.toConversation(): Conversation {
return when (conversation().conversationType()) {
FfiConversationType.DM -> Conversation.Dm(Dm(client, conversation(), lastMessage()))
else -> Conversation.Group(Group(client, conversation(), lastMessage()))
FfiConversationType.DM -> Conversation.Dm(Dm(client.inboxId, conversation(), lastMessage()))
else -> Conversation.Group(Group(client.inboxId, conversation(), lastMessage()))
}
}

Expand All @@ -234,13 +234,13 @@ data class Conversations(
FfiConversationType.DM -> trySend(
Conversation.Dm(
Dm(
client,
client.inboxId,
conversation
)
)
)

else -> trySend(Conversation.Group(Group(client, conversation)))
else -> trySend(Conversation.Group(Group(client.inboxId, conversation)))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/java/org/xmtp/android/library/Dm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import uniffi.xmtpv3.FfiMessageCallback
import uniffi.xmtpv3.FfiSubscribeException
import java.util.Date

class Dm(val client: Client, private val libXMTPGroup: FfiConversation, private val ffiLastMessage: FfiMessage? = null) {
class Dm(private val clientInboxId: String, private val libXMTPGroup: FfiConversation, private val ffiLastMessage: FfiMessage? = null) {
val id: String
get() = libXMTPGroup.id().toHex()

Expand Down Expand Up @@ -150,7 +150,7 @@ class Dm(val client: Client, private val libXMTPGroup: FfiConversation, private
}

suspend fun isCreator(): Boolean {
return metadata().creatorInboxId() == client.inboxId
return metadata().creatorInboxId() == clientInboxId
}

suspend fun members(): List<Member> {
Expand Down
6 changes: 3 additions & 3 deletions library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import uniffi.xmtpv3.FfiSubscribeException
import java.util.Date

class Group(
val client: Client,
private val clientInboxId: String,
private val libXMTPGroup: FfiConversation,
private val ffiLastMessage: FfiMessage? = null,
) {
Expand Down Expand Up @@ -193,7 +193,7 @@ class Group(
}

suspend fun isCreator(): Boolean {
return metadata().creatorInboxId() == client.inboxId
return metadata().creatorInboxId() == clientInboxId
}

suspend fun addMembers(addresses: List<String>) {
Expand Down Expand Up @@ -234,7 +234,7 @@ class Group(

suspend fun peerInboxIds(): List<String> {
val ids = members().map { it.inboxId }.toMutableList()
ids.remove(client.inboxId)
ids.remove(clientInboxId)
return ids
}

Expand Down

0 comments on commit 4b9d152

Please sign in to comment.