Skip to content

Commit

Permalink
feat: return DecodedMessage when sending PreparedMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Oct 17, 2023
1 parent b017588 commit 170f428
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/PreparedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Envelope } from '@xmtp/proto/ts/dist/types/message_api/v1/message_api.pb'
import { bytesToHex } from './crypto/utils'
import { sha256 } from './crypto/encryption'
import { DecodedMessage } from './Message'

export class PreparedMessage {
messageEnvelope: Envelope
onSend: () => Promise<void>
onSend: () => Promise<DecodedMessage>

constructor(messageEnvelope: Envelope, onSend: () => Promise<void>) {
constructor(
messageEnvelope: Envelope,
onSend: () => Promise<DecodedMessage>
) {
this.messageEnvelope = messageEnvelope
this.onSend = onSend
}
Expand All @@ -20,6 +24,6 @@ export class PreparedMessage {
}

async send() {
await this.onSend()
return this.onSend()
}
}
29 changes: 25 additions & 4 deletions src/conversations/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,31 @@ export class ConversationV1<ContentTypes>
}
const payload = await this.client.encodeContent(content, options)
const msg = await this.createMessage(payload, recipient, options?.timestamp)
const msgBytes = msg.toBytes()

const env: messageApi.Envelope = {
contentTopic: this.topic,
message: msg.toBytes(),
message: msgBytes,
timestampNs: toNanoString(msg.sent),
}

return new PreparedMessage(env, async () => {
await this.client.publishEnvelopes(
topics.map((topic) => ({
contentTopic: topic,
message: msg.toBytes(),
message: msgBytes,
timestamp: msg.sent,
}))
)

return DecodedMessage.fromV1Message(
msg,
content,
options?.contentType || ContentTypeText,
payload,
topics[0],
this
)
})
}

Expand Down Expand Up @@ -703,23 +713,34 @@ export class ConversationV2<ContentTypes>
): Promise<PreparedMessage> {
const payload = await this.client.encodeContent(content, options)
const msg = await this.createMessage(payload, options?.timestamp)
const msgBytes = msg.toBytes()

const topic = options?.ephemeral ? this.ephemeralTopic : this.topic

const env: messageApi.Envelope = {
contentTopic: topic,
message: msg.toBytes(),
message: msgBytes,
timestampNs: toNanoString(msg.sent),
}

return new PreparedMessage(env, async () => {
await this.client.publishEnvelopes([
{
contentTopic: this.topic,
message: msg.toBytes(),
message: msgBytes,
timestamp: msg.sent,
},
])

return DecodedMessage.fromV2Message(
msg,
content,
options?.contentType || ContentTypeText,
topic,
payload,
this,
this.client.address
)
})
}

Expand Down
8 changes: 6 additions & 2 deletions test/conversations/Conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ describe('conversation', () => {
const preparedMessage = await aliceConversation.prepareMessage('1')
const messageID = await preparedMessage.messageID()

await preparedMessage.send()
const sentMessage = await preparedMessage.send()

const messages = await aliceConversation.messages()
const message = messages[0]
expect(message.id).toBe(messageID)
expect(sentMessage.id).toBe(messageID)
expect(sentMessage.messageVersion).toBe('v1')
})

it('can send a prepared message v2', async () => {
Expand All @@ -157,12 +159,14 @@ describe('conversation', () => {
const preparedMessage = await aliceConversation.prepareMessage('sup')
const messageID = await preparedMessage.messageID()

await preparedMessage.send()
const sentMessage = await preparedMessage.send()

const messages = await aliceConversation.messages()
const message = messages[0]
expect(message.id).toBe(messageID)
expect(message.content).toBe('sup')
expect(sentMessage.id).toBe(messageID)
expect(sentMessage.messageVersion).toBe('v2')
})

it('can send and stream ephemeral topic v1', async () => {
Expand Down

0 comments on commit 170f428

Please sign in to comment.