From 670ba0a2cfee2391da6968814eeeef68b0c604e4 Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Fri, 12 Apr 2024 01:16:05 -0700 Subject: [PATCH] group name functionality added --- .../modules/xmtpreactnativesdk/XMTPModule.kt | 30 +++++++++++-- example/src/tests/groupTests.ts | 42 +++++++++++++++++++ ios/XMTPModule.swift | 24 +++++++++++ src/index.ts | 15 +++++++ src/lib/Group.ts | 12 ++++++ 5 files changed, 119 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index ca64d7822..32776785c 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -794,6 +794,26 @@ class XMTPModule : Module() { } } + AsyncFunction("groupName") Coroutine { clientAddress: String, id: String -> + withContext(Dispatchers.IO) { + logV("groupName") + val client = clients[clientAddress] ?: throw XMTPException("No client") + val group = findGroup(clientAddress, id) + + group?.name + } + } + + AsyncFunction("updateGroupName") Coroutine { clientAddress: String, id: String, groupName: String -> + withContext(Dispatchers.IO) { + logV("updateGroupName") + val client = clients[clientAddress] ?: throw XMTPException("No client") + val group = findGroup(clientAddress, id) + + group?.updateGroupName(groupName) + } + } + AsyncFunction("isGroupActive") Coroutine { clientAddress: String, id: String -> withContext(Dispatchers.IO) { logV("isGroupActive") @@ -1002,10 +1022,12 @@ class XMTPModule : Module() { } } - AsyncFunction("refreshConsentList") { clientAddress: String -> - val client = clients[clientAddress] ?: throw XMTPException("No client") - val consentList = client.contacts.refreshConsentList() - consentList.entries.map { ConsentWrapper.encode(it.value) } + AsyncFunction("refreshConsentList") Coroutine { clientAddress: String -> + withContext(Dispatchers.IO) { + val client = clients[clientAddress] ?: throw XMTPException("No client") + val consentList = client.contacts.refreshConsentList() + consentList.entries.map { ConsentWrapper.encode(it.value) } + } } AsyncFunction("conversationConsentState") Coroutine { clientAddress: String, conversationTopic: String -> diff --git a/example/src/tests/groupTests.ts b/example/src/tests/groupTests.ts index e83e4ce67..e4c73c927 100644 --- a/example/src/tests/groupTests.ts +++ b/example/src/tests/groupTests.ts @@ -1265,6 +1265,48 @@ test('skipSync parameter behaves as expected', async () => { return true }) +test('can read and update group name', async () => { + const [alix, bo, caro] = await createClients(3) + const alixGroup = await alix.conversations.newGroup([bo.address]) + + let groupName = await alixGroup.groupName() + + assert(groupName === 'New Group', 'group name should be "New Group"') + + await alixGroup.updateGroupName('Test name update 1') + + groupName = await alixGroup.groupName() + + assert( + groupName === 'Test name update 1', + 'group name should be "Test name update 1"' + ) + + const boGroup = (await bo.conversations.listGroups())[0] + groupName = await boGroup.groupName(true) + + assert(groupName === 'New Group', 'group name should be "New Group"') + + await boGroup.sync() + + groupName = await boGroup.groupName(true) + + assert( + groupName === 'Test name update 1', + 'group name should be "Test name update 1"' + ) + + await alixGroup.addMembers([caro.address]) + const caroGroup = (await caro.conversations.listGroups())[0] + + groupName = await caroGroup.groupName(true) + assert( + groupName === 'Test name update 1', + 'group name should be "Test name update 1"' + ) + return true +}) + // Commenting this out so it doesn't block people, but nice to have? // test('can stream messages for a long time', async () => { // const bo = await Client.createRandom({ env: 'local', enableAlphaMls: true }) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index cdb90dd3d..3d3e5ad25 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -692,6 +692,30 @@ public class XMTPModule: Module { try await group.removeMembers(addresses: peerAddresses) } + + AsyncFunction("groupName") { (clientAddress: String, id: String) -> String in + guard let client = await clientsManager.getClient(key: clientAddress) else { + throw Error.noClient + } + + guard let group = try await findGroup(clientAddress: clientAddress, id: id) else { + throw Error.conversationNotFound("no group found for \(id)") + } + + return try group.groupName() + } + + AsyncFunction("updateGroupName") { (clientAddress: String, id: String, groupName: String) in + guard let client = await clientsManager.getClient(key: clientAddress) else { + throw Error.noClient + } + + guard let group = try await findGroup(clientAddress: clientAddress, id: id) else { + throw Error.conversationNotFound("no group found for \(id)") + } + + try await group.updateGroupName(groupName: groupName) + } AsyncFunction("isGroupActive") { (clientAddress: String, id: String) -> Bool in guard let client = await clientsManager.getClient(key: clientAddress) else { diff --git a/src/index.ts b/src/index.ts index 7fcb861f4..70c6d6087 100644 --- a/src/index.ts +++ b/src/index.ts @@ -218,6 +218,21 @@ export async function removeGroupMembers( return XMTPModule.removeGroupMembers(clientAddress, id, addresses) } +export function groupName( + address: string, + id: string +): string | PromiseLike { + return XMTPModule.groupName(address, id) +} + +export function updateGroupName( + address: string, + id: string, + groupName: string +): Promise { + return XMTPModule.updateGroupName(address, id, groupName) +} + export async function sign( clientAddress: string, digest: Uint8Array, diff --git a/src/lib/Group.ts b/src/lib/Group.ts index 28a90a957..326754add 100644 --- a/src/lib/Group.ts +++ b/src/lib/Group.ts @@ -196,6 +196,18 @@ export class Group< return XMTP.removeGroupMembers(this.client.address, this.id, addresses) } + async groupName(skipSync = false): Promise { + if (!skipSync) { + await this.sync() + } + + return XMTP.groupName(this.client.address, this.id) + } + + async updateGroupName(groupName: string): Promise { + return XMTP.updateGroupName(this.client.address, this.id, groupName) + } + async isActive(skipSync = false): Promise { if (!skipSync) { await this.sync()