mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
80 lines
2 KiB
TypeScript
80 lines
2 KiB
TypeScript
import { apiRoute, applyConfig, auth } from "@/api";
|
|
import { createRoute } from "@hono/zod-openapi";
|
|
import { Notification } from "@versia/kit/db";
|
|
import { RolePermissions } from "@versia/kit/tables";
|
|
import { z } from "zod";
|
|
import { ApiError } from "~/classes/errors/api-error";
|
|
import { ErrorSchema } from "~/types/api";
|
|
|
|
export const meta = applyConfig({
|
|
route: "/api/v1/notifications/:id",
|
|
ratelimits: {
|
|
max: 100,
|
|
duration: 60,
|
|
},
|
|
auth: {
|
|
required: true,
|
|
oauthPermissions: ["read:notifications"],
|
|
},
|
|
permissions: {
|
|
required: [RolePermissions.ManageOwnNotifications],
|
|
},
|
|
});
|
|
|
|
export const schemas = {
|
|
param: z.object({
|
|
id: z.string().uuid(),
|
|
}),
|
|
};
|
|
|
|
const route = createRoute({
|
|
method: "get",
|
|
path: "/api/v1/notifications/{id}",
|
|
summary: "Get notification",
|
|
middleware: [auth(meta.auth, meta.permissions)],
|
|
request: {
|
|
params: schemas.param,
|
|
},
|
|
responses: {
|
|
200: {
|
|
description: "Notification",
|
|
schema: Notification.schema,
|
|
},
|
|
401: {
|
|
description: "Unauthorized",
|
|
content: {
|
|
"application/json": {
|
|
schema: ErrorSchema,
|
|
},
|
|
},
|
|
},
|
|
404: {
|
|
description: "Notification not found",
|
|
content: {
|
|
"application/json": {
|
|
schema: ErrorSchema,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
export default apiRoute((app) =>
|
|
app.openapi(route, async (context) => {
|
|
const { id } = context.req.valid("param");
|
|
|
|
const { user } = context.get("auth");
|
|
if (!user) {
|
|
throw new ApiError(401, "Unauthorized");
|
|
}
|
|
|
|
const notification = await Notification.fromId(id, user.id);
|
|
|
|
if (!notification) {
|
|
throw new ApiError(404, "Notification not found");
|
|
}
|
|
|
|
return context.json(await notification.toApi(), 200);
|
|
}),
|
|
);
|