Skip to content

Commit

Permalink
fix: add the iOS side of group members
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jun 3, 2024
1 parent b3a84df commit db6b566
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}

Expand Down
37 changes: 37 additions & 0 deletions ios/Wrappers/MemberWrapper.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
14 changes: 14 additions & 0 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit db6b566

Please sign in to comment.