Merge branch 'search-pagination' into 'main'

Try using offset pagination for account search

See merge request soapbox-pub/ditto!655
This commit is contained in:
Alex Gleason 2025-02-13 05:36:04 +00:00
commit e3f5630bed
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 { renderStatus } from '@/views/mastodon/statuses.ts';
import { getFollowedPubkeys } from '@/queries.ts'; import { getFollowedPubkeys } from '@/queries.ts';
import { getPubkeysBySearch } from '@/utils/search.ts'; import { getPubkeysBySearch } from '@/utils/search.ts';
import { paginated } from '@/utils/api.ts'; import { paginated, paginatedList } from '@/utils/api.ts';
const searchQuerySchema = z.object({ const searchQuerySchema = z.object({
q: z.string().transform(decodeURIComponent), q: z.string().transform(decodeURIComponent),
@ -77,7 +77,11 @@ const searchController: AppController = async (c) => {
hashtags: [], 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. */ /** Get events for the search params. */

View file

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