Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… np/require-encryption-key
  • Loading branch information
nplasterer committed Apr 25, 2024
2 parents 443d86e + 878585d commit 16f5ac5
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,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")
Expand Down
42 changes: 42 additions & 0 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,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 })
Expand Down
24 changes: 24 additions & 0 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,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 {
Expand Down
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ export async function removeGroupMembers(
return XMTPModule.removeGroupMembers(clientAddress, id, addresses)
}

export function groupName(
address: string,
id: string
): string | PromiseLike<string> {
return XMTPModule.groupName(address, id)
}

export function updateGroupName(
address: string,
id: string,
groupName: string
): Promise<void> {
return XMTPModule.updateGroupName(address, id, groupName)
}

export async function sign(
clientAddress: string,
digest: Uint8Array,
Expand Down
12 changes: 12 additions & 0 deletions src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ export class Group<
return XMTP.removeGroupMembers(this.client.address, this.id, addresses)
}

async groupName(skipSync = false): Promise<string> {
if (!skipSync) {
await this.sync()
}

return XMTP.groupName(this.client.address, this.id)
}

async updateGroupName(groupName: string): Promise<void> {
return XMTP.updateGroupName(this.client.address, this.id, groupName)
}

async isActive(skipSync = false): Promise<boolean> {
if (!skipSync) {
await this.sync()
Expand Down

0 comments on commit 16f5ac5

Please sign in to comment.