Ensure bookmarks are displayed reverse-chronologically

This commit is contained in:
Alex Gleason 2024-01-01 14:51:04 -06:00
parent 5f6715db46
commit 9841d193eb
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 11 additions and 7 deletions

View File

@ -119,9 +119,10 @@ function buildLinkHeader(url: string, events: Event[]): string | undefined {
const firstEvent = events[0];
const lastEvent = events[events.length - 1];
const { localDomain } = Conf;
const { pathname, search } = new URL(url);
const next = new URL(pathname + search, Conf.localDomain);
const prev = new URL(pathname + search, Conf.localDomain);
const next = new URL(pathname + search, localDomain);
const prev = new URL(pathname + search, localDomain);
next.searchParams.set('until', String(lastEvent.created_at));
prev.searchParams.set('since', String(firstEvent.created_at));
@ -132,7 +133,7 @@ function buildLinkHeader(url: string, events: Event[]): string | undefined {
type Entity = { id: string };
type HeaderRecord = Record<string, string | string[]>;
/** Return results with pagination headers. */
/** Return results with pagination headers. Assumes chronological sorting of events. */
function paginated(c: AppContext, events: Event[], entities: (Entity | undefined)[], headers: HeaderRecord = {}) {
const link = buildLinkHeader(c.req.url, events);

View File

@ -47,10 +47,10 @@ async function renderAccounts(c: AppContext, authors: string[], signal = AbortSi
/** Render statuses by event IDs. */
async function renderStatuses(c: AppContext, ids: string[], signal = AbortSignal.timeout(1000)) {
const { since, until, limit } = paginationSchema.parse(c.req.query());
const { limit } = paginationSchema.parse(c.req.query());
const events = await eventsDB.getEvents(
[{ kinds: [1], ids, relations: ['author', 'event_stats', 'author_stats'], since, until, limit }],
[{ kinds: [1], ids, relations: ['author', 'event_stats', 'author_stats'], limit }],
{ signal },
);
@ -58,11 +58,14 @@ async function renderStatuses(c: AppContext, ids: string[], signal = AbortSignal
return c.json([]);
}
const sortedEvents = [...events].sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id));
const statuses = await Promise.all(
events.map((event) => renderStatus(event, c.get('pubkey'))),
sortedEvents.map((event) => renderStatus(event, c.get('pubkey'))),
);
return paginated(c, events, statuses);
// TODO: pagination with min_id and max_id based on the order of `ids`.
return c.json(statuses);
}
export { renderAccounts, renderEventAccounts, renderStatuses };