Skip to content

Commit

Permalink
fix up the bad merge
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Apr 3, 2024
1 parent 0654405 commit 2d3f683
Showing 1 changed file with 156 additions and 0 deletions.
156 changes: 156 additions & 0 deletions android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,29 @@ class XMTPModule : Module() {
}
}

AsyncFunction("listGroups") Coroutine { clientAddress: String ->
withContext(Dispatchers.IO) {
logV("listGroups")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val groupList = client.conversations.listGroups()
groupList.map { group ->
groups[group.cacheKey(clientAddress)] = group
GroupWrapper.encode(client, group)
}
}
}

AsyncFunction("listAll") Coroutine { clientAddress: String ->
withContext(Dispatchers.IO) {
val client = clients[clientAddress] ?: throw XMTPException("No client")
val conversationContainerList = client.conversations.list(includeGroups = true)
conversationContainerList.map { conversation ->
conversations[conversation.cacheKey(clientAddress)] = conversation
ConversationContainerWrapper.encode(client, conversation)
}
}
}

AsyncFunction("loadMessages") Coroutine { clientAddress: String, topic: String, limit: Int?, before: Long?, after: Long?, direction: String? ->
withContext(Dispatchers.IO) {
logV("loadMessages")
Expand All @@ -502,6 +525,24 @@ class XMTPModule : Module() {
}
}

AsyncFunction("groupMessages") Coroutine { clientAddress: String, id: String, limit: Int?, before: Long?, after: Long?, direction: String? ->
withContext(Dispatchers.IO) {
logV("groupMessages")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val beforeDate = if (before != null) Date(before) else null
val afterDate = if (after != null) Date(after) else null
val group = findGroup(clientAddress, id)
group?.decryptedMessages(
limit = limit,
before = beforeDate,
after = afterDate,
direction = MessageApiOuterClass.SortDirection.valueOf(
direction ?: "SORT_DIRECTION_DESCENDING"
)
)?.map { DecodedMessageWrapper.encode(it) }
}
}

AsyncFunction("loadBatchMessages") Coroutine { clientAddress: String, topics: List<String> ->
withContext(Dispatchers.IO) {
logV("loadBatchMessages")
Expand Down Expand Up @@ -566,6 +607,23 @@ class XMTPModule : Module() {
}
}

AsyncFunction("sendMessageToGroup") Coroutine { clientAddress: String, id: String, contentJson: String ->
withContext(Dispatchers.IO) {
logV("sendMessageToGroup")
val group =
findGroup(
clientAddress = clientAddress,
id = id
)
?: throw XMTPException("no group found for $id")
val sending = ContentJson.fromJson(contentJson)
group.send(
content = sending.content,
options = SendOptions(contentType = sending.type)
)
}
}

AsyncFunction("prepareMessage") Coroutine { clientAddress: String, conversationTopic: String, contentJson: String ->
withContext(Dispatchers.IO) {
logV("prepareMessage")
Expand Down Expand Up @@ -675,6 +733,84 @@ class XMTPModule : Module() {
ConversationWrapper.encode(client, conversation)
}
}
AsyncFunction("createGroup") Coroutine { clientAddress: String, peerAddresses: List<String>, permission: String ->
withContext(Dispatchers.IO) {
logV("createGroup")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val permissionLevel = when (permission) {
"creator_admin" -> GroupPermissions.GROUP_CREATOR_IS_ADMIN
else -> GroupPermissions.EVERYONE_IS_ADMIN
}
val group = client.conversations.newGroup(peerAddresses, permissionLevel)
GroupWrapper.encode(client, group)
}
}

AsyncFunction("listMemberAddresses") Coroutine { clientAddress: String, groupId: String ->
withContext(Dispatchers.IO) {
logV("listMembers")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val group = findGroup(clientAddress, groupId)
group?.memberAddresses()
}
}

AsyncFunction("syncGroups") Coroutine { clientAddress: String ->
withContext(Dispatchers.IO) {
logV("syncGroups")
val client = clients[clientAddress] ?: throw XMTPException("No client")
client.conversations.syncGroups()
}
}

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

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

group?.addMembers(peerAddresses)
}
}

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

group?.removeMembers(peerAddresses)
}
}

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

group?.isActive()
}
}

AsyncFunction("isGroupAdmin") Coroutine { clientAddress: String, id: String ->
withContext(Dispatchers.IO) {
logV("isGroupAdmin")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val group = findGroup(clientAddress, id)

group?.isAdmin()
}
}

Function("subscribeToConversations") { clientAddress: String ->
logV("subscribeToConversations")
Expand Down Expand Up @@ -711,6 +847,16 @@ class XMTPModule : Module() {
}
}

AsyncFunction("subscribeToGroupMessages") Coroutine { clientAddress: String, id: String ->
withContext(Dispatchers.IO) {
logV("subscribeToGroupMessages")
subscribeToGroupMessages(
clientAddress = clientAddress,
id = id
)
}
}

Function("unsubscribeFromConversations") { clientAddress: String ->
logV("unsubscribeFromConversations")
subscriptions[getConversationsKey(clientAddress)]?.cancel()
Expand Down Expand Up @@ -741,6 +887,16 @@ class XMTPModule : Module() {
}
}

AsyncFunction("unsubscribeFromGroupMessages") Coroutine { clientAddress: String, id: String ->
withContext(Dispatchers.IO) {
logV("unsubscribeFromGroupMessages")
unsubscribeFromGroupMessages(
clientAddress = clientAddress,
id = id
)
}
}

Function("registerPushToken") { pushServer: String, token: String ->
logV("registerPushToken")
xmtpPush = XMTPPush(appContext.reactContext!!, pushServer)
Expand Down

0 comments on commit 2d3f683

Please sign in to comment.