2024-11-25 13:09:28 +01:00
|
|
|
import { createBullBoard } from "@bull-board/api";
|
|
|
|
|
import { BullMQAdapter } from "@bull-board/api/bullMQAdapter";
|
|
|
|
|
import { HonoAdapter } from "@bull-board/hono";
|
2025-07-07 05:52:11 +02:00
|
|
|
import { RolePermission } from "@versia/client/schemas";
|
2025-06-15 04:38:20 +02:00
|
|
|
import { config } from "@versia-server/config";
|
2025-07-07 05:52:11 +02:00
|
|
|
import { ApiError } from "@versia-server/kit";
|
|
|
|
|
import { User } from "@versia-server/kit/db";
|
2025-06-29 22:56:52 +02:00
|
|
|
import { deliveryQueue } from "@versia-server/kit/queues/delivery";
|
|
|
|
|
import { fetchQueue } from "@versia-server/kit/queues/fetch";
|
|
|
|
|
import { inboxQueue } from "@versia-server/kit/queues/inbox";
|
|
|
|
|
import { mediaQueue } from "@versia-server/kit/queues/media";
|
|
|
|
|
import { pushQueue } from "@versia-server/kit/queues/push";
|
|
|
|
|
import { relationshipQueue } from "@versia-server/kit/queues/relationships";
|
2025-03-29 03:30:06 +01:00
|
|
|
import type { Hono } from "hono";
|
2024-12-18 20:42:40 +01:00
|
|
|
import { serveStatic } from "hono/bun";
|
2025-07-07 05:52:11 +02:00
|
|
|
import { getCookie } from "hono/cookie";
|
2025-08-21 00:45:58 +02:00
|
|
|
import { verify } from "hono/jwt";
|
2025-11-21 08:31:02 +01:00
|
|
|
import type { HonoEnv } from "~/types/api.ts";
|
2025-06-29 22:56:52 +02:00
|
|
|
import pkg from "../package.json" with { type: "json" };
|
2024-11-25 13:09:28 +01:00
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
export const applyToHono = (app: Hono<HonoEnv>): void => {
|
2024-11-25 13:09:28 +01:00
|
|
|
const serverAdapter = new HonoAdapter(serveStatic);
|
|
|
|
|
|
|
|
|
|
createBullBoard({
|
|
|
|
|
queues: [
|
|
|
|
|
new BullMQAdapter(inboxQueue),
|
|
|
|
|
new BullMQAdapter(deliveryQueue),
|
2024-11-25 20:29:59 +01:00
|
|
|
new BullMQAdapter(fetchQueue),
|
2025-01-02 03:27:26 +01:00
|
|
|
new BullMQAdapter(pushQueue),
|
2025-01-06 19:21:57 +01:00
|
|
|
new BullMQAdapter(mediaQueue),
|
2025-03-30 20:54:47 +02:00
|
|
|
new BullMQAdapter(relationshipQueue),
|
2024-11-25 13:09:28 +01:00
|
|
|
],
|
|
|
|
|
serverAdapter,
|
|
|
|
|
options: {
|
|
|
|
|
uiConfig: {
|
|
|
|
|
boardTitle: "Server Queues",
|
|
|
|
|
favIcon: {
|
|
|
|
|
default: "/favicon.png",
|
|
|
|
|
alternative: "/favicon.ico",
|
|
|
|
|
},
|
|
|
|
|
boardLogo: {
|
2025-02-15 02:47:29 +01:00
|
|
|
path: config.instance.branding.logo?.origin ?? pkg.icon,
|
2024-11-25 13:09:28 +01:00
|
|
|
height: 40,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-04-07 21:50:43 +02:00
|
|
|
uiBasePath: "node_modules/@bull-board/ui",
|
2024-11-25 13:09:28 +01:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
serverAdapter.setBasePath("/admin/queues");
|
|
|
|
|
app.route("/admin/queues", serverAdapter.registerPlugin());
|
2025-07-07 05:52:11 +02:00
|
|
|
|
|
|
|
|
app.use("/admin/queues/api/*", async (context, next) => {
|
|
|
|
|
const jwtCookie = getCookie(context, "jwt");
|
|
|
|
|
|
|
|
|
|
if (!jwtCookie) {
|
|
|
|
|
throw new ApiError(401, "Missing JWT cookie");
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 01:15:38 +02:00
|
|
|
const result = await verify(jwtCookie, config.authentication.key);
|
2025-07-07 05:52:11 +02:00
|
|
|
|
2025-08-21 00:45:58 +02:00
|
|
|
const { sub } = result;
|
2025-07-07 05:52:11 +02:00
|
|
|
|
|
|
|
|
if (!sub) {
|
|
|
|
|
throw new ApiError(401, "Invalid JWT (no sub)");
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 00:45:58 +02:00
|
|
|
const user = await User.fromId(sub as string);
|
2025-07-07 05:52:11 +02:00
|
|
|
|
|
|
|
|
if (!user?.hasPermission(RolePermission.ManageInstanceFederation)) {
|
|
|
|
|
throw new ApiError(
|
|
|
|
|
403,
|
|
|
|
|
`Missing '${RolePermission.ManageInstanceFederation}' permission`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await next();
|
|
|
|
|
});
|
2024-11-25 13:09:28 +01:00
|
|
|
};
|