diff --git a/src/utils/web.ts b/src/utils/web.ts index 837c4be..a782299 100644 --- a/src/utils/web.ts +++ b/src/utils/web.ts @@ -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; -/** 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); diff --git a/src/views.ts b/src/views.ts index 03de7d7..7c22170 100644 --- a/src/views.ts +++ b/src/views.ts @@ -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 };