Skip to content

Commit

Permalink
add support for getting members from the group and not loading again
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Aug 19, 2024
1 parent 7f685ef commit ed2fa97
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GroupWrapper {
"clientAddress" to client.address,
"id" to group.id,
"createdAt" to group.createdAt.time,
// "members" to group.members().map { MemberWrapper.encode(it) },
"members" to group.members().map { MemberWrapper.encode(it) },
"version" to "GROUP",
"topic" to group.topic,
"creatorInboxId" to group.creatorInboxId(),
Expand Down
29 changes: 7 additions & 22 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ test('can get members of a group', async () => {
const [alixClient, boClient] = await createClients(2)
const group = await alixClient.conversations.newGroup([boClient.address])

const members = await group.members()
const members = group.members

assert(members.length === 2, `Should be 2 members but was ${members.length}`)

Expand Down Expand Up @@ -514,10 +514,7 @@ test('can message in a group', async () => {
if (memberInboxIds.length !== 3) {
throw new Error('num group members should be 3')
}
const peerInboxIds = await alixGroup.peerInboxIds
if (peerInboxIds.length !== 2) {
throw new Error('num peer group members should be 2')
}

if (
!(
memberInboxIds.includes(alixClient.inboxId) &&
Expand All @@ -528,15 +525,6 @@ test('can message in a group', async () => {
throw new Error('missing address')
}

if (
!(
peerInboxIds.includes(boClient.inboxId) &&
peerInboxIds.includes(caroClient.inboxId)
)
) {
throw new Error('should include self')
}

// alix can send messages
await alixGroup.send('hello, world')
await alixGroup.send('gm')
Expand Down Expand Up @@ -2077,13 +2065,10 @@ test('can create new installation without breaking group', async () => {
await client1Group?.sync()
await client2Group?.sync()

assert(
(await client1Group?.members())?.length === 2,
`client 1 should see 2 members`
)
assert(client1Group?.members?.length === 2, `client 1 should see 2 members`)

assert(
(await client2Group?.members())?.length === 2,
(await client2Group?.membersList())?.length === 2,
`client 2 should see 2 members`
)

Expand All @@ -2100,7 +2085,7 @@ test('can create new installation without breaking group', async () => {

await client1Group?.send('This message will break the group')
assert(
(await client1Group?.members())?.length === 2,
client1Group?.members?.length === 2,
`client 1 should still see the 2 members`
)

Expand All @@ -2112,13 +2097,13 @@ test('can list many groups members in parallel', async () => {
const groups: Group[] = await createGroups(alix, [bo], 20, 0)

try {
await Promise.all(groups.slice(0, 10).map((g) => g.members()))
await Promise.all(groups.slice(0, 10).map((g) => g.membersList()))
} catch (e) {
throw new Error(`Failed listing 10 groups members with ${e}`)
}

try {
await Promise.all(groups.slice(0, 20).map((g) => g.members()))
await Promise.all(groups.slice(0, 20).map((g) => g.membersList()))
} catch (e) {
throw new Error(`Failed listing 20 groups members with ${e}`)
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Wrappers/GroupWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct GroupWrapper {
"clientAddress": client.address,
"id": group.id,
"createdAt": UInt64(group.createdAt.timeIntervalSince1970 * 1000),
"peerInboxIds": try group.peerInboxIds,
"members": try group.members.compactMap { member in return try MemberWrapper.encode(member) },
"version": "GROUP",
"topic": group.topic,
"creatorInboxId": try group.creatorInboxId(),
Expand Down
64 changes: 41 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,19 @@ export async function createGroup<
description,
pinnedFrameUrl,
}
return new Group(
client,
JSON.parse(
await XMTPModule.createGroup(
client.inboxId,
peerAddresses,
permissionLevel,
JSON.stringify(options)
)
const group = JSON.parse(
await XMTPModule.createGroup(
client.inboxId,
peerAddresses,
permissionLevel,
JSON.stringify(options)
)
)

const members = group['members'].map((mem: string) => {
return Member.from(mem)
})
return new Group(client, group, members)
}

export async function createGroupCustomPermissions<
Expand All @@ -212,24 +214,29 @@ export async function createGroupCustomPermissions<
description,
pinnedFrameUrl,
}
return new Group(
client,
JSON.parse(
await XMTPModule.createGroupCustomPermissions(
client.inboxId,
peerAddresses,
JSON.stringify(permissionPolicySet),
JSON.stringify(options)
)
const group = JSON.parse(
await XMTPModule.createGroupCustomPermissions(
client.inboxId,
peerAddresses,
JSON.stringify(permissionPolicySet),
JSON.stringify(options)
)
)
const members = group['members'].map((mem: string) => {
return Member.from(mem)
})
return new Group(client, group, members)
}

export async function listGroups<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
>(client: Client<ContentTypes>): Promise<Group<ContentTypes>[]> {
return (await XMTPModule.listGroups(client.inboxId)).map((json: string) => {
return new Group(client, JSON.parse(json))
const group = JSON.parse(json)
const members = group['members'].map((mem: string) => {
return Member.from(mem)
})
return new Group(client, group, members)
})
}

Expand Down Expand Up @@ -309,8 +316,12 @@ export async function findGroup<
client: Client<ContentTypes>,
groupId: string
): Promise<Group<ContentTypes> | undefined> {
const group = await XMTPModule.findGroup(client.inboxId, groupId)
return new Group(client, JSON.parse(group))
const json = await XMTPModule.findGroup(client.inboxId, groupId)
const group = JSON.parse(json)
const members = group['members'].map((mem: string) => {
return Member.from(mem)
})
return new Group(client, group, members)
}

export async function findV3Message<
Expand Down Expand Up @@ -567,7 +578,10 @@ export async function listAll<
return list.map((json: string) => {
const jsonObj = JSON.parse(json)
if (jsonObj.version === ConversationVersion.GROUP) {
return new Group(client, jsonObj)
const members = jsonObj.members.map((mem: string) => {
return Member.from(mem)
})
return new Group(client, jsonObj, members)
} else {
return new Conversation(client, jsonObj)
}
Expand Down Expand Up @@ -1146,7 +1160,11 @@ export async function processWelcomeMessage<
client.inboxId,
encryptedMessage
)
return new Group(client, JSON.parse(json))
const group = JSON.parse(json)
const members = group['members'].map((mem: string) => {
return Member.from(mem)
})
return new Group(client, group, members)
}

export async function exportNativeLogs() {
Expand Down
13 changes: 8 additions & 5 deletions src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type PermissionUpdateOption = 'allow' | 'deny' | 'admin' | 'super_admin'
export interface GroupParams {
id: string
createdAt: number
// members: Member[]
creatorInboxId: InboxId
topic: string
name: string
Expand All @@ -35,7 +34,7 @@ export class Group<
client: XMTP.Client<ContentTypes>
id: string
createdAt: number
// members: Member[]
members: Member[]
version = ConversationVersion.GROUP
topic: string
creatorInboxId: InboxId
Expand All @@ -46,11 +45,15 @@ export class Group<
description: string
// pinnedFrameUrl: string

constructor(client: XMTP.Client<ContentTypes>, params: GroupParams) {
constructor(
client: XMTP.Client<ContentTypes>,
params: GroupParams,
members: Member[]
) {
this.client = client
this.id = params.id
this.createdAt = params.createdAt
// this.members = params.members
this.members = members
this.topic = params.topic
this.creatorInboxId = params.creatorInboxId
this.name = params.name
Expand Down Expand Up @@ -628,7 +631,7 @@ export class Group<
* @returns {Promise<Member[]>} A Promise that resolves to an array of Member objects.
* To get the latest member list from the network, call sync() first.
*/
async members(): Promise<Member[]> {
async membersList(): Promise<Member[]> {
return await XMTP.listGroupMembers(this.client.inboxId, this.id)
}
}

0 comments on commit ed2fa97

Please sign in to comment.