Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Contacts.ConsentList.load() #671

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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