Skip to content

Commit

Permalink
create new members class
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed May 22, 2024
1 parent d6b2a3d commit d2cb60b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 39 deletions.
40 changes: 20 additions & 20 deletions library/src/androidTest/java/org/xmtp/android/library/GroupTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@ class GroupTest {
alixGroup.addMembers(listOf(caro.walletAddress))
boGroup.sync()
}
assertEquals(alixGroup.memberInboxIds().size, 3)
assertEquals(boGroup.memberInboxIds().size, 3)
assertEquals(alixGroup.members().size, 3)
assertEquals(boGroup.members().size, 3)

runBlocking {
alixGroup.removeMembers(listOf(caro.walletAddress))
boGroup.sync()
}
assertEquals(alixGroup.memberInboxIds().size, 2)
assertEquals(boGroup.memberInboxIds().size, 2)
assertEquals(alixGroup.members().size, 2)
assertEquals(boGroup.members().size, 2)

runBlocking {
boGroup.addMembers(listOf(caro.walletAddress))
alixGroup.sync()
}
assertEquals(alixGroup.memberInboxIds().size, 3)
assertEquals(boGroup.memberInboxIds().size, 3)
assertEquals(alixGroup.members().size, 3)
assertEquals(boGroup.members().size, 3)

assertEquals(boGroup.permissionLevel(), GroupPermissions.EVERYONE_IS_ADMIN)
assertEquals(alixGroup.permissionLevel(), GroupPermissions.EVERYONE_IS_ADMIN)
Expand Down Expand Up @@ -120,28 +120,28 @@ class GroupTest {
boGroup.addMembers(listOf(caro.walletAddress))
alixGroup.sync()
}
assertEquals(alixGroup.memberInboxIds().size, 3)
assertEquals(boGroup.memberInboxIds().size, 3)
assertEquals(alixGroup.members().size, 3)
assertEquals(boGroup.members().size, 3)

assertThrows(XMTPException::class.java) {
runBlocking { alixGroup.removeMembers(listOf(caro.walletAddress)) }
}
runBlocking { boGroup.sync() }
assertEquals(alixGroup.memberInboxIds().size, 3)
assertEquals(boGroup.memberInboxIds().size, 3)
assertEquals(alixGroup.members().size, 3)
assertEquals(boGroup.members().size, 3)
runBlocking {
boGroup.removeMembers(listOf(caro.walletAddress))
alixGroup.sync()
}
assertEquals(alixGroup.memberInboxIds().size, 2)
assertEquals(boGroup.memberInboxIds().size, 2)
assertEquals(alixGroup.members().size, 2)
assertEquals(boGroup.members().size, 2)

assertThrows(XMTPException::class.java) {
runBlocking { alixGroup.addMembers(listOf(caro.walletAddress)) }
}
runBlocking { boGroup.sync() }
assertEquals(alixGroup.memberInboxIds().size, 2)
assertEquals(boGroup.memberInboxIds().size, 2)
assertEquals(alixGroup.members().size, 2)
assertEquals(boGroup.members().size, 2)

