2024-09-27 13:00:12 +02:00
|
|
|
import { apiRoute, applyConfig, auth } from "@/api";
|
|
|
|
|
import { createRoute } from "@hono/zod-openapi";
|
2024-05-06 09:16:33 +02:00
|
|
|
import { z } from "zod";
|
2024-10-24 16:28:38 +02:00
|
|
|
import { Note } from "~/classes/database/note";
|
2024-06-08 06:57:29 +02:00
|
|
|
import { RolePermissions } from "~/drizzle/schema";
|
2024-09-27 13:00:12 +02:00
|
|
|
import { ErrorSchema } from "~/types/api";
|
2024-05-06 09:16:33 +02:00
|
|
|
|
|
|
|
|
export const meta = applyConfig({
|
|
|
|
|
ratelimits: {
|
|
|
|
|
max: 100,
|
|
|
|
|
duration: 60,
|
|
|
|
|
},
|
|
|
|
|
route: "/api/v1/statuses/:id/unpin",
|
|
|
|
|
auth: {
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2024-06-08 06:57:29 +02:00
|
|
|
permissions: {
|
2024-06-13 04:26:43 +02:00
|
|
|
required: [RolePermissions.ManageOwnNotes, RolePermissions.ViewNotes],
|
2024-06-08 06:57:29 +02:00
|
|
|
},
|
2024-05-06 09:16:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const schemas = {
|
|
|
|
|
param: z.object({
|
|
|
|
|
id: z.string().uuid(),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-27 13:00:12 +02:00
|
|
|
const route = createRoute({
|
|
|
|
|
method: "post",
|
|
|
|
|
path: "/api/v1/statuses/{id}/unpin",
|
|
|
|
|
summary: "Unpin a status",
|
|
|
|
|
middleware: [auth(meta.auth, meta.permissions)],
|
|
|
|
|
request: {
|
|
|
|
|
params: schemas.param,
|
|
|
|
|
},
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "Unpinned status",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: Note.schema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
401: {
|
|
|
|
|
description: "Unauthorized",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
404: {
|
|
|
|
|
description: "Record not found",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-19 20:06:38 +02:00
|
|
|
export default apiRoute((app) =>
|
2024-09-27 13:00:12 +02:00
|
|
|
app.openapi(route, async (context) => {
|
|
|
|
|
const { id } = context.req.valid("param");
|
|
|
|
|
const { user } = context.get("auth");
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-27 13:00:12 +02:00
|
|
|
if (!user) {
|
|
|
|
|
return context.json({ error: "Unauthorized" }, 401);
|
|
|
|
|
}
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-27 13:00:12 +02:00
|
|
|
const status = await Note.fromId(id, user.id);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-27 13:00:12 +02:00
|
|
|
if (!status) {
|
|
|
|
|
return context.json({ error: "Record not found" }, 404);
|
|
|
|
|
}
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-27 13:00:12 +02:00
|
|
|
if (status.author.id !== user.id) {
|
|
|
|
|
return context.json({ error: "Unauthorized" }, 401);
|
|
|
|
|
}
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-27 13:00:12 +02:00
|
|
|
await user.unpin(status);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-27 13:00:12 +02:00
|
|
|
if (!status) {
|
|
|
|
|
return context.json({ error: "Record not found" }, 404);
|
|
|
|
|
}
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-27 13:00:12 +02:00
|
|
|
return context.json(await status.toApi(user), 200);
|
|
|
|
|
}),
|
2024-08-19 20:06:38 +02:00
|
|
|
);
|