diff --git a/src/PreparedMessage.ts b/src/PreparedMessage.ts index fcd1482c8..0a59313da 100644 --- a/src/PreparedMessage.ts +++ b/src/PreparedMessage.ts @@ -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 + onSend: () => Promise - constructor(messageEnvelope: Envelope, onSend: () => Promise) { + constructor( + messageEnvelope: Envelope, + onSend: () => Promise + ) { this.messageEnvelope = messageEnvelope this.onSend = onSend } @@ -20,6 +24,6 @@ export class PreparedMessage { } async send() { - await this.onSend() + return this.onSend() } } diff --git a/src/conversations/Conversation.ts b/src/conversations/Conversation.ts index e8380411d..31db3b801 100644 --- a/src/conversations/Conversation.ts +++ b/src/conversations/Conversation.ts @@ -247,10 +247,11 @@ export class ConversationV1 } 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), } @@ -258,10 +259,19 @@ export class ConversationV1 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 + ) }) } @@ -703,12 +713,13 @@ export class ConversationV2 ): Promise { 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), } @@ -716,10 +727,20 @@ export class ConversationV2 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 + ) }) } diff --git a/test/conversations/Conversation.test.ts b/test/conversations/Conversation.test.ts index 5f5c51668..4bab7bf10 100644 --- a/test/conversations/Conversation.test.ts +++ b/test/conversations/Conversation.test.ts @@ -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 () => { @@ -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 () => {