Skip to content

Commit

Permalink
Merge pull request #671 from marcomele/main
Browse files Browse the repository at this point in the history
Fix `Contacts.ConsentList.load()`
  • Loading branch information
rygine authored Oct 8, 2024
2 parents 0eac1bb + 760da4a commit 68393a8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-news-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/xmtp-js": patch
---

Allow larger page size and limit in the API client when querying for consent
8 changes: 7 additions & 1 deletion packages/js-sdk/src/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type QueryParams = {
export type QueryAllOptions = {
direction?: messageApi.SortDirection
limit?: number
pageSize?: number
}

export type QueryStreamOptions = Flatten<
Expand Down Expand Up @@ -317,14 +318,19 @@ export default class HttpApiClient implements ApiClient {
{
direction = SortDirection.SORT_DIRECTION_ASCENDING,
limit,
pageSize,
}: QueryAllOptions
): Promise<messageApi.Envelope[]> {
const out: messageApi.Envelope[] = []
const maxPageSize = params.contentTopic.startsWith('userpreferences-')
? 500
: 100

// Use queryIteratePages for better performance. 1/100th the number of Promises to resolve compared to queryStream
for await (const page of this.queryIteratePages(params, {
direction,
// If there is a limit of < 100, use that as the page size. Otherwise use 100 and stop if/when limit reached.
pageSize: limit && limit < 100 ? limit : 100,
pageSize: pageSize ? Math.min(pageSize, maxPageSize) : maxPageSize,
})) {
for (const envelope of page) {
out.push(envelope)
Expand Down
4 changes: 3 additions & 1 deletion packages/js-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type ListMessagesOptions = {
endTime?: Date
limit?: number
direction?: messageApi.SortDirection
pageSize?: number
}

export type ListMessagesPaginatedOptions = {
Expand Down Expand Up @@ -723,14 +724,15 @@ export default class Client<ContentTypes = any> {
if (!opts) {
opts = {}
}
const { startTime, endTime, limit } = opts
const { startTime, endTime, limit, pageSize } = opts

const envelopes = await this.apiClient.query(
{ contentTopic: topic, startTime, endTime },
{
direction:
opts.direction || messageApi.SortDirection.SORT_DIRECTION_ASCENDING,
limit,
pageSize,
}
)
const results: Out[] = []
Expand Down
2 changes: 1 addition & 1 deletion packages/js-sdk/src/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class ConsentList {
[timestampNs, message] as [string | undefined, Uint8Array],
{
// special exception for private preferences topic
limit: 500,
pageSize: 500,
// ensure messages are in ascending order
direction: messageApi.SortDirection.SORT_DIRECTION_ASCENDING,
startTime,
Expand Down

0 comments on commit 68393a8

Please sign in to comment.