Skip to content

Commit

Permalink
add a way to just stream one or the other and order dms and groups
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 8, 2024
1 parent 10b39ed commit 35502e2
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions library/src/main/java/org/xmtp/android/library/Conversations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ data class Conversations(
LAST_MESSAGE;
}

enum class ConversationType {
ALL,
GROUPS,
DMS;
}

suspend fun fromWelcome(envelopeBytes: ByteArray): Conversation {
val conversation = ffiConversations.processStreamedWelcomeMessage(envelopeBytes)
return if (conversation.groupMetadata().conversationType() == "dm") {
Expand Down Expand Up @@ -152,6 +158,7 @@ data class Conversations(
after: Date? = null,
before: Date? = null,
limit: Int? = null,
order: ConversationOrder = ConversationOrder.CREATED_AT,
consentState: ConsentState? = null,
): List<Group> {
val ffiGroups = ffiConversations.listGroups(
Expand All @@ -162,8 +169,9 @@ data class Conversations(
if (consentState != null) ConsentState.toFfiConsentState(consentState) else null
)
)
val sortedConversations = sortConversations(ffiGroups, order)

return ffiGroups.map {
return sortedConversations.map {
Group(client, it)
}
}
Expand All @@ -172,6 +180,7 @@ data class Conversations(
after: Date? = null,
before: Date? = null,
limit: Int? = null,
order: ConversationOrder = ConversationOrder.CREATED_AT,
consentState: ConsentState? = null,
): List<Dm> {
val ffiDms = ffiConversations.listDms(
Expand All @@ -182,8 +191,9 @@ data class Conversations(
if (consentState != null) ConsentState.toFfiConsentState(consentState) else null
)
)
val sortedConversations = sortConversations(ffiDms, order)

return ffiDms.map {
return sortedConversations.map {
Dm(client, it)
}
}
Expand Down Expand Up @@ -247,7 +257,7 @@ data class Conversations(
}
}

fun stream(/*Maybe Put a way to specify group, dm, or both?*/): Flow<Conversation> =
fun stream(type: ConversationType = ConversationType.ALL): Flow<Conversation> =
callbackFlow {
val conversationCallback = object : FfiConversationCallback {
override fun onConversation(conversation: FfiConversation) {
Expand All @@ -263,11 +273,16 @@ data class Conversations(
}
}

val stream = ffiConversations.stream(conversationCallback)
val stream = when (type) {
ConversationType.ALL -> ffiConversations.stream(conversationCallback)
ConversationType.GROUPS -> ffiConversations.streamGroups(conversationCallback)
ConversationType.DMS -> ffiConversations.streamDms(conversationCallback)
}

awaitClose { stream.end() }
}

fun streamAllMessages(/*Maybe Put a way to specify group, dm, or both?*/): Flow<DecodedMessage> =
fun streamAllMessages(type: ConversationType = ConversationType.ALL): Flow<DecodedMessage> =
callbackFlow {
val messageCallback = object : FfiMessageCallback {
override fun onMessage(message: FfiMessage) {
Expand All @@ -280,7 +295,12 @@ data class Conversations(
}
}

val stream = ffiConversations.streamAllMessages(messageCallback)
val stream = when (type) {
ConversationType.ALL -> ffiConversations.streamAllMessages(messageCallback)
ConversationType.GROUPS -> ffiConversations.streamAllGroupMessages(messageCallback)
ConversationType.DMS -> ffiConversations.streamAllDmMessages(messageCallback)
}

awaitClose { stream.end() }
}
}

0 comments on commit 35502e2

Please sign in to comment.