diff --git a/src/Client.ts b/src/Client.ts index 1f53196c8..3fb11c204 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -49,6 +49,7 @@ export type ListMessagesOptions = { startTime?: Date endTime?: Date limit?: number + direction?: messageApi.SortDirection } export type ListMessagesPaginatedOptions = { @@ -491,7 +492,8 @@ export default class Client { const res = await this.apiClient.query( { contentTopics: [topic], startTime, endTime }, { - direction: messageApi.SortDirection.SORT_DIRECTION_ASCENDING, + direction: + opts.direction || messageApi.SortDirection.SORT_DIRECTION_ASCENDING, limit, } ) diff --git a/src/index.ts b/src/index.ts index e62b8a997..17eb3c144 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,7 @@ import { CompositeCodec, ContentTypeComposite, } from './codecs/Composite' +import { SortDirection } from './ApiClient' export { Client, @@ -53,4 +54,5 @@ export { Composite, CompositeCodec, ContentTypeComposite, + SortDirection, } diff --git a/test/conversations/Conversation.test.ts b/test/conversations/Conversation.test.ts index b72be9601..dbe0b0585 100644 --- a/test/conversations/Conversation.test.ts +++ b/test/conversations/Conversation.test.ts @@ -98,6 +98,23 @@ describe('conversation', () => { expect(numMessages).toBe(1) }) + it('allows for sorted listing', async () => { + const aliceConversation = await alice.conversations.newConversation( + bob.address + ) + await aliceConversation.send('1') + await sleep(1) + await aliceConversation.send('2') + + const sortedAscending = await aliceConversation.messages() + expect(sortedAscending[0].content).toBe('1') + + const sortedDescending = await aliceConversation.messages({ + direction: SortDirection.SORT_DIRECTION_DESCENDING, + }) + expect(sortedDescending[0].content).toBe('2') + }) + it('streams messages', async () => { const aliceConversation = await alice.conversations.newConversation( bob.address