Skip to content

Commit

Permalink
Optimistic Sendings (#270)
Browse files Browse the repository at this point in the history
* dumpt he latest

* add the updated binaries

* add support for optimistic sends

* write more involved test

* fix the lint errors

* fix the lint
  • Loading branch information
nplasterer authored Jul 8, 2024
1 parent 0429b97 commit cc07eee
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,8 @@ class GroupTest {
.collect { message ->
allMessages.add(message.topic)
}
} catch (e: Exception) {}
} catch (e: Exception) {
}
}
Thread.sleep(2500)

Expand Down Expand Up @@ -871,4 +872,33 @@ class GroupTest {

assert(hex.hexToByteArray().contentEquals(boGroup.id))
}

@Test
fun testUnpublishedMessages() {
val boGroup = runBlocking {
boClient.conversations.newGroup(
listOf(
alix.walletAddress,
caro.walletAddress
)
)
}
val preparedMessage = boGroup.prepareMessage("Test text")
assertEquals(boGroup.messages().size, 2)
assertEquals(boGroup.messages(deliveryStatus = MessageDeliveryStatus.PUBLISHED).size, 1)
assertEquals(boGroup.messages(deliveryStatus = MessageDeliveryStatus.UNPUBLISHED).size, 1)

runBlocking {
preparedMessage.publish()
boGroup.sync()
}

assertEquals(boGroup.messages(deliveryStatus = MessageDeliveryStatus.PUBLISHED).size, 2)
assertEquals(boGroup.messages(deliveryStatus = MessageDeliveryStatus.UNPUBLISHED).size, 0)
assertEquals(boGroup.messages().size, 2)

val message = boGroup.messages().first()

assertEquals(preparedMessage.messageId, message.id)
}
}
4 changes: 2 additions & 2 deletions library/src/main/java/libxmtp-version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 5c78fbad
Version: d4d8134a
Branch: main
Date: 2024-07-03 01:23:38 +0000
Date: 2024-07-08 15:44:32 +0000
60 changes: 49 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 @@ -24,6 +24,7 @@ import uniffi.xmtpv3.FfiMetadataField
import uniffi.xmtpv3.FfiPermissionUpdateType
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionOption
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.PermissionPolicySet
import uniffi.xmtpv3.org.xmtp.android.library.libxmtp.UnpublishedMessage
import java.util.Date
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.DurationUnit
Expand Down Expand Up @@ -57,11 +58,11 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
get() = libXMTPGroup.groupPinnedFrameUrl()

suspend fun send(text: String): String {
return send(prepareMessage(content = text, options = null))
return send(encodeContent(content = text, options = null))
}

suspend fun <T> send(content: T, options: SendOptions? = null): String {
val preparedMessage = prepareMessage(content = content, options = options)
val preparedMessage = encodeContent(content = content, options = options)
return send(preparedMessage)
}

Expand All @@ -73,7 +74,7 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
return messageId.toHex()
}

