mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { AppMiddleware } from '@/app.ts';
|
|
import { paginationSchema } from '@/schemas/pagination.ts';
|
|
import { Storages } from '@/storages.ts';
|
|
|
|
/** Fixes compatibility with Mastodon apps by that don't use `Link` headers. */
|
|
export const paginationMiddleware: AppMiddleware = async (c, next) => {
|
|
const pagination = paginationSchema.parse(c.req.query());
|
|
|
|
const {
|
|
max_id: maxId,
|
|
min_id: minId,
|
|
since,
|
|
until,
|
|
} = pagination;
|
|
|
|
if ((maxId && !until) || (minId && !since)) {
|
|
const ids: string[] = [];
|
|
|
|
if (maxId) ids.push(maxId);
|
|
if (minId) ids.push(minId);
|
|
|
|
if (ids.length) {
|
|
const store = await Storages.db();
|
|
|
|
const events = await store.query(
|
|
[{ ids, limit: ids.length }],
|
|
{ signal: c.req.raw.signal },
|
|
);
|
|
|
|
for (const event of events) {
|
|
if (!until && maxId === event.id) pagination.until = event.created_at;
|
|
if (!since && minId === event.id) pagination.since = event.created_at;
|
|
}
|
|
}
|
|
}
|
|
|
|
c.set('pagination', {
|
|
since: pagination.since,
|
|
until: pagination.until,
|
|
limit: pagination.limit,
|
|
});
|
|
|
|
c.set('listPagination', {
|
|
limit: pagination.limit,
|
|
offset: pagination.offset,
|
|
});
|
|
|
|
await next();
|
|
};
|