Skip to content

Commit

Permalink
update the consent filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 1, 2024
1 parent 537c3b4 commit b235706
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
9 changes: 8 additions & 1 deletion library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.BatchQueryResponse
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryRequest
import uniffi.xmtpv3.FfiDeviceSyncKind
import uniffi.xmtpv3.FfiV2SubscribeRequest
import uniffi.xmtpv3.FfiV2Subscription
import uniffi.xmtpv3.FfiV2SubscriptionCallback
Expand Down Expand Up @@ -783,7 +784,8 @@ class Client() {
}

suspend fun requestMessageHistorySync() {
v3Client?.requestHistorySync() ?: throw XMTPException("Error no V3 client initialized")
v3Client?.sendSyncRequest(FfiDeviceSyncKind.MESSAGES)
?: throw XMTPException("Error no V3 client initialized")
}

suspend fun revokeAllOtherInstallations(signingKey: SigningKey) {
Expand All @@ -800,6 +802,11 @@ class Client() {
return InboxState(client.inboxState(refreshFromNetwork))
}

suspend fun syncConsent() {
v3Client?.sendSyncRequest(FfiDeviceSyncKind.CONSENT)
?: throw XMTPException("Error no V3 client initialized")
}

val privateKeyBundle: PrivateKeyBundle
get() = PrivateKeyBundleBuilder.buildFromV1Key(v1keys)

Expand Down
13 changes: 13 additions & 0 deletions library/src/main/java/org/xmtp/android/library/ConversationV1.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import uniffi.xmtpv3.FfiEnvelope
import uniffi.xmtpv3.FfiV2SubscriptionCallback
import uniffi.xmtpv3.GenericException
import java.util.Date

data class ConversationV1(
Expand All @@ -49,6 +50,10 @@ data class ConversationV1(
override fun onMessage(message: FfiEnvelope) {
trySend(decode(envelope = envelopeFromFFi(message)))
}

override fun onError(error: GenericException) {
Log.e("XMTP V1 stream", error.message.toString())
}
}
val stream = client.subscribe(listOf(topic.description), streamCallback)
awaitClose { launch { stream.end() } }
Expand Down Expand Up @@ -283,6 +288,10 @@ data class ConversationV1(
override fun onMessage(message: FfiEnvelope) {
trySend(envelopeFromFFi(message))
}

override fun onError(error: GenericException) {
Log.e("XMTP V1 stream", error.message.toString())
}
}
val stream = client.subscribe(listOf(ephemeralTopic), streamCallback)
awaitClose { launch { stream.end() } }
Expand All @@ -293,6 +302,10 @@ data class ConversationV1(
override fun onMessage(message: FfiEnvelope) {
trySend(decrypt(envelope = envelopeFromFFi(message)))
}

override fun onError(error: GenericException) {
Log.e("XMTP V1 stream", error.message.toString())
}
}
val stream = client.subscribe(listOf(topic.description), streamCallback)
awaitClose { launch { stream.end() } }
Expand Down
13 changes: 13 additions & 0 deletions library/src/main/java/org/xmtp/android/library/ConversationV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import org.xmtp.proto.message.contents.Invitation
import uniffi.xmtpv3.FfiEnvelope
import uniffi.xmtpv3.FfiV2SubscriptionCallback
import uniffi.xmtpv3.GenericException
import java.util.Date

data class ConversationV2(
Expand Down Expand Up @@ -149,6 +150,10 @@ data class ConversationV2(
trySend(it)
}
}

override fun onError(error: GenericException) {
Log.e("XMTP V2 stream", error.message.toString())
}
}
val stream = client.subscribe(listOf(topic), streamCallback)
awaitClose { launch { stream.end() } }
Expand Down Expand Up @@ -284,6 +289,10 @@ data class ConversationV2(
override fun onMessage(message: FfiEnvelope) {
trySend(Util.envelopeFromFFi(message))
}

override fun onError(error: GenericException) {
Log.e("XMTP V2 stream", error.message.toString())
}
}
val stream = client.subscribe(listOf(ephemeralTopic), streamCallback)
awaitClose { launch { stream.end() } }
Expand All @@ -294,6 +303,10 @@ data class ConversationV2(
override fun onMessage(message: FfiEnvelope) {
trySend(decrypt(envelope = Util.envelopeFromFFi(message)))
}

override fun onError(error: GenericException) {
Log.e("XMTP V2 stream", error.message.toString())
}
}
val stream = client.subscribe(listOf(topic), streamCallback)
awaitClose { launch { stream.end() } }
Expand Down
36 changes: 22 additions & 14 deletions library/src/main/java/org/xmtp/android/library/Conversations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import uniffi.xmtpv3.FfiSubscribeException
import uniffi.xmtpv3.FfiV2SubscribeRequest
import uniffi.xmtpv3.FfiV2Subscription
import uniffi.xmtpv3.FfiV2SubscriptionCallback
import uniffi.xmtpv3.GenericException
import uniffi.xmtpv3.NoPointer
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.GroupPermissionPreconfiguration
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionPolicySet
Expand Down Expand Up @@ -307,12 +308,14 @@ data class Conversations(
after: Date? = null,
before: Date? = null,
limit: Int? = null,
consentState: ConsentState? = null,
): List<Group> {
val ffiGroups = libXMTPConversations?.listGroups(
opts = FfiListConversationsOptions(
after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
limit?.toLong()
limit?.toLong(),
if (consentState != null) ConsentState.toFfiConsentState(consentState) else null
)
) ?: throw XMTPException("Client does not support V3 dms")

Expand All @@ -325,13 +328,15 @@ data class Conversations(
after: Date? = null,
before: Date? = null,
limit: Int? = null,
consentState: ConsentState? = null,
): List<Dm> {
if (client.hasV2Client) throw XMTPException("Only supported for V3 only clients.")
val ffiDms = libXMTPConversations?.listDms(
opts = FfiListConversationsOptions(
after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
limit?.toLong()
limit?.toLong(),
if (consentState != null) ConsentState.toFfiConsentState(consentState) else null
)
) ?: throw XMTPException("Client does not support V3 dms")

Expand All @@ -354,12 +359,12 @@ data class Conversations(
FfiListConversationsOptions(
after?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
before?.time?.nanoseconds?.toLong(DurationUnit.NANOSECONDS),
limit?.toLong()
limit?.toLong(),
if (consentState != null) ConsentState.toFfiConsentState(consentState) else null
)
) ?: throw XMTPException("Client does not support V3 dms")

val filteredConversations = filterByConsentState(ffiConversations, consentState)
val sortedConversations = sortConversations(filteredConversations, order)
val sortedConversations = sortConversations(ffiConversations, order)

return sortedConversations.map { it.toConversation() }
}
Expand Down Expand Up @@ -394,15 +399,6 @@ data class Conversations(
}
}

private fun filterByConsentState(
conversations: List<FfiConversation>,
consentState: ConsentState?,
): List<FfiConversation> {
return consentState?.let { state ->
conversations.filter { it.consentState() == toFfiConsentState(state) }
} ?: conversations
}

private fun FfiConversation.toConversation(): Conversation {
return if (groupMetadata().conversationType() == "dm") {
Conversation.Dm(Dm(client, this))
Expand Down Expand Up @@ -533,6 +529,10 @@ data class Conversations(
}
}
}

override fun onError(error: GenericException) {
Log.e("XMTP stream v2 conversations", error.message.toString())
}
}

