2024-05-29 02:59:49 +02:00
|
|
|
import { applyConfig, handleZodError } from "@/api";
|
|
|
|
|
import { errorResponse, jsonResponse } from "@/response";
|
2024-07-11 12:56:28 +02:00
|
|
|
import type { Hono } from "@hono/hono";
|
2024-05-06 09:16:33 +02:00
|
|
|
import { zValidator } from "@hono/zod-validator";
|
|
|
|
|
import { and, count, eq, inArray } from "drizzle-orm";
|
|
|
|
|
import { z } from "zod";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { db } from "~/drizzle/db";
|
|
|
|
|
import { Notes } from "~/drizzle/schema";
|
|
|
|
|
import { config } from "~/packages/config-manager";
|
|
|
|
|
import { Note } from "~/packages/database-interface/note";
|
|
|
|
|
import { User } from "~/packages/database-interface/user";
|
2024-05-06 09:16:33 +02:00
|
|
|
|
|
|
|
|
export const meta = applyConfig({
|
|
|
|
|
allowedMethods: ["GET"],
|
|
|
|
|
auth: {
|
|
|
|
|
required: false,
|
|
|
|
|
},
|
|
|
|
|
ratelimits: {
|
|
|
|
|
duration: 60,
|
|
|
|
|
max: 500,
|
|
|
|
|
},
|
|
|
|
|
route: "/users/:uuid/outbox",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const schemas = {
|
|
|
|
|
param: z.object({
|
|
|
|
|
uuid: z.string().uuid(),
|
|
|
|
|
}),
|
|
|
|
|
query: z.object({
|
|
|
|
|
page: z.string().optional(),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-17 19:39:59 +02:00
|
|
|
const NOTES_PER_PAGE = 20;
|
|
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
export default (app: Hono) =>
|
|
|
|
|
app.on(
|
|
|
|
|
meta.allowedMethods,
|
|
|
|
|
meta.route,
|
|
|
|
|
zValidator("param", schemas.param, handleZodError),
|
|
|
|
|
zValidator("query", schemas.query, handleZodError),
|
|
|
|
|
async (context) => {
|
|
|
|
|
const { uuid } = context.req.valid("param");
|
|
|
|
|
|
2024-05-17 19:39:59 +02:00
|
|
|
const author = await User.fromId(uuid);
|
|
|
|
|
|
|
|
|
|
if (!author) {
|
|
|
|
|
return errorResponse("User not found", 404);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-20 00:17:35 +02:00
|
|
|
if (author.isRemote()) {
|
|
|
|
|
return errorResponse(
|
|
|
|
|
"Cannot view users from remote instances",
|
|
|
|
|
403,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
const pageNumber = Number(context.req.valid("query").page) || 1;
|
|
|
|
|
|
|
|
|
|
const notes = await Note.manyFromSql(
|
|
|
|
|
and(
|
|
|
|
|
eq(Notes.authorId, uuid),
|
|
|
|
|
inArray(Notes.visibility, ["public", "unlisted"]),
|
|
|
|
|
),
|
|
|
|
|
undefined,
|
2024-05-17 19:39:59 +02:00
|
|
|
NOTES_PER_PAGE,
|
|
|
|
|
NOTES_PER_PAGE * (pageNumber - 1),
|
2024-05-06 09:16:33 +02:00
|
|
|
);
|
|
|
|
|
|
2024-05-17 19:39:59 +02:00
|
|
|
const totalNotes = (
|
|
|
|
|
await db
|
|
|
|
|
.select({
|
|
|
|
|
count: count(),
|
|
|
|
|
})
|
|
|
|
|
.from(Notes)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(Notes.authorId, uuid),
|
|
|
|
|
inArray(Notes.visibility, ["public", "unlisted"]),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
)[0].count;
|
2024-05-06 09:16:33 +02:00
|
|
|
|
|
|
|
|
return jsonResponse({
|
2024-05-17 19:39:59 +02:00
|
|
|
first: new URL(
|
|
|
|
|
`/users/${uuid}/outbox?page=1`,
|
2024-05-06 09:16:33 +02:00
|
|
|
config.http.base_url,
|
|
|
|
|
).toString(),
|
2024-05-17 19:39:59 +02:00
|
|
|
last: new URL(
|
|
|
|
|
`/users/${uuid}/outbox?page=${Math.ceil(
|
|
|
|
|
totalNotes / NOTES_PER_PAGE,
|
|
|
|
|
)}`,
|
|
|
|
|
config.http.base_url,
|
|
|
|
|
).toString(),
|
|
|
|
|
total_items: totalNotes,
|
|
|
|
|
// Server actor
|
|
|
|
|
author: author.getUri(),
|
2024-05-06 09:16:33 +02:00
|
|
|
next:
|
2024-05-17 19:39:59 +02:00
|
|
|
notes.length === NOTES_PER_PAGE
|
2024-05-06 09:16:33 +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()
|
|
|
|
|
: undefined,
|
|
|
|
|
items: notes.map((note) => note.toLysand()),
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|