From 60e5fb4abb3c5180aa4d1d87910e022ff4599add Mon Sep 17 00:00:00 2001 From: Nicholas Molnar Date: Wed, 5 Oct 2022 11:49:13 -0700 Subject: [PATCH 1/2] feat: add sorting of messages --- src/Client.ts | 4 +++- test/conversations/Conversation.test.ts | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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/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 From 09e33b99c2f5e0e8433eff7724e3b12ce15472a8 Mon Sep 17 00:00:00 2001 From: Nicholas Molnar Date: Wed, 5 Oct 2022 12:00:15 -0700 Subject: [PATCH 2/2] fix: export sortdirection from root --- src/index.ts | 2 ++ 1 file changed, 2 insertions(+) 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, }