Skip to content

Commit

Permalink
Added documentation for memberAddresses and new skipSync group test
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Apr 11, 2024
1 parent 485c3e2 commit 20a69ad
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 95d6ace79946933ecf80684613842ee553dd76a2

COCOAPODS: 1.14.2
COCOAPODS: 1.15.2
66 changes: 66 additions & 0 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,72 @@ test('can check if group is denied', async () => {
return true
})

test('skipSync parameter behaves as expected', async () => {
const [alix, bo, caro] = await createClients(3)
const alixGroup = await alix.conversations.newGroup([bo.address])

await alixGroup.send({ text: 'hello' })

// List groups with skipSync true will return empty until the first sync
let boGroups = await bo.conversations.listGroups(true)
assert(boGroups.length === 0, 'num groups for bo is 0 until we sync')

await bo.conversations.syncGroups()

boGroups = await bo.conversations.listGroups(true)
assert(boGroups.length === 1, 'num groups for bo is 1')

// Num members will include the initial num of members even before sync
let numMembers = (await boGroups[0].memberAddresses(true)).length
assert(numMembers === 2, 'num members should be 2')

// Num messages for a group will be 0 until we sync the group
let numMessages = (await boGroups[0].messages(true)).length
assert(numMessages === 0, 'num members should be 1')

await bo.conversations.syncGroups()

// Num messages is still 0 because we didnt sync the group itself
numMessages = (await boGroups[0].messages(true)).length
assert(numMessages === 0, 'num messages should be 0')

await boGroups[0].sync()

// after syncing the group we now see the correct number of messages
numMessages = (await boGroups[0].messages(true)).length
assert(numMessages === 1, 'num members should be 1')

await alixGroup.addMembers([caro.address])

numMembers = (await boGroups[0].memberAddresses(true)).length
assert(numMembers === 2, 'num members should be 2')

await bo.conversations.syncGroups()

// Even though we synced the groups, we need to sync the group itself to see the new member
numMembers = (await boGroups[0].memberAddresses(true)).length
assert(numMembers === 2, 'num members should be 2')

await boGroups[0].sync()

numMembers = (await boGroups[0].memberAddresses(true)).length
assert(numMembers === 3, 'num members should be 3')

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _alixGroup2 = await alix.conversations.newGroup([
bo.address,
caro.address,
])
boGroups = await bo.conversations.listGroups()
assert(boGroups.length === 2, 'num groups for bo is 2')

// Even before syncing the group, syncGroups will return the initial number of members
numMembers = (await boGroups[1].memberAddresses(true)).length
assert(numMembers === 3, 'num members should be 3')

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
15 changes: 12 additions & 3 deletions src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ export class Group<
return this.client.address
}

async memberAddresses(skipSync = false): Promise<string[]> {
/**
* This method returns an array of addresses associated with the group.
*
* @param {boolean} skipSync - Optional flag to skip syncing members with the network before returning. Defaults to false.
* If skipSync set to true, the method will return the array of member addresses already known from the last network sync.
* Setting skipSync to true is an optional optimization to immediately return all members without
* fetching from the network first. This is useful for clients who prefer to manage syncing logic themselves via the sync() method.
* @returns {Promise<DecodedMessage<ContentTypes>[]>} A Promise that resolves to an array of DecodedMessage objects.
*/
async memberAddresses(skipSync: boolean = false): Promise<string[]> {
if (!skipSync) {
await this.sync()
}
Expand Down Expand Up @@ -123,8 +132,8 @@ export class Group<
}

/**
* Executes a network request to fetch the latest messages associated with the group
* and save them to the local state.
* Executes a network request to fetch the latest messages and membership changes
* associated with the group and saves them to the local state.
*/
async sync() {
await XMTP.syncGroup(this.client.address, this.id)
Expand Down

0 comments on commit 20a69ad

Please sign in to comment.