Try using offset pagination for account search

This commit is contained in:
Alex Gleason 2025-02-12 23:35:43 -06:00
parent a85daf1e67
commit 3c0e6dac76
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 9 additions and 5 deletions

View file

@ -12,7 +12,7 @@ import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
import { getFollowedPubkeys } from '@/queries.ts';
import { getPubkeysBySearch } from '@/utils/search.ts';
import { paginated } from '@/utils/api.ts';
import { paginated, paginatedList } from '@/utils/api.ts';
const searchQuerySchema = z.object({
q: z.string().transform(decodeURIComponent),
@ -77,7 +77,11 @@ const searchController: AppController = async (c) => {
hashtags: [],
};
return paginated(c, events, body);
if (result.data.type === 'accounts') {
return paginatedList(c, { ...result.data, ...params }, body);
} else {
return paginated(c, events, body);
}
};
/** Get events for the search params. */

View file

@ -243,18 +243,18 @@ function buildListLinkHeader(url: string, params: { offset: number; limit: numbe
function paginatedList(
c: AppContext,
params: { offset: number; limit: number },
entities: unknown[],
body: object | unknown[],
headers: HeaderRecord = {},
) {
const link = buildListLinkHeader(c.req.url, params);
const hasMore = entities.length > 0;
const hasMore = Array.isArray(body) ? body.length > 0 : true;
if (link) {
headers.link = hasMore ? link : link.split(', ').find((link) => link.endsWith('; rel="prev"'))!;
}
// Filter out undefined entities.
const results = entities.filter(Boolean);
const results = Array.isArray(body) ? body.filter(Boolean) : body;
return c.json(results, 200, headers);
}