From db6b566e32f7a06c4052bd967648a7f788c3b44c Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Mon, 3 Jun 2024 00:11:59 -0700 Subject: [PATCH] fix: add the iOS side of group members --- .../modules/xmtpreactnativesdk/XMTPModule.kt | 6 +-- ios/Wrappers/MemberWrapper.swift | 37 +++++++++++++++++++ ios/XMTPModule.swift | 14 +++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 ios/Wrappers/MemberWrapper.swift diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index 597f608a4..4d133dbf0 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -806,12 +806,12 @@ class XMTPModule : Module() { } } - AsyncFunction("listGroupMember") Coroutine { clientAddress: String, groupId: String -> + AsyncFunction("listGroupMembers") Coroutine { clientAddress: String, groupId: String -> withContext(Dispatchers.IO) { - logV("listGroupMember") + logV("listGroupMembers") val client = clients[clientAddress] ?: throw XMTPException("No client") val group = findGroup(clientAddress, groupId) - group?.members().map { MemberWrapper.encode(it) } + group?.members()?.map { MemberWrapper.encode(it) } } } diff --git a/ios/Wrappers/MemberWrapper.swift b/ios/Wrappers/MemberWrapper.swift new file mode 100644 index 000000000..142dc55bd --- /dev/null +++ b/ios/Wrappers/MemberWrapper.swift @@ -0,0 +1,37 @@ +// +// MemberWrapper.swift +// XMTPReactNative +// +// Created by Naomi Plasterer on 6/3/24. +// + +import Foundation +import XMTP + +// Wrapper around XMTP.Group to allow passing these objects back into react native. +struct MemberWrapper { + static func encodeToObj(_ member: XMTP.Member) throws -> [String: Any] { + let permissionString = switch member.permissionLevel { + case .Member: + "member" + case .Admin: + "admin" + case .SuperAdmin: + "super_admin" + } + return [ + "inboxId": member.inboxId, + "addresses": member.addresses, + "permissionLevel": permissionString, + ] + } + + static func encode(_ member: XMTP.Member) throws -> String { + let obj = try encodeToObj(member) + let data = try JSONSerialization.data(withJSONObject: obj) + guard let result = String(data: data, encoding: .utf8) else { + throw WrapperError.encodeError("could not encode member") + } + return result + } +} diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index 98ff9645c..210dae2e6 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -697,6 +697,20 @@ public class XMTPModule: Module { return try group.members.map(\.inboxId) } + AsyncFunction("listGroupMembers") { (clientAddress: String, groupId: String) -> [String] in + guard let client = await clientsManager.getClient(key: clientAddress) else { + throw Error.noClient + } + + guard let group = try await findGroup(clientAddress: clientAddress, id: groupId) else { + throw Error.conversationNotFound("no group found for \(groupId)") + } + return try group.members.compactMap { member in + return try MemberWrapper.encode(member) + } + } + + AsyncFunction("syncGroups") { (clientAddress: String) in guard let client = await clientsManager.getClient(key: clientAddress) else { throw Error.noClient