Skip to content

Commit

Permalink
get all dm tests passing on both platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 11, 2024
1 parent d48013a commit 1da2c97
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 38 deletions.
20 changes: 10 additions & 10 deletions android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,9 @@ class XMTPModule : Module() {
}
}

AsyncFunction("sendMessageToConversation") Coroutine { inboxId: String, id: String, contentJson: String ->
AsyncFunction("sendMessage") Coroutine { inboxId: String, id: String, contentJson: String ->
withContext(Dispatchers.IO) {
logV("sendMessageToConversation")
logV("sendMessage")
val client = clients[inboxId] ?: throw XMTPException("No client")
val conversation = client.findConversation(id)
?: throw XMTPException("no conversation found for $id")
Expand Down Expand Up @@ -1151,10 +1151,10 @@ class XMTPModule : Module() {
subscribeToAllMessages(inboxId = inboxId, getStreamType(type))
}

AsyncFunction("subscribeToConversationMessages") Coroutine { inboxId: String, id: String ->
AsyncFunction("subscribeToMessages") Coroutine { inboxId: String, id: String ->
withContext(Dispatchers.IO) {
logV("subscribeToConversationMessages")
subscribeToConversationMessages(
logV("subscribeToMessages")
subscribeToMessages(
inboxId = inboxId,
id = id
)
Expand All @@ -1171,10 +1171,10 @@ class XMTPModule : Module() {
subscriptions[getConversationsKey(inboxId)]?.cancel()
}

AsyncFunction("unsubscribeFromConversationMessages") Coroutine { inboxId: String, id: String ->
AsyncFunction("unsubscribeFromMessages") Coroutine { inboxId: String, id: String ->
withContext(Dispatchers.IO) {
logV("unsubscribeFromConversationMessages")
unsubscribeFromConversationMessages(
logV("unsubscribeFromMessages")
unsubscribeFromMessages(
inboxId = inboxId,
id = id
)
Expand Down Expand Up @@ -1284,7 +1284,7 @@ class XMTPModule : Module() {
}
}

private suspend fun subscribeToConversationMessages(inboxId: String, id: String) {
private suspend fun subscribeToMessages(inboxId: String, id: String) {
val client = clients[inboxId] ?: throw XMTPException("No client")
val conversation = client.findConversation(id)
?: throw XMTPException("no conversation found for $id")
Expand Down Expand Up @@ -1317,7 +1317,7 @@ class XMTPModule : Module() {
return "conversations:$inboxId"
}

private fun unsubscribeFromConversationMessages(
private fun unsubscribeFromMessages(
inboxId: String,
id: String,
) {
Expand Down
28 changes: 14 additions & 14 deletions example/src/tests/dmTests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Test, assert, createClients, delayToPropogate } from './test-utils'
import {
Conversation,
ConversationId,
ConversationVersion,
} from '../../../src/index'
import { Conversation } from '../../../src/index'

export const dmTests: Test[] = []
let counter = 1
Expand All @@ -28,28 +24,27 @@ test('can list dms with params', async () => {
await boDm2.send({ text: `third message` })
await boGroup2.send({ text: `first message` })
await boDm1.send({ text: `dm message` })
// Order should be [Dm1, Group2, Dm2, Group1]

await boClient.conversations.syncAllConversations()
const boConvosOrderCreated = await boClient.conversations.listDms()
const boConvosOrderLastMessage = await boClient.conversations.listDms(
{ lastMessage: true },
'lastMessage'
)
const boGroupsLimit = await boClient.conversations.listDms({}, undefined, 1)
const boDmsLimit = await boClient.conversations.listDms({}, undefined, 1)

assert(
boConvosOrderCreated
.map((conversation: any) => conversation.id)
.toString() === [boDm1.id, boDm2.id].toString(),
`Conversation created at order should be ${[boDm1.id, boDm2.id].toString()} but was ${boConvosOrderCreated.map((group: any) => group.id).toString()}`
`Conversation created at order should be ${[boDm1.id, boDm2.id].toString()} but was ${boConvosOrderCreated.map((convo: any) => convo.id).toString()}`
)

assert(
boConvosOrderLastMessage
.map((conversation: any) => conversation.id)
.toString() === [boDm1.id, boDm2.id].toString(),
`Conversation last message order should be ${[boDm1.id, boDm2.id].toString()} but was ${boConvosOrderLastMessage.map((group: any) => group.id).toString()}`
`Conversation last message order should be ${[boDm1.id, boDm2.id].toString()} but was ${boConvosOrderLastMessage.map((convo: any) => convo.id).toString()}`
)

const messages = await boConvosOrderLastMessage[0].messages()
Expand All @@ -58,6 +53,11 @@ test('can list dms with params', async () => {
`last message 1 should be dm message ${messages[0].content()}`
)

assert(
boDmsLimit[0].id === boDm1.id,
`Dms limit should be ${boDm1.id} but was ${boDmsLimit[0].id}`
)

return true
})

Expand All @@ -68,7 +68,7 @@ test('can stream all dm messages', async () => {
let messageCallbacks = 0
await bo.conversations.stream(async () => {
conversationCallbacks++
})
}, 'dms')

await bo.conversations.streamAllMessages(async () => {
messageCallbacks++
Expand All @@ -82,12 +82,12 @@ test('can stream all dm messages', async () => {
await delayToPropogate()

assert(
conversationCallbacks === 2,
'conversation stream should have received 2 conversation'
conversationCallbacks === 1,
'conversation stream should have received 1 conversation'
)
assert(
messageCallbacks === 2,
'message stream should have received 2 message'
messageCallbacks === 1,
'message stream should have received 1 message'
)

return true
Expand Down
2 changes: 1 addition & 1 deletion ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ public class XMTPModule: Module {
.streamAllMessages(type: type)
{
try sendEvent(
"messages",
"message",
[
"inboxId": inboxId,
"message": DecodedMessageWrapper.encodeToObj(
Expand Down
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,13 @@ export async function prepareMessage(
return await XMTPModule.prepareMessage(inboxId, conversationId, contentJson)
}

export async function sendMessageToConversation(
export async function sendMessage(
inboxId: InboxId,
conversationId: ConversationId,
content: any
): Promise<MessageId> {
const contentJson = JSON.stringify(content)
return await XMTPModule.sendMessageToConversation(
inboxId,
conversationId,
contentJson
)
return await XMTPModule.sendMessage(inboxId, conversationId, contentJson)
}

export async function publishPreparedMessages(
Expand Down
7 changes: 1 addition & 6 deletions src/lib/Dm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
content = { text: content }
}

return await XMTP.sendMessageToConversation(
this.client.inboxId,
this.id,
content
)
return await XMTP.sendMessage(this.client.inboxId, this.id, content)
} catch (e) {
console.info('ERROR in send()', e.message)
throw e
Expand Down Expand Up @@ -179,7 +175,6 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
message: DecodedMessage<ContentTypes>
conversationId: string
}) => {
// Long term these checks should be able to be done on the native layer as well, but additional checks in JS for safety
if (inboxId !== this.client.inboxId) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Group<
content = { text: content }
}

return await XMTP.sendMessageToConversation(
return await XMTP.sendMessage(
this.client.inboxId,
this.id,
content
Expand Down

0 comments on commit 1da2c97

Please sign in to comment.