2024-08-19 20:06:38 +02:00
|
|
|
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
2024-05-06 09:16:33 +02:00
|
|
|
import { zValidator } from "@hono/zod-validator";
|
|
|
|
|
import { z } from "zod";
|
2024-07-27 20:46:19 +02:00
|
|
|
import { RolePermissions } from "~/drizzle/schema";
|
|
|
|
|
import { Relationship } from "~/packages/database-interface/relationship";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { User } from "~/packages/database-interface/user";
|
2024-05-06 09:16:33 +02:00
|
|
|
|
|
|
|
|
export const meta = applyConfig({
|
|
|
|
|
allowedMethods: ["POST"],
|
|
|
|
|
ratelimits: {
|
|
|
|
|
max: 30,
|
|
|
|
|
duration: 60,
|
|
|
|
|
},
|
|
|
|
|
route: "/api/v1/accounts/:id/unpin",
|
|
|
|
|
auth: {
|
|
|
|
|
required: true,
|
|
|
|
|
oauthPermissions: ["write:accounts"],
|
|
|
|
|
},
|
2024-06-08 06:57:29 +02:00
|
|
|
permissions: {
|
|
|
|
|
required: [
|
2024-06-13 04:26:43 +02:00
|
|
|
RolePermissions.ManageOwnAccount,
|
|
|
|
|
RolePermissions.ViewAccounts,
|
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-08-19 20:06:38 +02:00
|
|
|
export default apiRoute((app) =>
|
2024-05-06 09:16:33 +02:00
|
|
|
app.on(
|
|
|
|
|
meta.allowedMethods,
|
|
|
|
|
meta.route,
|
|
|
|
|
zValidator("param", schemas.param, handleZodError),
|
2024-06-08 06:57:29 +02:00
|
|
|
auth(meta.auth, meta.permissions),
|
2024-05-06 09:16:33 +02:00
|
|
|
async (context) => {
|
|
|
|
|
const { id } = context.req.valid("param");
|
2024-08-27 17:20:36 +02:00
|
|
|
const { user: self } = context.get("auth");
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-06-13 04:26:43 +02:00
|
|
|
if (!self) {
|
2024-08-19 21:03:59 +02:00
|
|
|
return context.json({ error: "Unauthorized" }, 401);
|
2024-06-13 04:26:43 +02:00
|
|
|
}
|
2024-05-06 09:16:33 +02:00
|
|
|
|
|
|
|
|
const otherUser = await User.fromId(id);
|
|
|
|
|
|
2024-06-13 04:26:43 +02:00
|
|
|
if (!otherUser) {
|
2024-08-19 21:03:59 +02:00
|
|
|
return context.json({ error: "User not found" }, 404);
|
2024-06-13 04:26:43 +02:00
|
|
|
}
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-07-27 20:46:19 +02:00
|
|
|
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
2024-05-06 09:16:33 +02:00
|
|
|
self,
|
|
|
|
|
otherUser,
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-27 20:46:19 +02:00
|
|
|
if (foundRelationship.data.endorsed) {
|
|
|
|
|
await foundRelationship.update({
|
|
|
|
|
endorsed: false,
|
|
|
|
|
});
|
2024-05-06 09:16:33 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-19 21:03:59 +02:00
|
|
|
return context.json(foundRelationship.toApi());
|
2024-05-06 09:16:33 +02:00
|
|
|
},
|
2024-08-19 20:06:38 +02:00
|
|
|
),
|
|
|
|
|
);
|