Add `paginated` helper function, DRY pagination code

This commit is contained in:
Alex Gleason 2023-08-29 12:55:00 -05:00
parent ce4a330812
commit 4216a7931a
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 28 additions and 30 deletions

View File

@ -6,7 +6,7 @@ import { booleanParamSchema } from '@/schema.ts';
import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { toAccount, toRelationship, toStatus } from '@/transformers/nostr-to-mastoapi.ts';
import { eventDateComparator, isFollowing, lookupAccount } from '@/utils.ts';
import { buildLinkHeader, paginationSchema, parseBody } from '@/utils/web.ts';
import { paginated, paginationSchema, parseBody } from '@/utils/web.ts';
import { createEvent } from '@/utils/web.ts';
const createAccountController: AppController = (c) => {
@ -110,9 +110,7 @@ const accountStatusesController: AppController = async (c) => {
}
const statuses = await Promise.all(events.map(toStatus));
const link = buildLinkHeader(c.req.url, events);
return c.json(statuses, 200, link ? { link } : undefined);
return paginated(c, events, statuses);
};
const fileSchema = z.custom<File>((value) => value instanceof File);

View File

@ -1,6 +1,6 @@
import { type AppController } from '@/app.ts';
import * as mixer from '@/mixer.ts';
import { buildLinkHeader, paginationSchema } from '@/utils/web.ts';
import { paginated, paginationSchema } from '@/utils/web.ts';
import { toNotification } from '@/transformers/nostr-to-mastoapi.ts';
import { Time } from '@/utils.ts';
@ -14,9 +14,7 @@ const notificationsController: AppController = async (c) => {
);
const statuses = await Promise.all(events.map(toNotification));
const link = buildLinkHeader(c.req.url, events);
return c.json(statuses, 200, link ? { link } : undefined);
return paginated(c, events, statuses);
};
export { notificationsController };

View File

@ -3,7 +3,7 @@ import * as mixer from '@/mixer.ts';
import { getFeed, getPublicFeed } from '@/queries.ts';
import { booleanParamSchema } from '@/schema.ts';
import { toStatus } from '@/transformers/nostr-to-mastoapi.ts';
import { buildLinkHeader, paginationSchema } from '@/utils/web.ts';
import { paginated, paginationSchema } from '@/utils/web.ts';
import { Time } from '@/utils.ts';
import type { AppController } from '@/app.ts';
@ -17,10 +17,8 @@ const homeTimelineController: AppController = async (c) => {
return c.json([]);
}
const statuses = (await Promise.all(events.map(toStatus))).filter(Boolean);
const link = buildLinkHeader(c.req.url, events);
return c.json(statuses, 200, link ? { link } : undefined);
const statuses = await Promise.all(events.map(toStatus));
return paginated(c, events, statuses);
};
const publicQuerySchema = z.object({
@ -36,10 +34,8 @@ const publicTimelineController: AppController = async (c) => {
return c.json([]);
}
const statuses = (await Promise.all(events.map(toStatus))).filter(Boolean);
const link = buildLinkHeader(c.req.url, events);
return c.json(statuses, 200, link ? { link } : undefined);
const statuses = await Promise.all(events.map(toStatus));
return paginated(c, events, statuses);
};
const hashtagTimelineController: AppController = async (c) => {
@ -55,10 +51,8 @@ const hashtagTimelineController: AppController = async (c) => {
return c.json([]);
}
const statuses = (await Promise.all(events.map(toStatus))).filter(Boolean);
const link = buildLinkHeader(c.req.url, events);
return c.json(statuses, 200, link ? { link } : undefined);
const statuses = await Promise.all(events.map(toStatus));
return paginated(c, events, statuses);
};
export { hashtagTimelineController, homeTimelineController, publicTimelineController };

View File

@ -89,6 +89,22 @@ function buildLinkHeader(url: string, events: Event[]): string | undefined {
return `<${next}>; rel="next", <${prev}>; rel="prev"`;
}
type Entity = { id: string };
type HeaderRecord = Record<string, string | string[]>;
/** Return results with pagination headers. */
function paginated(c: AppContext, events: Event[], entities: (Entity | undefined)[], headers: HeaderRecord = {}) {
const link = buildLinkHeader(c.req.url, events);
if (link) {
headers.link = link;
}
// Filter out undefined entities.
const results = entities.filter((entity): entity is Entity => Boolean(entity));
return c.json(results, 200, headers);
}
/** JSON-LD context. */
type LDContext = (string | Record<string, string | Record<string, string>>)[];
@ -107,12 +123,4 @@ function activityJson<T, P extends string>(c: Context<any, P>, object: T) {
return response;
}
export {
activityJson,
buildLinkHeader,
createAdminEvent,
createEvent,
type PaginationParams,
paginationSchema,
parseBody,
};
export { activityJson, createAdminEvent, createEvent, paginated, type PaginationParams, paginationSchema, parseBody };