Skip to content

Commit

Permalink
feat: V3 only dms
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 26, 2024
1 parent bbcbf99 commit b4b8a1d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 39 deletions.
35 changes: 10 additions & 25 deletions example/src/tests/conversationTests.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions example/src/tests/v3OnlyTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])
Expand Down
40 changes: 28 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,13 @@ export async function findGroup<
): Promise<Group<ContentTypes> | 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)
}

Expand All @@ -526,9 +530,13 @@ export async function findConversation<
): Promise<ConversationContainer<ContentTypes> | 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)
Expand All @@ -545,9 +553,13 @@ export async function findConversationByTopic<
): Promise<ConversationContainer<ContentTypes> | 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)
Expand All @@ -564,9 +576,13 @@ export async function findDm<
): Promise<Dm<ContentTypes> | 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)
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/Dm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
}

async consentState(): Promise<ConsentState> {
return await XMTP.conversationConsentState(this.client.inboxId, this.id)
return await XMTP.conversationV3ConsentState(this.client.inboxId, this.id)
}

async updateConsent(state: ConsentState): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ export class Group<
}

async consentState(): Promise<ConsentState> {
return await XMTP.conversationConsentState(this.client.inboxId, this.id)
return await XMTP.conversationV3ConsentState(this.client.inboxId, this.id)
}

async updateConsent(state: ConsentState): Promise<void> {
Expand Down

0 comments on commit b4b8a1d

Please sign in to comment.