2024-12-30 20:18:48 +01:00
|
|
|
import { apiRoute } from "@/api";
|
2025-02-05 21:49:39 +01:00
|
|
|
import { createRoute, z } from "@hono/zod-openapi";
|
2024-09-16 15:29:09 +02:00
|
|
|
import {
|
|
|
|
|
Collection as CollectionSchema,
|
|
|
|
|
Note as NoteSchema,
|
|
|
|
|
} from "@versia/federation/schemas";
|
2024-11-01 21:05:54 +01:00
|
|
|
import { Note, User, db } from "@versia/kit/db";
|
|
|
|
|
import { Notes } from "@versia/kit/tables";
|
2024-10-11 15:46:05 +02:00
|
|
|
import { and, eq, inArray } from "drizzle-orm";
|
2024-12-30 18:00:23 +01:00
|
|
|
import { ApiError } from "~/classes/errors/api-error";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { config } from "~/packages/config-manager";
|
2024-09-16 15:29:09 +02:00
|
|
|
import { ErrorSchema } from "~/types/api";
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-12-30 20:26:56 +01:00
|
|
|
const schemas = {
|
2024-05-06 09:16:33 +02:00
|
|
|
param: z.object({
|
|
|
|
|
uuid: z.string().uuid(),
|
|
|
|
|
}),
|
|
|
|
|
query: z.object({
|
|
|
|
|
page: z.string().optional(),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const route = createRoute({
|
|
|
|
|
method: "get",
|
|
|
|
|
path: "/users/{uuid}/outbox",
|
|
|
|
|
summary: "Get user outbox",
|
|
|
|
|
request: {
|
|
|
|
|
params: schemas.param,
|
|
|
|
|
query: schemas.query,
|
|
|
|
|
},
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "User outbox",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: CollectionSchema.extend({
|
|
|
|
|
items: z.array(NoteSchema),
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
404: {
|
|
|
|
|
description: "User not found",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
403: {
|
|
|
|
|
description: "Cannot view users from remote instances",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-17 19:39:59 +02:00
|
|
|
const NOTES_PER_PAGE = 20;
|
|
|
|
|
|
2024-08-19 20:06:38 +02:00
|
|
|
export default apiRoute((app) =>
|
2024-09-16 15:29:09 +02:00
|
|
|
app.openapi(route, async (context) => {
|
|
|
|
|
const { uuid } = context.req.valid("param");
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const author = await User.fromId(uuid);
|
2024-05-17 19:39:59 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
if (!author) {
|
2024-12-30 18:00:23 +01:00
|
|
|
throw new ApiError(404, "User not found");
|
2024-09-16 15:29:09 +02:00
|
|
|
}
|
2024-05-17 19:39:59 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
if (author.isRemote()) {
|
2024-12-30 18:00:23 +01:00
|
|
|
throw new ApiError(403, "User is not on this instance");
|
2024-09-16 15:29:09 +02:00
|
|
|
}
|
2024-07-20 00:17:35 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const pageNumber = Number(context.req.valid("query").page) || 1;
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const notes = await Note.manyFromSql(
|
|
|
|
|
and(
|
|
|
|
|
eq(Notes.authorId, uuid),
|
|
|
|
|
inArray(Notes.visibility, ["public", "unlisted"]),
|
|
|
|
|
),
|
|
|
|
|
undefined,
|
|
|
|
|
NOTES_PER_PAGE,
|
|
|
|
|
NOTES_PER_PAGE * (pageNumber - 1),
|
|
|
|
|
);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-10-11 15:46:05 +02:00
|
|
|
const totalNotes = await db.$count(
|
|
|
|
|
Notes,
|
|
|
|
|
and(
|
|
|
|
|
eq(Notes.authorId, uuid),
|
|
|
|
|
inArray(Notes.visibility, ["public", "unlisted"]),
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const json = {
|
|
|
|
|
first: new URL(
|
|
|
|
|
`/users/${uuid}/outbox?page=1`,
|
|
|
|
|
config.http.base_url,
|
|
|
|
|
).toString(),
|
|
|
|
|
last: new URL(
|
|
|
|
|
`/users/${uuid}/outbox?page=${Math.ceil(
|
|
|
|
|
totalNotes / NOTES_PER_PAGE,
|
|
|
|
|
)}`,
|
|
|
|
|
config.http.base_url,
|
|
|
|
|
).toString(),
|
|
|
|
|
total: totalNotes,
|
2025-02-01 16:32:18 +01:00
|
|
|
author: author.getUri().toString(),
|
2024-09-16 15:29:09 +02:00
|
|
|
next:
|
|
|
|
|
notes.length === NOTES_PER_PAGE
|
|
|
|
|
? new URL(
|
|
|
|
|
`/users/${uuid}/outbox?page=${pageNumber + 1}`,
|
|
|
|
|
config.http.base_url,
|
|
|
|
|
).toString()
|
|
|
|
|
: null,
|
|
|
|
|
previous:
|
|
|
|
|
pageNumber > 1
|
|
|
|
|
? new URL(
|
|
|
|
|
`/users/${uuid}/outbox?page=${pageNumber - 1}`,
|
|
|
|
|
config.http.base_url,
|
|
|
|
|
).toString()
|
|
|
|
|
: null,
|
|
|
|
|
items: notes.map((note) => note.toVersia()),
|
|
|
|
|
};
|
2024-08-26 19:27:40 +02:00
|
|
|
|
2025-02-01 16:32:18 +01:00
|
|
|
const { headers } = await author.sign(
|
|
|
|
|
json,
|
|
|
|
|
new URL(context.req.url),
|
|
|
|
|
"GET",
|
|
|
|
|
);
|
2024-08-26 19:27:40 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
return context.json(json, 200, headers.toJSON());
|
|
|
|
|
}),
|
2024-08-19 20:06:38 +02:00
|
|
|
);
|