refactor: 🚚 Organize code into sub-packages, instead of a single large package

This commit is contained in:
Jesse Wierzbinski 2025-06-15 04:38:20 +02:00
parent 79742f47dc
commit a6d3ebbeef
No known key found for this signature in database
366 changed files with 942 additions and 833 deletions

View file

@ -0,0 +1,120 @@
import { ApiError } from "@versia/kit";
import { describeRoute } from "hono-openapi";
import { resolver, validator } from "hono-openapi/zod";
import { z } from "zod";
import { apiRoute, handleZodError } from "@/api";
import { InboxJobType, inboxQueue } from "~/classes/queues/inbox";
export default apiRoute((app) =>
app.post(
"/users/:uuid/inbox",
describeRoute({
summary: "Receive federation inbox",
tags: ["Federation"],
responses: {
200: {
description: "Request processed",
},
201: {
description: "Request accepted",
},
400: {
description: "Bad request",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
401: {
description: "Signature could not be verified",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
404: {
description: "Not found",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
500: {
description: "Internal server error",
content: {
"application/json": {
schema: resolver(
z.object({
error: z.string(),
message: z.string(),
}),
),
},
},
},
},
}),
validator(
"param",
z.object({
uuid: z.string().uuid(),
}),
handleZodError,
),
validator(
"header",
z.object({
"versia-signature": z.string().optional(),
"versia-signed-at": z.coerce.number().optional(),
"versia-signed-by": z
.string()
.url()
.or(z.string().startsWith("instance "))
.optional(),
authorization: z.string().optional(),
}),
handleZodError,
),
async (context) => {
const body = await context.req.json();
const {
"versia-signature": signature,
"versia-signed-at": signedAt,
"versia-signed-by": signedBy,
authorization,
} = context.req.valid("header");
await inboxQueue.add(InboxJobType.ProcessEntity, {
data: body,
headers: {
"versia-signature": signature,
"versia-signed-at": signedAt,
"versia-signed-by": signedBy,
authorization,
},
request: {
body: await context.req.text(),
method: context.req.method,
url: context.req.url,
},
ip: context.env.ip ?? null,
});
return context.body(
"Request processing initiated.\nImplement the Instance Messaging Extension to receive any eventual feedback (errors, etc.)",
200,
);
},
),
);

View file

@ -0,0 +1,76 @@
import { ApiError } from "@versia/kit";
import { User } from "@versia/kit/db";
import { UserSchema } from "@versia/sdk/schemas";
import { describeRoute } from "hono-openapi";
import { resolver, validator } from "hono-openapi/zod";
import { z } from "zod";
import { apiRoute, handleZodError } from "@/api";
export default apiRoute((app) =>
app.get(
"/users/:uuid",
describeRoute({
summary: "Get user data",
tags: ["Federation"],
responses: {
200: {
description: "User data",
content: {
"application/json": {
schema: resolver(UserSchema),
},
},
},
301: {
description:
"Redirect to user profile (for web browsers). Uses user-agent for detection.",
},
404: ApiError.accountNotFound().schema,
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
},
}),
validator(
"param",
z.object({
uuid: z.string().uuid(),
}),
handleZodError,
),
// @ts-expect-error idk why this is happening and I don't care
async (context) => {
const { uuid } = context.req.valid("param");
const user = await User.fromId(uuid);
if (!user) {
throw ApiError.accountNotFound();
}
if (user.remote) {
throw new ApiError(403, "User is not on this instance");
}
// Try to detect a web browser and redirect to the user's profile page
if (context.req.header("user-agent")?.includes("Mozilla")) {
return context.redirect(user.toApi().url);
}
const userJson = user.toVersia();
const { headers } = await user.sign(
userJson,
new URL(context.req.url),
"GET",
);
return context.json(userJson, 200, headers.toJSON());
},
),
);

View file

@ -0,0 +1,138 @@
import { ApiError } from "@versia/kit";
import { db, Note, User } from "@versia/kit/db";
import { Notes } from "@versia/kit/tables";
import * as VersiaEntities from "@versia/sdk/entities";
import { CollectionSchema, NoteSchema } from "@versia/sdk/schemas";
import { config } from "@versia-server/config";
import { and, eq, inArray } from "drizzle-orm";
import { describeRoute } from "hono-openapi";
import { resolver, validator } from "hono-openapi/zod";
import { z } from "zod";
import { apiRoute, handleZodError } from "@/api";
const NOTES_PER_PAGE = 20;
export default apiRoute((app) =>
app.get(
"/users/:uuid/outbox",
describeRoute({
summary: "Get user outbox",
tags: ["Federation"],
responses: {
200: {
description: "User outbox",
content: {
"application/json": {
schema: resolver(
CollectionSchema.extend({
items: z.array(NoteSchema),
}),
),
},
},
},
404: {
description: "User not found",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
403: {
description: "Cannot view users from remote instances",
content: {
"application/json": {
schema: resolver(ApiError.zodSchema),
},
},
},
},
}),
validator(
"param",
z.object({
uuid: z.string().uuid(),
}),
handleZodError,
),
validator(
"query",
z.object({
page: z.string().optional(),
}),
handleZodError,
),
async (context) => {
const { uuid } = context.req.valid("param");
const author = await User.fromId(uuid);
if (!author) {
throw new ApiError(404, "User not found");
}
if (author.remote) {
throw new ApiError(403, "User is not on this instance");
}
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,
NOTES_PER_PAGE,
NOTES_PER_PAGE * (pageNumber - 1),
);
const totalNotes = await db.$count(
Notes,
and(
eq(Notes.authorId, uuid),
inArray(Notes.visibility, ["public", "unlisted"]),
),
);
const json = new VersiaEntities.Collection({
first: new URL(
`/users/${uuid}/outbox?page=1`,
config.http.base_url,
).href,
last: new URL(
`/users/${uuid}/outbox?page=${Math.ceil(
totalNotes / NOTES_PER_PAGE,
)}`,
config.http.base_url,
).href,
total: totalNotes,
author: author.uri.href,
next:
notes.length === NOTES_PER_PAGE
? new URL(
`/users/${uuid}/outbox?page=${pageNumber + 1}`,
config.http.base_url,
).href
: null,
previous:
pageNumber > 1
? new URL(
`/users/${uuid}/outbox?page=${pageNumber - 1}`,
config.http.base_url,
).href
: null,
items: notes.map((note) => note.toVersia()),
});
const { headers } = await author.sign(
json,
new URL(context.req.url),
"GET",
);
return context.json(json, 200, headers.toJSON());
},
),
);