Skip to content

Commit

Permalink
add the member inbox ids work to the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed May 29, 2024
1 parent d21dc24 commit e39ad5c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 49 deletions.
63 changes: 27 additions & 36 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,21 @@ test('can message in a group', async () => {
// alix group should match create time from list function
assert(alixGroups[0].createdAt === alixGroup.createdAt, 'group create time')

// alix can confirm memberAddresses
// alix can confirm memberInboxIds
await alixGroup.sync()
const memberAddresses = await alixGroup.memberAddresses()
if (memberAddresses.length !== 3) {
const memberInboxIds = await alixGroup.memberInboxIds()
if (memberInboxIds.length !== 3) {
throw new Error('num group members should be 3')
}
const peerAddresses = await alixGroup.peerAddresses
if (peerAddresses.length !== 2) {
throw new Error('num peer group members should be 2')
}
const lowercasedAddresses: string[] = memberAddresses.map((s) =>
s.toLowerCase()
)
if (
!(
lowercasedAddresses.includes(alixClient.address.toLowerCase()) &&
lowercasedAddresses.includes(boClient.address.toLowerCase()) &&
lowercasedAddresses.includes(caroClient.address.toLowerCase())
memberInboxIds.includes(alixClient.inboxId) &&
memberInboxIds.includes(boClient.inboxId) &&
memberInboxIds.includes(caroClient.inboxId)
)
) {
throw new Error('missing address')
Expand Down Expand Up @@ -368,19 +365,16 @@ test('can add members to a group', async () => {
throw new Error('num groups should be 1')
}

// alix can confirm memberAddresses
// alix can confirm memberInboxIds
await alixGroup.sync()
const memberAddresses = await alixGroup.memberAddresses()
if (memberAddresses.length !== 2) {
const memberInboxIds = await alixGroup.memberInboxIds()
if (memberInboxIds.length !== 2) {
throw new Error('num group members should be 2')
}
const lowercasedAddresses: string[] = memberAddresses.map((s) =>
s.toLowerCase()
)
if (
!(
lowercasedAddresses.includes(alixClient.address.toLowerCase()) &&
lowercasedAddresses.includes(boClient.address.toLowerCase())
memberInboxIds.includes(alixClient.inboxId) &&
memberInboxIds.includes(boClient.inboxId)
)
) {
throw new Error('missing address')
Expand Down Expand Up @@ -416,7 +410,7 @@ test('can add members to a group', async () => {
}

await boGroups[0].sync()
const boGroupMembers = await boGroups[0].memberAddresses()
const boGroupMembers = await boGroups[0].memberInboxIds()
if (boGroupMembers.length !== 3) {
throw new Error('num group members should be 3')
}
Expand Down Expand Up @@ -457,19 +451,16 @@ test('can remove members from a group', async () => {
throw new Error('num groups should be 1')
}

// alix can confirm memberAddresses
// alix can confirm memberInboxIds
await alixGroup.sync()
const memberAddresses = await alixGroup.memberAddresses()
if (memberAddresses.length !== 3) {
const memberInboxIds = await alixGroup.memberInboxIds()
if (memberInboxIds.length !== 3) {
throw new Error('num group members should be 3')
}
const lowercasedAddresses: string[] = memberAddresses.map((s) =>
s.toLowerCase()
)
if (
!(
lowercasedAddresses.includes(alixClient.address.toLowerCase()) &&
lowercasedAddresses.includes(boClient.address.toLowerCase())
memberInboxIds.includes(alixClient.inboxId) &&
memberInboxIds.includes(boClient.inboxId)
)
) {
throw new Error('missing address')
Expand Down Expand Up @@ -504,7 +495,7 @@ test('can remove members from a group', async () => {

await alixGroup.removeMembers([caroClient.address])
await alixGroup.sync()
const alixGroupMembers = await alixGroup.memberAddresses()
const alixGroupMembers = await alixGroup.memberInboxIds()
if (alixGroupMembers.length !== 2) {
throw new Error('num group members should be 2')
}
Expand All @@ -514,7 +505,7 @@ test('can remove members from a group', async () => {
throw new Error('caros group should not be active')
}

const caroGroupMembers = await caroGroups[0].memberAddresses()
const caroGroupMembers = await caroGroups[0].memberInboxIds()
if (caroGroupMembers.length !== 2) {
throw new Error('num group members should be 2')
}
Expand Down Expand Up @@ -828,12 +819,12 @@ test('can make a group with admin permissions', async () => {

const group = await adminClient.conversations.newGroup(
[anotherClient.address],
'creator_admin'
'admin_only'
)

if (group.permissionLevel !== 'creator_admin') {
if (group.permissionLevel !== 'admin_only') {
throw Error(
`Group permission level should be creator_admin but was ${group.permissionLevel}`
`Group permission level should be admin_only but was ${group.permissionLevel}`
)
}

Expand Down Expand Up @@ -1272,7 +1263,7 @@ test('sync function behaves as expected', async () => {
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()).length
let numMembers = (await boGroups[0].memberInboxIds()).length
assert(numMembers === 2, 'num members should be 2')

// Num messages for a group will be 0 until we sync the group
Expand All @@ -1293,18 +1284,18 @@ test('sync function behaves as expected', async () => {

await alixGroup.addMembers([caro.address])

numMembers = (await boGroups[0].memberAddresses()).length
numMembers = (await boGroups[0].memberInboxIds()).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()).length
numMembers = (await boGroups[0].memberInboxIds()).length
assert(numMembers === 2, 'num members should be 2')

await boGroups[0].sync()

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

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -1317,7 +1308,7 @@ test('sync function behaves as expected', async () => {
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()).length
numMembers = (await boGroups[1].memberInboxIds()).length
assert(numMembers === 3, 'num members should be 3')

return true
Expand Down
4 changes: 2 additions & 2 deletions ios/Wrappers/GroupWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ struct GroupWrapper {
static func encodeToObj(_ group: XMTP.Group, client: XMTP.Client) throws -> [String: Any] {
let permissionString = switch try group.permissionLevel() {
case .everyoneIsAdmin:
"everyone_admin"
"all_members"
case .groupCreatorIsAdmin:
"creator_admin"
"admin_only"
}
return [
"clientAddress": client.address,
Expand Down
6 changes: 3 additions & 3 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public class XMTPModule: Module {
}
let permissionLevel: GroupPermissions = {
switch permission {
case "creator_admin":
case "admin_only":
return .groupCreatorIsAdmin
default:
return .everyoneIsAdmin
Expand All @@ -657,15 +657,15 @@ public class XMTPModule: Module {
}
}

AsyncFunction("listMemberAddresses") { (clientAddress: String, groupId: String) -> [String] in
AsyncFunction("listMemberInboxIds") { (clientAddress: String, groupId: String) -> [String] in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}

guard let group = try await findGroup(clientAddress: clientAddress, id: groupId) else {
throw Error.conversationNotFound("no group found for \(groupId)")
}
return group.memberAddresses
return group.memberInboxIds
}

AsyncFunction("syncGroups") { (clientAddress: String) in
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function createGroup<
>(
client: Client<ContentTypes>,
peerAddresses: string[],
permissionLevel: 'everyone_admin' | 'creator_admin' = 'everyone_admin'
permissionLevel: 'all_members' | 'admin_only' = 'all_members'
): Promise<Group<ContentTypes>> {
return new Group(
client,
Expand All @@ -129,10 +129,10 @@ export async function listGroups<
})
}

export async function listMemberAddresses<
export async function listMemberInboxIds<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
>(client: Client<ContentTypes>, id: string): Promise<string[]> {
return XMTPModule.listMemberAddresses(client.address, id)
return XMTPModule.listMemberInboxIds(client.address, id)
}

export async function sendMessageToGroup(
Expand Down
15 changes: 15 additions & 0 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Client<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
> {
address: string
inboxId: string

Check failure on line 32 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'inboxId' has no initializer and is not definitely assigned in the constructor.

Check failure on line 32 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / test

Property 'inboxId' has no initializer and is not definitely assigned in the constructor.

Check failure on line 32 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'inboxId' has no initializer and is not definitely assigned in the constructor.
conversations: Conversations<ContentTypes>
contacts: Contacts
codecRegistry: { [key: string]: XMTPModule.ContentCodec<unknown> }
Expand Down Expand Up @@ -202,6 +203,20 @@ export class Client<
return await XMTPModule.deleteLocalDatabase(this.address)
}

/**
* Drop the local database connection. This function is delicate and should be used with caution. App will error if database not properly reconnected. See: reconnectLocalDatabase()
*/
dropLocalDatabaseConnection() {
return XMTPModule.dropLocalDatabaseConnection()

Check failure on line 210 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'dropLocalDatabaseConnection' does not exist on type 'typeof import("/home/runner/work/xmtp-react-native/xmtp-react-native/src/index")'.

Check failure on line 210 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / test

Property 'dropLocalDatabaseConnection' does not exist on type 'typeof import("/home/runner/work/xmtp-react-native/xmtp-react-native/src/index")'.

Check failure on line 210 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'dropLocalDatabaseConnection' does not exist on type 'typeof import("/home/runner/work/xmtp-react-native/xmtp-react-native/src/index")'.
}

/**
* Reconnects the local database after being dropped.
*/
async reconnectLocalDatabase() {
return await XMTPModule.reconnectLocalDatabase()

Check failure on line 217 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'reconnectLocalDatabase' does not exist on type 'typeof import("/home/runner/work/xmtp-react-native/xmtp-react-native/src/index")'.

Check failure on line 217 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / test

Property 'reconnectLocalDatabase' does not exist on type 'typeof import("/home/runner/work/xmtp-react-native/xmtp-react-native/src/index")'.

Check failure on line 217 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'reconnectLocalDatabase' does not exist on type 'typeof import("/home/runner/work/xmtp-react-native/xmtp-react-native/src/index")'.
}

/**
* Determines whether the current user can send messages to the specified peers over groups.
*
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export default class Conversations<
*/
async newGroup(
peerAddresses: string[],
permissionLevel: 'everyone_admin' | 'creator_admin' = 'everyone_admin'
permissionLevel: 'all_members' | 'admin_only' = 'all_members'
): Promise<Group<ContentTypes>> {
return await XMTPModule.createGroup(
this.client,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Group<
version = ConversationVersion.GROUP
topic: string
adminAddress: string
permissionLevel: 'everyone_admin' | 'creator_admin'
permissionLevel: 'all_members' | 'admin_only'

constructor(
client: XMTP.Client<ContentTypes>,
Expand All @@ -30,7 +30,7 @@ export class Group<
createdAt: number
peerAddresses: string[]
adminAddress: string
permissionLevel: 'everyone_admin' | 'creator_admin'
permissionLevel: 'all_members' | 'admin_only'
topic: string
}
) {
Expand All @@ -52,8 +52,8 @@ export class Group<
* To get the latest member addresses from the network, call sync() first.
* @returns {Promise<DecodedMessage<ContentTypes>[]>} A Promise that resolves to an array of DecodedMessage objects.
*/
async memberAddresses(): Promise<string[]> {
return XMTP.listMemberAddresses(this.client, this.id)
async memberInboxIds(): Promise<string[]> {
return XMTP.listMemberInboxIds(this.client, this.id)
}

/**
Expand Down

0 comments on commit e39ad5c

Please sign in to comment.