2024-11-01 20:42:32 +01:00
|
|
|
import { apiRoute, applyConfig } from "@/api";
|
2024-09-16 15:29:09 +02:00
|
|
|
import { createRoute } from "@hono/zod-openapi";
|
2024-11-01 20:42:32 +01:00
|
|
|
import type { Entity } from "@versia/federation/types";
|
2024-05-06 09:16:33 +02:00
|
|
|
import { z } from "zod";
|
2024-09-16 15:29:09 +02:00
|
|
|
import { ErrorSchema } from "~/types/api";
|
2024-11-25 21:17:52 +01:00
|
|
|
import { InboxJobType, inboxQueue } from "~/worker";
|
2024-05-06 09:16:33 +02:00
|
|
|
|
|
|
|
|
export const meta = applyConfig({
|
|
|
|
|
auth: {
|
|
|
|
|
required: false,
|
|
|
|
|
},
|
|
|
|
|
ratelimits: {
|
|
|
|
|
duration: 60,
|
|
|
|
|
max: 500,
|
|
|
|
|
},
|
2024-05-17 09:51:49 +02:00
|
|
|
route: "/users/:uuid/inbox",
|
2024-05-06 09:16:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const schemas = {
|
|
|
|
|
param: z.object({
|
|
|
|
|
uuid: z.string().uuid(),
|
|
|
|
|
}),
|
|
|
|
|
header: z.object({
|
2024-11-23 23:02:18 +01:00
|
|
|
"x-signature": z.string().optional(),
|
|
|
|
|
"x-nonce": z.string().optional(),
|
2024-11-24 16:40:23 +01:00
|
|
|
"x-signed-by": z
|
|
|
|
|
.string()
|
|
|
|
|
.url()
|
|
|
|
|
.or(z.string().startsWith("instance "))
|
|
|
|
|
.optional(),
|
2024-05-22 02:59:03 +02:00
|
|
|
authorization: z.string().optional(),
|
2024-05-06 09:16:33 +02:00
|
|
|
}),
|
2024-05-17 10:37:06 +02:00
|
|
|
body: z.any(),
|
2024-05-06 09:16:33 +02:00
|
|
|
};
|
|
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const route = createRoute({
|
|
|
|
|
method: "post",
|
|
|
|
|
path: "/users/{uuid}/inbox",
|
|
|
|
|
summary: "Receive federation inbox",
|
|
|
|
|
request: {
|
|
|
|
|
params: schemas.param,
|
|
|
|
|
headers: schemas.header,
|
|
|
|
|
body: {
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: schemas.body,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "Request processed",
|
|
|
|
|
},
|
|
|
|
|
201: {
|
|
|
|
|
description: "Request accepted",
|
|
|
|
|
},
|
|
|
|
|
400: {
|
|
|
|
|
description: "Bad request",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
401: {
|
|
|
|
|
description: "Signature could not be verified",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
403: {
|
|
|
|
|
description: "Cannot view users from remote instances",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
404: {
|
|
|
|
|
description: "Not found",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
500: {
|
|
|
|
|
description: "Internal server error",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: z.object({
|
|
|
|
|
error: z.string(),
|
|
|
|
|
message: z.string(),
|
2024-05-22 02:59:03 +02:00
|
|
|
}),
|
2024-09-16 15:29:09 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-05-17 19:56:13 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
export default apiRoute((app) =>
|
|
|
|
|
app.openapi(route, async (context) => {
|
|
|
|
|
const body: Entity = await context.req.valid("json");
|
|
|
|
|
|
2024-11-25 21:17:52 +01:00
|
|
|
await inboxQueue.add(InboxJobType.ProcessEntity, {
|
2024-11-24 21:35:59 +01:00
|
|
|
data: body,
|
|
|
|
|
headers: context.req.valid("header"),
|
|
|
|
|
request: {
|
|
|
|
|
body: await context.req.text(),
|
|
|
|
|
method: context.req.method,
|
|
|
|
|
url: context.req.url,
|
2024-10-28 13:13:50 +01:00
|
|
|
},
|
2024-11-24 21:35:59 +01:00
|
|
|
ip: context.env.ip ?? null,
|
|
|
|
|
});
|
2024-10-28 13:13:50 +01:00
|
|
|
|
2024-11-25 21:17:52 +01:00
|
|
|
return context.newResponse(
|
|
|
|
|
"Request processing initiated.\nImplement the Instance Messaging Extension to receive any eventual feedback (errors, etc.)",
|
|
|
|
|
200,
|
|
|
|
|
);
|
2024-11-01 20:42:32 +01:00
|
|
|
}),
|
|
|
|
|
);
|