assertEquals(boGroup.permissionLevel(), GroupPermissions.GROUP_CREATOR_IS_ADMIN)
assertEquals(alixGroup.permissionLevel(), GroupPermissions.GROUP_CREATOR_IS_ADMIN)
Expand All @@ -162,7 +162,7 @@ class GroupTest {
)
}
assertEquals(
group.memberInboxIds().sorted(),
group.members().map { it.inboxId }.sorted(),
listOf(
caroClient.inboxId.lowercase(),
alixClient.inboxId.lowercase(),
Expand Down Expand Up @@ -207,7 +207,7 @@ class GroupTest {
val group = runBlocking { boClient.conversations.newGroup(listOf(alix.walletAddress)) }
runBlocking { group.addMembers(listOf(caro.walletAddress)) }
assertEquals(
group.memberInboxIds().sorted(),
group.members().map { it.inboxId }.sorted(),
listOf(
caroClient.inboxId.lowercase(),
alixClient.inboxId.lowercase(),
Expand All @@ -228,7 +228,7 @@ class GroupTest {
}
runBlocking { group.removeMembers(listOf(caro.walletAddress)) }
assertEquals(
group.memberInboxIds().sorted(),
group.members().map { it.inboxId }.sorted(),
listOf(
alixClient.inboxId.lowercase(),
boClient.inboxId.lowercase()
Expand All @@ -241,7 +241,7 @@ class GroupTest {
val group = runBlocking { boClient.conversations.newGroup(listOf(alix.walletAddress)) }
runBlocking { group.addMembersByInboxId(listOf(caroClient.inboxId)) }
assertEquals(
group.memberInboxIds().sorted(),
group.members().map { it.inboxId }.sorted(),
listOf(
caroClient.inboxId.lowercase(),
alixClient.inboxId.lowercase(),
Expand All @@ -262,7 +262,7 @@ class GroupTest {
}
runBlocking { group.removeMembersByInboxId(listOf(caroClient.inboxId)) }
assertEquals(
group.memberInboxIds().sorted(),
group.members().map { it.inboxId }.sorted(),
listOf(
alixClient.inboxId.lowercase(),
boClient.inboxId.lowercase()
Expand All @@ -284,7 +284,7 @@ class GroupTest {
val group = runBlocking { alixClient.conversations.listGroups().first() }
runBlocking { group.removeMembers(listOf(caroClient.address)) }
assertEquals(
group.memberInboxIds().sorted(),
group.members().map { it.inboxId }.sorted(),
listOf(
alixClient.inboxId.lowercase(),
boClient.inboxId.lowercase()
Expand Down
13 changes: 7 additions & 6 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ data class ClientOptions(
val preEnableIdentityCallback: PreEventCallback? = null,
val appContext: Context? = null,
val enableAlphaMls: Boolean = false,
val dbPath: String? = null,
val dbDirectory: String? = null,
val dbEncryptionKey: ByteArray? = null,
) {
data class Api(
Expand Down Expand Up @@ -324,14 +324,15 @@ class Client() {
}
val alias = "xmtp-${options.api.env}-${inboxId.lowercase()}"

dbPath = if (options.dbPath == null) {
val dbDir = File(appContext?.filesDir?.absolutePath, "xmtp_db")
dbDir.mkdir()
dbDir.absolutePath + "/$alias.db3"
val dbDir = if (options.dbDirectory == null) {
File(appContext?.filesDir?.absolutePath, "xmtp_db")
} else {
options.dbPath
File(appContext?.filesDir?.absolutePath, options.dbDirectory)
}

dbDir.mkdir()
dbPath = dbDir.absolutePath + "/$alias.db3"

val encryptionKey = if (options.dbEncryptionKey == null) {
val keyStore = KeyStore.getInstance("AndroidKeyStore")
withContext(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sealed class Conversation {
return when (this) {
is V1 -> conversationV1.peerAddress
is V2 -> conversationV2.peerAddress
is Group -> group.peerAddresses().joinToString(",")
is Group -> group.peerInboxIds().joinToString(",")
}
}

Expand All @@ -65,7 +65,7 @@ sealed class Conversation {
return when (this) {
is V1 -> listOf(conversationV1.peerAddress)
is V2 -> listOf(conversationV2.peerAddress)
is Group -> group.peerAddresses()
is Group -> group.peerInboxIds()
}
}

Expand Down
15 changes: 4 additions & 11 deletions library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import uniffi.xmtpv3.FfiListMessagesOptions
import uniffi.xmtpv3.FfiMessage
import uniffi.xmtpv3.FfiMessageCallback
import uniffi.xmtpv3.GroupPermissions
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.Member
import java.util.Date
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.DurationUnit
Expand Down Expand Up @@ -202,24 +203,16 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
}
}

fun memberInboxIds(): List<String> {
return libXMTPGroup.listMembers().map { it.inboxId }
fun members(): List<Member> {
return libXMTPGroup.listMembers().map { Member(it) }
}

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

fun peerAddresses(): List<String> {
val addresses =
libXMTPGroup.listMembers().map { it.accountAddresses.first() }.map { it.lowercase() }
.toMutableList()
addresses.remove(client.address.lowercase())
return addresses
}

suspend fun updateGroupName(name: String) {
return libXMTPGroup.updateGroupName(name)
}
Expand Down
12 changes: 12 additions & 0 deletions library/src/main/java/org/xmtp/android/library/libxmtp/Member.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uniffi.xmtpv3.org.xmtp.android.library.libxmtp

import org.xmtp.android.library.Client
import uniffi.xmtpv3.FfiGroupMember

class Member(private val ffiMember: FfiGroupMember) {

val inboxId: String
get() = ffiMember.inboxId
val addresses: List<String>
get() = ffiMember.accountAddresses
}

0 comments on commit d2cb60b

Please sign in to comment.