mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor: 🚚 Organize code into sub-packages, instead of a single large package
This commit is contained in:
parent
79742f47dc
commit
a6d3ebbeef
366 changed files with 942 additions and 833 deletions
79
packages/api/routes/notes/[uuid]/index.ts
Normal file
79
packages/api/routes/notes/[uuid]/index.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import { Status as StatusSchema } from "@versia/client/schemas";
|
||||
import { ApiError } from "@versia/kit";
|
||||
import { Note } from "@versia/kit/db";
|
||||
import { Notes } from "@versia/kit/tables";
|
||||
import { 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";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.get(
|
||||
"/notes/:id",
|
||||
describeRoute({
|
||||
summary: "Retrieve the Versia representation of a note.",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Note",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(NoteSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description:
|
||||
"Entity not found, is remote, or the requester is not allowed to view it.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(ApiError.zodSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
validator(
|
||||
"param",
|
||||
z.object({
|
||||
id: StatusSchema.shape.id,
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
|
||||
const note = await Note.fromSql(
|
||||
and(
|
||||
eq(Notes.id, id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
);
|
||||
|
||||
if (!(note && (await note.isViewableByUser(null))) || note.remote) {
|
||||
throw ApiError.noteNotFound();
|
||||
}
|
||||
|
||||
// If base_url uses https and request uses http, rewrite request to use https
|
||||
// This fixes reverse proxy errors
|
||||
const reqUrl = new URL(context.req.url);
|
||||
if (
|
||||
config.http.base_url.protocol === "https:" &&
|
||||
reqUrl.protocol === "http:"
|
||||
) {
|
||||
reqUrl.protocol = "https:";
|
||||
}
|
||||
|
||||
const { headers } = await note.author.sign(
|
||||
note.toVersia(),
|
||||
reqUrl,
|
||||
"GET",
|
||||
);
|
||||
|
||||
return context.json(note.toVersia(), 200, headers.toJSON());
|
||||
},
|
||||
),
|
||||
);
|
||||
147
packages/api/routes/notes/[uuid]/quotes.ts
Normal file
147
packages/api/routes/notes/[uuid]/quotes.ts
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
import { Status as StatusSchema } from "@versia/client/schemas";
|
||||
import { ApiError } from "@versia/kit";
|
||||
import { db, Note } from "@versia/kit/db";
|
||||
import { Notes } from "@versia/kit/tables";
|
||||
import * as VersiaEntities from "@versia/sdk/entities";
|
||||
import { URICollectionSchema } 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";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.get(
|
||||
"/notes/:id/quotes",
|
||||
describeRoute({
|
||||
summary: "Retrieve all quotes of a Versia Note.",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Note quotes",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(URICollectionSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description:
|
||||
"Entity not found, is remote, or the requester is not allowed to view it.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(ApiError.zodSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
validator(
|
||||
"param",
|
||||
z.object({
|
||||
id: StatusSchema.shape.id,
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
validator(
|
||||
"query",
|
||||
z.object({
|
||||
limit: z.coerce.number().int().min(1).max(100).default(40),
|
||||
offset: z.coerce.number().int().nonnegative().default(0),
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
const { limit, offset } = context.req.valid("query");
|
||||
|
||||
const note = await Note.fromSql(
|
||||
and(
|
||||
eq(Notes.id, id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
);
|
||||
|
||||
if (!(note && (await note.isViewableByUser(null))) || note.remote) {
|
||||
throw ApiError.noteNotFound();
|
||||
}
|
||||
|
||||
const quotes = await Note.manyFromSql(
|
||||
and(
|
||||
eq(Notes.quotingId, note.id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
undefined,
|
||||
limit,
|
||||
offset,
|
||||
);
|
||||
|
||||
const quoteCount = await db.$count(
|
||||
Notes,
|
||||
and(
|
||||
eq(Notes.quotingId, note.id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
);
|
||||
|
||||
const uriCollection = new VersiaEntities.URICollection({
|
||||
author: note.author.uri.href,
|
||||
first: new URL(
|
||||
`/notes/${note.id}/quotes?offset=0`,
|
||||
config.http.base_url,
|
||||
).href,
|
||||
last:
|
||||
quoteCount > limit
|
||||
? new URL(
|
||||
`/notes/${note.id}/quotes?offset=${
|
||||
quoteCount - limit
|
||||
}`,
|
||||
config.http.base_url,
|
||||
).href
|
||||
: new URL(
|
||||
`/notes/${note.id}/quotes`,
|
||||
config.http.base_url,
|
||||
).href,
|
||||
next:
|
||||
offset + limit < quoteCount
|
||||
? new URL(
|
||||
`/notes/${note.id}/quotes?offset=${
|
||||
offset + limit
|
||||
}`,
|
||||
config.http.base_url,
|
||||
).href
|
||||
: null,
|
||||
previous:
|
||||
offset - limit >= 0
|
||||
? new URL(
|
||||
`/notes/${note.id}/quotes?offset=${
|
||||
offset - limit
|
||||
}`,
|
||||
config.http.base_url,
|
||||
).href
|
||||
: null,
|
||||
total: quoteCount,
|
||||
items: quotes.map((reply) => reply.getUri().href),
|
||||
});
|
||||
|
||||
// If base_url uses https and request uses http, rewrite request to use https
|
||||
// This fixes reverse proxy errors
|
||||
const reqUrl = new URL(context.req.url);
|
||||
if (
|
||||
config.http.base_url.protocol === "https:" &&
|
||||
reqUrl.protocol === "http:"
|
||||
) {
|
||||
reqUrl.protocol = "https:";
|
||||
}
|
||||
|
||||
const { headers } = await note.author.sign(
|
||||
uriCollection,
|
||||
reqUrl,
|
||||
"GET",
|
||||
);
|
||||
|
||||
return context.json(uriCollection, 200, headers.toJSON());
|
||||
},
|
||||
),
|
||||
);
|
||||
145
packages/api/routes/notes/[uuid]/replies.ts
Normal file
145
packages/api/routes/notes/[uuid]/replies.ts
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
import { Status as StatusSchema } from "@versia/client/schemas";
|
||||
import { ApiError } from "@versia/kit";
|
||||
import { db, Note } from "@versia/kit/db";
|
||||
import { Notes } from "@versia/kit/tables";
|
||||
import * as VersiaEntities from "@versia/sdk/entities";
|
||||
import { URICollectionSchema } 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";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.get(
|
||||
"/notes/:id/replies",
|
||||
describeRoute({
|
||||
summary: "Retrieve all replies to a Versia Note.",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Note replies",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(URICollectionSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description:
|
||||
"Entity not found, is remote, or the requester is not allowed to view it.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(ApiError.zodSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
validator(
|
||||
"param",
|
||||
z.object({ id: StatusSchema.shape.id }),
|
||||
handleZodError,
|
||||
),
|
||||
validator(
|
||||
"query",
|
||||
z.object({
|
||||
limit: z.coerce.number().int().min(1).max(100).default(40),
|
||||
offset: z.coerce.number().int().nonnegative().default(0),
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
const { limit, offset } = context.req.valid("query");
|
||||
|
||||
const note = await Note.fromSql(
|
||||
and(
|
||||
eq(Notes.id, id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
);
|
||||
|
||||
if (!(note && (await note.isViewableByUser(null))) || note.remote) {
|
||||
throw ApiError.noteNotFound();
|
||||
}
|
||||
|
||||
const replies = await Note.manyFromSql(
|
||||
and(
|
||||
eq(Notes.replyId, note.id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
undefined,
|
||||
limit,
|
||||
offset,
|
||||
);
|
||||
|
||||
const replyCount = await db.$count(
|
||||
Notes,
|
||||
and(
|
||||
eq(Notes.replyId, note.id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
);
|
||||
|
||||
const uriCollection = new VersiaEntities.URICollection({
|
||||
author: note.author.uri.href,
|
||||
first: new URL(
|
||||
`/notes/${note.id}/replies?offset=0`,
|
||||
config.http.base_url,
|
||||
).href,
|
||||
last:
|
||||
replyCount > limit
|
||||
? new URL(
|
||||
`/notes/${note.id}/replies?offset=${
|
||||
replyCount - limit
|
||||
}`,
|
||||
config.http.base_url,
|
||||
).href
|
||||
: new URL(
|
||||
`/notes/${note.id}/replies`,
|
||||
config.http.base_url,
|
||||
).href,
|
||||
next:
|
||||
offset + limit < replyCount
|
||||
? new URL(
|
||||
`/notes/${note.id}/replies?offset=${
|
||||
offset + limit
|
||||
}`,
|
||||
config.http.base_url,
|
||||
).href
|
||||
: null,
|
||||
previous:
|
||||
offset - limit >= 0
|
||||
? new URL(
|
||||
`/notes/${note.id}/replies?offset=${
|
||||
offset - limit
|
||||
}`,
|
||||
config.http.base_url,
|
||||
).href
|
||||
: null,
|
||||
total: replyCount,
|
||||
items: replies.map((reply) => reply.getUri().href),
|
||||
});
|
||||
|
||||
// If base_url uses https and request uses http, rewrite request to use https
|
||||
// This fixes reverse proxy errors
|
||||
const reqUrl = new URL(context.req.url);
|
||||
if (
|
||||
config.http.base_url.protocol === "https:" &&
|
||||
reqUrl.protocol === "http:"
|
||||
) {
|
||||
reqUrl.protocol = "https:";
|
||||
}
|
||||
|
||||
const { headers } = await note.author.sign(
|
||||
uriCollection,
|
||||
reqUrl,
|
||||
"GET",
|
||||
);
|
||||
|
||||
return context.json(uriCollection, 200, headers.toJSON());
|
||||
},
|
||||
),
|
||||
);
|
||||
151
packages/api/routes/notes/[uuid]/shares.ts
Normal file
151
packages/api/routes/notes/[uuid]/shares.ts
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
import { Status as StatusSchema } from "@versia/client/schemas";
|
||||
import { ApiError } from "@versia/kit";
|
||||
import { db, Note } from "@versia/kit/db";
|
||||
import { Notes } from "@versia/kit/tables";
|
||||
import * as VersiaEntities from "@versia/sdk/entities";
|
||||
import { URICollectionSchema } 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";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.get(
|
||||
"/notes/:id/shares",
|
||||
describeRoute({
|
||||
summary: "Retrieve all shares of a Versia Note.",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Note shares",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(URICollectionSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description:
|
||||
"Entity not found, is remote, or the requester is not allowed to view it.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(ApiError.zodSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
validator(
|
||||
"param",
|
||||
z.object({
|
||||
id: StatusSchema.shape.id,
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
validator(
|
||||
"query",
|
||||
z.object({
|
||||
limit: z.coerce.number().int().min(1).max(100).default(40),
|
||||
offset: z.coerce.number().int().nonnegative().default(0),
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
const { limit, offset } = context.req.valid("query");
|
||||
|
||||
const note = await Note.fromSql(
|
||||
and(
|
||||
eq(Notes.id, id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
);
|
||||
|
||||
if (!(note && (await note.isViewableByUser(null))) || note.remote) {
|
||||
throw ApiError.noteNotFound();
|
||||
}
|
||||
|
||||
const shares = await Note.manyFromSql(
|
||||
and(
|
||||
eq(Notes.reblogId, note.id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
undefined,
|
||||
limit,
|
||||
offset,
|
||||
);
|
||||
|
||||
const shareCount = await db.$count(
|
||||
Notes,
|
||||
and(
|
||||
eq(Notes.reblogId, note.id),
|
||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||
),
|
||||
);
|
||||
|
||||
const uriCollection = new VersiaEntities.URICollection({
|
||||
author: note.author.uri.href,
|
||||
first: new URL(
|
||||
`/notes/${note.id}/shares?offset=0`,
|
||||
config.http.base_url,
|
||||
).href,
|
||||
last:
|
||||
shareCount > limit
|
||||
? new URL(
|
||||
`/notes/${note.id}/shares?offset=${
|
||||
shareCount - limit
|
||||
}`,
|
||||
config.http.base_url,
|
||||
).href
|
||||
: new URL(
|
||||
`/notes/${note.id}/shares`,
|
||||
config.http.base_url,
|
||||
).href,
|
||||
next:
|
||||
offset + limit < shareCount
|
||||
? new URL(
|
||||
`/notes/${note.id}/shares?offset=${
|
||||
offset + limit
|
||||
}`,
|
||||
config.http.base_url,
|
||||
).href
|
||||
: null,
|
||||
previous:
|
||||
offset - limit >= 0
|
||||
? new URL(
|
||||
`/notes/${note.id}/shares?offset=${
|
||||
offset - limit
|
||||
}`,
|
||||
config.http.base_url,
|
||||
).href
|
||||
: null,
|
||||
total: shareCount,
|
||||
items: shares.map(
|
||||
(share) =>
|
||||
new URL(`/shares/${share.id}`, config.http.base_url)
|
||||
.href,
|
||||
),
|
||||
});
|
||||
|
||||
// If base_url uses https and request uses http, rewrite request to use https
|
||||
// This fixes reverse proxy errors
|
||||
const reqUrl = new URL(context.req.url);
|
||||
if (
|
||||
config.http.base_url.protocol === "https:" &&
|
||||
reqUrl.protocol === "http:"
|
||||
) {
|
||||
reqUrl.protocol = "https:";
|
||||
}
|
||||
|
||||
const { headers } = await note.author.sign(
|
||||
uriCollection,
|
||||
reqUrl,
|
||||
"GET",
|
||||
);
|
||||
|
||||
return context.json(uriCollection, 200, headers.toJSON());
|
||||
},
|
||||
),
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue