From b4b8a1d4c852ba694b14103fb6e9f5767495c643 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Fri, 25 Oct 2024 21:49:16 -0700 Subject: [PATCH] feat: V3 only dms --- example/src/tests/conversationTests.ts | 35 +++++++--------------- example/src/tests/v3OnlyTests.ts | 39 +++++++++++++++++++++++++ src/index.ts | 40 ++++++++++++++++++-------- src/lib/Dm.ts | 2 +- src/lib/Group.ts | 2 +- 5 files changed, 79 insertions(+), 39 deletions(-) diff --git a/example/src/tests/conversationTests.ts b/example/src/tests/conversationTests.ts index 512e3523..ff1e0d4c 100644 --- a/example/src/tests/conversationTests.ts +++ b/example/src/tests/conversationTests.ts @@ -1,22 +1,5 @@ -import { Wallet } from 'ethers' -import { Platform } from 'expo-modules-core' -import { DecodedMessage } from 'xmtp-react-native-sdk/lib/DecodedMessage' - -import { - Test, - assert, - createClients, - createV3Clients, - delayToPropogate, -} from './test-utils' -import { - Client, - Conversation, - Dm, - Group, - ConversationContainer, - ConversationVersion, -} from '../../../src/index' +import { Test, assert, createV3Clients, delayToPropogate } from './test-utils' +import { ConversationContainer, ConversationVersion } from '../../../src/index' export const conversationTests: Test[] = [] let counter = 1 @@ -35,6 +18,9 @@ test('can find a conversations by id', async () => { await boClient.conversations.syncConversations() const boGroup = await boClient.conversations.findConversation(alixGroup.id) const boDm = await boClient.conversations.findConversation(alixDm.id) + const boDm2 = await boClient.conversations.findConversation('GARBAGE') + + assert(boDm2 === undefined, `bodm2 should be undefined`) assert( boGroup?.id === alixGroup.id, @@ -103,7 +89,7 @@ test('can list conversations with params', async () => { await boGroup1.send({ text: `third message` }) await boDm2.send({ text: `third message` }) await boGroup2.send({ text: `first message` }) - await boDm1.send({ text: `first message` }) + await boDm1.send({ text: `dm message` }) // Order should be [Dm1, Group2, Dm2, Group1] await boClient.conversations.syncAllConversations() @@ -133,13 +119,12 @@ test('can list conversations with params', async () => { const messages = await boConvosOrderLastMessage[0].messages() assert( - messages[0].content() === 'first message', - `last message should be first message ${messages[0].content()}` + messages[0].content() === 'dm message', + `last message 1 should be dm message ${messages[0].content()}` ) - // TODO FIX ME // assert( - // boConvosOrderLastMessage[0].lastMessage?.content() === 'first message', - // `last message should be last message ${boConvosOrderLastMessage[0].lastMessage?.content()}` + // boConvosOrderLastMessage[0].lastMessage?.content() === 'dm message', + // `last message 2 should be dm message ${boConvosOrderLastMessage[0].lastMessage?.content()}` // ) assert( boGroupsLimit.length === 1, diff --git a/example/src/tests/v3OnlyTests.ts b/example/src/tests/v3OnlyTests.ts index 8f3a0346..9e000478 100644 --- a/example/src/tests/v3OnlyTests.ts +++ b/example/src/tests/v3OnlyTests.ts @@ -82,6 +82,22 @@ test('can create group', async () => { ) }) +test('can create dm', async () => { + const [alixV2, boV3, caroV2V3] = await createV3TestingClients() + const dm = await boV3.conversations.findOrCreateDm(caroV2V3.address) + assert(dm?.members?.length === 2, `dm should have 2 members`) + + try { + await boV3.conversations.findOrCreateDm(alixV2.address) + // eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (error) { + return true + } + throw new Error( + 'should throw error when trying to add a V2 only client to a dm' + ) +}) + test('can send message', async () => { const [alixV2, boV3, caroV2V3] = await createV3TestingClients() const group = await boV3.conversations.newGroup([caroV2V3.address]) @@ -105,6 +121,29 @@ test('can send message', async () => { return true }) +test('can send messages to dm', async () => { + const [alixV2, boV3, caroV2V3] = await createV3TestingClients() + const dm = await boV3.conversations.findOrCreateDm(caroV2V3.address) + await dm.send('gm') + await dm.sync() + const dmMessages = await dm.messages() + assert( + dmMessages[0].content() === 'gm', + `first should be gm but was ${dmMessages[0].content()}` + ) + + await caroV2V3.conversations.syncConversations() + const sameDm = await caroV2V3.conversations.findConversation(dm.id) + await sameDm?.sync() + + const sameDmMessages = await sameDm!!.messages() + assert( + sameDmMessages[0].content() === 'gm', + `second should be gm but was ${sameDmMessages[0].content()}` + ) + return true +}) + test('can group consent', async () => { const [alixV2, boV3, caroV2V3] = await createV3TestingClients() const group = await boV3.conversations.newGroup([caroV2V3.address]) diff --git a/src/index.ts b/src/index.ts index ebfa11b5..05171383 100644 --- a/src/index.ts +++ b/src/index.ts @@ -512,9 +512,13 @@ export async function findGroup< ): Promise | undefined> { const json = await XMTPModule.findGroup(client.inboxId, groupId) const group = JSON.parse(json) - const members = group['members']?.map((mem: string) => { - return Member.from(mem) - }) + if (!group || Object.keys(group).length === 0) { + return undefined + } + const members = + group['members']?.map((mem: string) => { + return Member.from(mem) + }) || [] return new Group(client, group, members) } @@ -526,9 +530,13 @@ export async function findConversation< ): Promise | undefined> { const json = await XMTPModule.findConversation(client.inboxId, conversationId) const conversation = JSON.parse(json) - const members = conversation['members']?.map((mem: string) => { - return Member.from(mem) - }) + if (!conversation || Object.keys(conversation).length === 0) { + return undefined + } + const members = + conversation['members']?.map((mem: string) => { + return Member.from(mem) + }) || [] if (conversation.version === ConversationVersion.GROUP) { return new Group(client, conversation, members) @@ -545,9 +553,13 @@ export async function findConversationByTopic< ): Promise | undefined> { const json = await XMTPModule.findConversationByTopic(client.inboxId, topic) const conversation = JSON.parse(json) - const members = conversation['members']?.map((mem: string) => { - return Member.from(mem) - }) + if (!conversation || Object.keys(conversation).length === 0) { + return undefined + } + const members = + conversation['members']?.map((mem: string) => { + return Member.from(mem) + }) || [] if (conversation.version === ConversationVersion.GROUP) { return new Group(client, conversation, members) @@ -564,9 +576,13 @@ export async function findDm< ): Promise | undefined> { const json = await XMTPModule.findDm(client.inboxId, address) const dm = JSON.parse(json) - const members = dm['members']?.map((mem: string) => { - return Member.from(mem) - }) + if (!dm || Object.keys(dm).length === 0) { + return undefined + } + const members = + dm['members']?.map((mem: string) => { + return Member.from(mem) + }) || [] return new Dm(client, dm, members) } diff --git a/src/lib/Dm.ts b/src/lib/Dm.ts index 982ac7ca..dd965714 100644 --- a/src/lib/Dm.ts +++ b/src/lib/Dm.ts @@ -236,7 +236,7 @@ export class Dm } async consentState(): Promise { - return await XMTP.conversationConsentState(this.client.inboxId, this.id) + return await XMTP.conversationV3ConsentState(this.client.inboxId, this.id) } async updateConsent(state: ConsentState): Promise { diff --git a/src/lib/Group.ts b/src/lib/Group.ts index ded6c827..96558ec6 100644 --- a/src/lib/Group.ts +++ b/src/lib/Group.ts @@ -618,7 +618,7 @@ export class Group< } async consentState(): Promise { - return await XMTP.conversationConsentState(this.client.inboxId, this.id) + return await XMTP.conversationV3ConsentState(this.client.inboxId, this.id) } async updateConsent(state: ConsentState): Promise {