val stream = client.subscribe2(
Expand Down Expand Up @@ -963,6 +963,10 @@ data class Conversations(
else -> {}
}
}

override fun onError(error: GenericException) {
Log.e("XMTP stream all v2 messages", error.message.toString())
}
}

stream = client.subscribe2(subscriptionRequest, subscriptionCallback)
Expand Down Expand Up @@ -1013,6 +1017,10 @@ data class Conversations(
else -> {}
}
}

override fun onError(error: GenericException) {
Log.e("XMTP stream all v2 messages", error.message.toString())
}
}

stream = client.subscribe2(subscriptionRequest, subscriptionCallback)
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ class Group(val client: Client, private val libXMTPGroup: FfiConversation) {
suspend fun updateConsentState(state: ConsentState) {
if (client.hasV2Client) {
when (state) {
ConsentState.ALLOWED -> client.contacts.allowConversations(groupIds = listOf(id))
ConsentState.DENIED -> client.contacts.denyConversations(groupIds = listOf(id))
ConsentState.ALLOWED -> client.contacts.allowConversations(conversationIds = listOf(id))
ConsentState.DENIED -> client.contacts.denyConversations(conversationIds = listOf(id))
ConsentState.UNKNOWN -> Unit
}
}
Expand Down

0 comments on commit b235706

Please sign in to comment.