server/server/api/users/[uuid]/outbox/index.ts

71 lines
2.1 KiB
TypeScript
Raw Normal View History

import { apiRoute, applyConfig } from "@api";
2024-04-07 07:30:49 +02:00
import { jsonResponse } from "@response";
2024-04-14 02:07:05 +02:00
import { and, count, eq, inArray } from "drizzle-orm";
import { db } from "~drizzle/db";
import { Notes } from "~drizzle/schema";
import { Note } from "~packages/database-interface/note";
export const meta = applyConfig({
2024-04-07 07:30:49 +02:00
allowedMethods: ["GET"],
auth: {
required: false,
},
ratelimits: {
duration: 60,
max: 500,
},
route: "/users/:uuid/outbox",
});
export default apiRoute(async (req, matchedRoute, extraData) => {
2024-04-07 07:30:49 +02:00
const uuid = matchedRoute.params.uuid;
const pageNumber = Number(matchedRoute.query.page) || 1;
const config = await extraData.configManager.getConfig();
const host = new URL(config.http.base_url).hostname;
const notes = await Note.manyFromSql(
and(
eq(Notes.authorId, uuid),
inArray(Notes.visibility, ["public", "unlisted"]),
),
undefined,
20,
20 * (pageNumber - 1),
);
const totalNotes = await db
2024-04-14 02:07:05 +02:00
.select({
count: count(),
})
.from(Notes)
2024-04-14 02:07:05 +02:00
.where(
and(
eq(Notes.authorId, uuid),
inArray(Notes.visibility, ["public", "unlisted"]),
2024-04-14 02:07:05 +02:00
),
);
2024-04-07 07:30:49 +02:00
return jsonResponse({
first: `${host}/users/${uuid}/outbox?page=1`,
last: `${host}/users/${uuid}/outbox?page=1`,
total_items: totalNotes,
2024-04-07 07:30:49 +02:00
// Server actor
2024-04-08 05:28:18 +02:00
author: new URL("/users/actor", config.http.base_url).toString(),
2024-04-08 05:30:11 +02:00
next:
notes.length === 20
2024-04-08 05:30:11 +02:00
? new URL(
`/users/${uuid}/outbox?page=${pageNumber + 1}`,
config.http.base_url,
).toString()
: undefined,
prev:
pageNumber > 1
? new URL(
`/users/${uuid}/outbox?page=${pageNumber - 1}`,
config.http.base_url,
).toString()
2024-04-07 07:30:49 +02:00
: undefined,
items: notes.map((note) => note.toLysand()),
2024-04-07 07:30:49 +02:00
});
});