fun <T> prepareMessage(content: T, options: SendOptions?): EncodedContent {
fun <T> encodeContent(content: T, options: SendOptions?): EncodedContent {
val codec = Client.codecRegistry.find(options?.contentType)

fun <Codec : ContentCodec<T>> encode(codec: Codec, content: Any?): EncodedContent {
Expand All @@ -99,6 +100,11 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
return encoded
}

fun <T> prepareMessage(content: T, options: SendOptions? = null): UnpublishedMessage {
val encodeContent = encodeContent(content = content, options = options)
return UnpublishedMessage(libXMTPGroup.sendOptimistic(encodeContent.toByteArray()))
}

suspend fun sync() {
libXMTPGroup.sync()
}
Expand Down Expand Up @@ -261,35 +267,67 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
}

suspend fun updateAddMemberPermission(newPermissionOption: PermissionOption) {
return libXMTPGroup.updatePermissionPolicy(FfiPermissionUpdateType.ADD_MEMBER, PermissionOption.toFfiPermissionPolicy(newPermissionOption), null)
return libXMTPGroup.updatePermissionPolicy(
FfiPermissionUpdateType.ADD_MEMBER,
PermissionOption.toFfiPermissionPolicy(newPermissionOption),
null
)
}

suspend fun updateRemoveMemberPermission(newPermissionOption: PermissionOption) {
return libXMTPGroup.updatePermissionPolicy(FfiPermissionUpdateType.REMOVE_MEMBER, PermissionOption.toFfiPermissionPolicy(newPermissionOption), null)
return libXMTPGroup.updatePermissionPolicy(
FfiPermissionUpdateType.REMOVE_MEMBER,
PermissionOption.toFfiPermissionPolicy(newPermissionOption),
null
)
}

suspend fun updateAddAdminPermission(newPermissionOption: PermissionOption) {
return libXMTPGroup.updatePermissionPolicy(FfiPermissionUpdateType.ADD_ADMIN, PermissionOption.toFfiPermissionPolicy(newPermissionOption), null)
return libXMTPGroup.updatePermissionPolicy(
FfiPermissionUpdateType.ADD_ADMIN,
PermissionOption.toFfiPermissionPolicy(newPermissionOption),
null
)
}

suspend fun updateRemoveAdminPermission(newPermissionOption: PermissionOption) {
return libXMTPGroup.updatePermissionPolicy(FfiPermissionUpdateType.REMOVE_ADMIN, PermissionOption.toFfiPermissionPolicy(newPermissionOption), null)
return libXMTPGroup.updatePermissionPolicy(
FfiPermissionUpdateType.REMOVE_ADMIN,
PermissionOption.toFfiPermissionPolicy(newPermissionOption),
null
)
}

suspend fun updateGroupNamePermission(newPermissionOption: PermissionOption) {
return libXMTPGroup.updatePermissionPolicy(FfiPermissionUpdateType.UPDATE_METADATA, PermissionOption.toFfiPermissionPolicy(newPermissionOption), FfiMetadataField.GROUP_NAME)
return libXMTPGroup.updatePermissionPolicy(
FfiPermissionUpdateType.UPDATE_METADATA,
PermissionOption.toFfiPermissionPolicy(newPermissionOption),
FfiMetadataField.GROUP_NAME
)
}

suspend fun updateGroupDescriptionPermission(newPermissionOption: PermissionOption) {
return libXMTPGroup.updatePermissionPolicy(FfiPermissionUpdateType.UPDATE_METADATA, PermissionOption.toFfiPermissionPolicy(newPermissionOption), FfiMetadataField.DESCRIPTION)
return libXMTPGroup.updatePermissionPolicy(
FfiPermissionUpdateType.UPDATE_METADATA,
PermissionOption.toFfiPermissionPolicy(newPermissionOption),
FfiMetadataField.DESCRIPTION
)
}

suspend fun updateGroupImageUrlSquarePermission(newPermissionOption: PermissionOption) {
return libXMTPGroup.updatePermissionPolicy(FfiPermissionUpdateType.UPDATE_METADATA, PermissionOption.toFfiPermissionPolicy(newPermissionOption), FfiMetadataField.IMAGE_URL_SQUARE)
return libXMTPGroup.updatePermissionPolicy(
FfiPermissionUpdateType.UPDATE_METADATA,
PermissionOption.toFfiPermissionPolicy(newPermissionOption),
FfiMetadataField.IMAGE_URL_SQUARE
)
}

suspend fun updateGroupPinnedFrameUrlPermission(newPermissionOption: PermissionOption) {
return libXMTPGroup.updatePermissionPolicy(FfiPermissionUpdateType.UPDATE_METADATA, PermissionOption.toFfiPermissionPolicy(newPermissionOption), FfiMetadataField.PINNED_FRAME_URL)
return libXMTPGroup.updatePermissionPolicy(
FfiPermissionUpdateType.UPDATE_METADATA,
PermissionOption.toFfiPermissionPolicy(newPermissionOption),
FfiMetadataField.PINNED_FRAME_URL
)
}

fun isAdmin(inboxId: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uniffi.xmtpv3.org.xmtp.android.library.libxmtp

import org.xmtp.android.library.toHex
import uniffi.xmtpv3.FfiUnpublishedMessage

class UnpublishedMessage(private val libXMTPUnpublishedMessage: FfiUnpublishedMessage) {
val messageId: String
get() = libXMTPUnpublishedMessage.id().toHex()

suspend fun publish() {
libXMTPUnpublishedMessage.publish()
}
}
Loading

0 comments on commit cc07eee

Please sign in to comment.