From 20a69ad8fb6cf8365f35f6b93545d50a2ad545a6 Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Thu, 11 Apr 2024 09:25:48 -0700 Subject: [PATCH] Added documentation for memberAddresses and new skipSync group test --- example/ios/Podfile.lock | 2 +- example/src/tests/groupTests.ts | 66 +++++++++++++++++++++++++++++++++ src/lib/Group.ts | 15 ++++++-- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 40761c7fb..31e6dfa8d 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -769,4 +769,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 95d6ace79946933ecf80684613842ee553dd76a2 -COCOAPODS: 1.14.2 +COCOAPODS: 1.15.2 diff --git a/example/src/tests/groupTests.ts b/example/src/tests/groupTests.ts index 8e81e9e8d..e83e4ce67 100644 --- a/example/src/tests/groupTests.ts +++ b/example/src/tests/groupTests.ts @@ -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 }) diff --git a/src/lib/Group.ts b/src/lib/Group.ts index efeb68e14..28a90a957 100644 --- a/src/lib/Group.ts +++ b/src/lib/Group.ts @@ -45,7 +45,16 @@ export class Group< return this.client.address } - async memberAddresses(skipSync = false): Promise { + /** + * 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[]>} A Promise that resolves to an array of DecodedMessage objects. + */ + async memberAddresses(skipSync: boolean = false): Promise { if (!skipSync) { await this.sync() } @@ -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)