Skip to content

Commit

Permalink
fix: add new async handling to android native module
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Apr 1, 2024
1 parent 1fe40b2 commit be980b5
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class XMTPModule : Module() {
}
}

AsyncFunction("listGroups") { clientAddress: String ->
AsyncFunction("listGroups") Coroutine { clientAddress: String ->
logV("listGroups")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val groupList = client.conversations.listGroups()
Expand All @@ -483,7 +483,7 @@ class XMTPModule : Module() {
}
}

AsyncFunction("listAll") { clientAddress: String ->
AsyncFunction("listAll") Coroutine { clientAddress: String ->
val client = clients[clientAddress] ?: throw XMTPException("No client")
val conversationContainerList = client.conversations.list(includeGroups = true)
conversationContainerList.map { conversation ->
Expand Down Expand Up @@ -513,7 +513,7 @@ class XMTPModule : Module() {
.map { DecodedMessageWrapper.encode(it) }
}

AsyncFunction("groupMessages") { clientAddress: String, id: String, limit: Int?, before: Long?, after: Long?, direction: String? ->
AsyncFunction("groupMessages") Coroutine { clientAddress: String, id: String, limit: Int?, before: Long?, after: Long?, direction: String? ->
logV("groupMessages")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val beforeDate = if (before != null) Date(before) else null
Expand Down Expand Up @@ -589,7 +589,7 @@ class XMTPModule : Module() {
)
}

AsyncFunction("sendMessageToGroup") { clientAddress: String, id: String, contentJson: String ->
AsyncFunction("sendMessageToGroup") Coroutine { clientAddress: String, id: String, contentJson: String ->
logV("sendMessageToGroup")
val group =
findGroup(
Expand Down Expand Up @@ -705,7 +705,7 @@ class XMTPModule : Module() {
ConversationWrapper.encode(client, conversation)
}

AsyncFunction("createGroup") { clientAddress: String, peerAddresses: List<String>, permission: String ->
AsyncFunction("createGroup") Coroutine { clientAddress: String, peerAddresses: List<String>, permission: String ->
logV("createGroup")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val permissionLevel = when (permission) {
Expand All @@ -716,7 +716,7 @@ class XMTPModule : Module() {
GroupWrapper.encode(client, group)
}

AsyncFunction("listMemberAddresses") { clientAddress: String, groupId: String ->
AsyncFunction("listMemberAddresses") Coroutine { clientAddress: String, groupId: String ->
logV("listMembers")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val group = findGroup(clientAddress, groupId)
Expand All @@ -729,38 +729,38 @@ class XMTPModule : Module() {
runBlocking { client.conversations.syncGroups() }
}

AsyncFunction("syncGroup") { clientAddress: String, id: String ->
AsyncFunction("syncGroup") Coroutine { clientAddress: String, id: String ->
logV("syncGroup")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val group = findGroup(clientAddress, id)
runBlocking { group?.sync() }
}

AsyncFunction("addGroupMembers") { clientAddress: String, id: String, peerAddresses: List<String> ->
AsyncFunction("addGroupMembers") Coroutine { clientAddress: String, id: String, peerAddresses: List<String> ->
logV("addGroupMembers")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val group = findGroup(clientAddress, id)

runBlocking { group?.addMembers(peerAddresses) }
}

AsyncFunction("removeGroupMembers") { clientAddress: String, id: String, peerAddresses: List<String> ->
AsyncFunction("removeGroupMembers") Coroutine { clientAddress: String, id: String, peerAddresses: List<String> ->
logV("removeGroupMembers")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val group = findGroup(clientAddress, id)

runBlocking { group?.removeMembers(peerAddresses) }
}

Function("isGroupActive") { clientAddress: String, id: String ->
AsyncFunction("isGroupActive") Coroutine { clientAddress: String, id: String ->
logV("isGroupActive")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val group = findGroup(clientAddress, id)

group?.isActive()
}

Function("isGroupAdmin") { clientAddress: String, id: String ->
AsyncFunction("isGroupAdmin") Coroutine { clientAddress: String, id: String ->
logV("isGroupAdmin")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val group = findGroup(clientAddress, id)
Expand Down Expand Up @@ -801,7 +801,7 @@ class XMTPModule : Module() {
)
}

AsyncFunction("subscribeToGroupMessages") { clientAddress: String, id: String ->
AsyncFunction("subscribeToGroupMessages") Coroutine { clientAddress: String, id: String ->
logV("subscribeToGroupMessages")
subscribeToGroupMessages(
clientAddress = clientAddress,
Expand Down Expand Up @@ -837,7 +837,7 @@ class XMTPModule : Module() {
)
}

AsyncFunction("unsubscribeFromGroupMessages") { clientAddress: String, id: String ->
AsyncFunction("unsubscribeFromGroupMessages") Coroutine { clientAddress: String, id: String ->
logV("unsubscribeFromGroupMessages")
unsubscribeFromGroupMessages(
clientAddress = clientAddress,
Expand Down Expand Up @@ -925,14 +925,14 @@ class XMTPModule : Module() {
preEnableIdentityCallbackDeferred?.complete(Unit)
}

AsyncFunction("allowGroups") { clientAddress: String, groupIds: List<String> ->
AsyncFunction("allowGroups") Coroutine { clientAddress: String, groupIds: List<String> ->
logV("allowGroups")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val groupDataIds = groupIds.mapNotNull { Hex.hexStringToByteArray(it) }
client.contacts.allowGroup(groupDataIds)
}

AsyncFunction("denyGroups") { clientAddress: String, groupIds: List<String> ->
AsyncFunction("denyGroups") Coroutine { clientAddress: String, groupIds: List<String> ->
logV("denyGroups")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val groupDataIds = groupIds.mapNotNull { Hex.hexStringToByteArray(it) }
Expand Down Expand Up @@ -977,7 +977,7 @@ class XMTPModule : Module() {
return null
}

private fun findGroup(
private suspend fun findGroup(
clientAddress: String,
id: String,
): Group? {
Expand Down Expand Up @@ -1147,7 +1147,7 @@ class XMTPModule : Module() {
}
}

private fun subscribeToGroupMessages(clientAddress: String, id: String) {
private suspend fun subscribeToGroupMessages(clientAddress: String, id: String) {
val group =
findGroup(
clientAddress = clientAddress,
Expand Down Expand Up @@ -1202,7 +1202,7 @@ class XMTPModule : Module() {
subscriptions[conversation.cacheKey(clientAddress)]?.cancel()
}

private fun unsubscribeFromGroupMessages(
private suspend fun unsubscribeFromGroupMessages(
clientAddress: String,
id: String,
) {
Expand Down

0 comments on commit be980b5

Please sign in to comment.