Skip to content

Commit 816106f

Browse files
committed
add more tests and group streaming
1 parent 5c15d7b commit 816106f

File tree

5 files changed

+256
-25
lines changed

5 files changed

+256
-25
lines changed

library/src/androidTest/java/org/xmtp/android/library/GroupTest.kt

+60-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ class GroupTest {
5959

6060
@Test
6161
fun testCanListGroupMembers() {
62+
val group = boClient.conversations.newGroup(
63+
listOf(
64+
alix.walletAddress.lowercase(),
65+
caro.walletAddress.lowercase()
66+
)
67+
)
68+
assertEquals(
69+
group.memberAddresses().sorted(),
70+
listOf(
71+
caro.walletAddress.lowercase(),
72+
alix.walletAddress.lowercase(),
73+
bo.walletAddress.lowercase()
74+
).sorted()
75+
)
76+
}
77+
78+
@Test
79+
fun testCanAddGroupMembers() {
6280
val group = boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
6381
group.addMembers(listOf(caro.walletAddress.lowercase()))
6482
assertEquals(
@@ -71,6 +89,24 @@ class GroupTest {
7189
)
7290
}
7391

92+
@Test
93+
fun testCanRemoveGroupMembers() {
94+
val group = boClient.conversations.newGroup(
95+
listOf(
96+
alix.walletAddress.lowercase(),
97+
caro.walletAddress.lowercase()
98+
)
99+
)
100+
group.removeMembers(listOf(caro.walletAddress.lowercase()))
101+
assertEquals(
102+
group.memberAddresses().sorted(),
103+
listOf(
104+
alix.walletAddress.lowercase(),
105+
bo.walletAddress.lowercase()
106+
).sorted()
107+
)
108+
}
109+
74110
@Test
75111
fun testCanListGroups() {
76112
boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
@@ -83,7 +119,7 @@ class GroupTest {
83119
fun testCanListGroupsAndConversations() {
84120
boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
85121
boClient.conversations.newGroup(listOf(caro.walletAddress.lowercase()))
86-
boClient.conversations.newConversation(alix.walletAddress)
122+
boClient.conversations.newConversation(alix.walletAddress.lowercase())
87123
val convos = boClient.conversations.list(includeGroups = true)
88124
assertEquals(convos.size, 3)
89125
}
@@ -100,4 +136,27 @@ class GroupTest {
100136
assertEquals(sameGroup.messages().size, 2)
101137
assertEquals(sameGroup.messages().first().body, "gm")
102138
}
139+
140+
@Test
141+
fun testCanSendContentTypesToGroup() {
142+
val group = boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
143+
144+
}
145+
146+
@Test
147+
fun testCanStreamGroupMessages() {
148+
val group = boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
149+
}
150+
151+
@Test
152+
fun testCanStreamGroups() {
153+
val group = boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
154+
155+
}
156+
157+
@Test
158+
fun testCanStreamGroupsAndConversations() {
159+
val group = boClient.conversations.newGroup(listOf(alix.walletAddress.lowercase()))
160+
161+
}
103162
}

library/src/main/java/org/xmtp/android/library/Conversations.kt

+34-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package org.xmtp.android.library
33
import android.util.Log
44
import io.grpc.StatusException
55
import kotlinx.coroutines.CancellationException
6+
import kotlinx.coroutines.coroutineScope
67
import kotlinx.coroutines.flow.Flow
78
import kotlinx.coroutines.flow.MutableStateFlow
89
import kotlinx.coroutines.flow.flow
10+
import kotlinx.coroutines.launch
911
import kotlinx.coroutines.runBlocking
1012
import org.xmtp.android.library.GRPCApiClient.Companion.makeQueryRequest
1113
import org.xmtp.android.library.GRPCApiClient.Companion.makeSubscribeRequest
14+
import org.xmtp.android.library.libxmtp.Message
15+
import org.xmtp.android.library.libxmtp.MessageEmitter
1216
import org.xmtp.android.library.messages.Envelope
1317
import org.xmtp.android.library.messages.EnvelopeBuilder
1418
import org.xmtp.android.library.messages.InvitationV1
@@ -34,6 +38,7 @@ import org.xmtp.proto.message.contents.Invitation
3438
import org.xmtp.android.library.messages.DecryptedMessage
3539
import uniffi.xmtpv3.FfiConversations
3640
import uniffi.xmtpv3.FfiListConversationsOptions
41+
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.GroupEmitter
3742
import java.util.Date
3843

3944
data class Conversations(
@@ -450,7 +455,21 @@ data class Conversations(
450455
* of the information of those conversations according to the topics
451456
* @return Stream of data information for the conversations
452457
*/
453-
fun stream(): Flow<Conversation> = flow {
458+
fun stream(includeGroups: Boolean = false): Flow<Conversation> = flow {
459+
if (includeGroups) {
460+
val groupEmitter = GroupEmitter()
461+
462+
coroutineScope {
463+
launch {
464+
groupEmitter.groups.collect { group ->
465+
emit(Conversation.Group(Group(client, group)))
466+
}
467+
}
468+
}
469+
470+
libXMTPConversations?.stream(groupEmitter.callback)
471+
}
472+
454473
val streamedConversationTopics: MutableSet<String> = mutableSetOf()
455474
client.subscribeTopic(
456475
listOf(Topic.userIntro(client.address), Topic.userInvite(client.address)),
@@ -474,6 +493,20 @@ data class Conversations(
474493
}
475494
}
476495

496+
fun streamGroups(): Flow<Group> = flow {
497+
val groupEmitter = GroupEmitter()
498+
499+
coroutineScope {
500+
launch {
501+
groupEmitter.groups.collect { group ->
502+
emit(Group(client, group))
503+
}
504+
}
505+
}
506+
507+
libXMTPConversations?.stream(groupEmitter.callback)
508+
}
509+
477510
/**
478511
* Get the stream of all messages of the current [Client]
479512
* @return Flow object of [DecodedMessage] that represents all the messages of the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package uniffi.xmtpv3.org.xmtp.android.library.libxmtp
2+
3+
import kotlinx.coroutines.flow.MutableSharedFlow
4+
import kotlinx.coroutines.flow.asSharedFlow
5+
import uniffi.xmtpv3.FfiConversationCallback
6+
import uniffi.xmtpv3.FfiGroup
7+
8+
class GroupEmitter {
9+
private val _groups = MutableSharedFlow<FfiGroup>()
10+
val groups = _groups.asSharedFlow()
11+
12+
val callback: FfiConversationCallback = object : FfiConversationCallback {
13+
override fun onConversation(conversation: FfiGroup) {
14+
_groups.tryEmit(conversation)
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)