Skip to content

Commit

Permalink
Merge pull request #183 from xmtp/allow-sorted-list
Browse files Browse the repository at this point in the history
Add sorting of messages to list endpoint
  • Loading branch information
neekolas authored Oct 5, 2022
2 parents 8603d76 + 09e33b9 commit a056fee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type ListMessagesOptions = {
startTime?: Date
endTime?: Date
limit?: number
direction?: messageApi.SortDirection
}

export type ListMessagesPaginatedOptions = {
Expand Down Expand Up @@ -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,
}
)
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
CompositeCodec,
ContentTypeComposite,
} from './codecs/Composite'
import { SortDirection } from './ApiClient'

export {
Client,
Expand All @@ -53,4 +54,5 @@ export {
Composite,
CompositeCodec,
ContentTypeComposite,
SortDirection,
}
17 changes: 17 additions & 0 deletions test/conversations/Conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a056fee

Please sign in to comment.