server/api/api/v1/accounts/:id/unpin.ts
2025-03-22 02:34:03 +01:00

75 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
accountNotFound,
apiRoute,
auth,
reusedResponses,
withUserParam,
} from "@/api";
import { createRoute, z } from "@hono/zod-openapi";
import {
Account as AccountSchema,
Relationship as RelationshipSchema,
} from "@versia/client-ng/schemas";
import { Relationship } from "@versia/kit/db";
import { RolePermissions } from "@versia/kit/tables";
const route = createRoute({
method: "post",
path: "/api/v1/accounts/{id}/unpin",
summary: "Unfeature account from profile",
description: "Remove the given account from the users featured profiles.",
externalDocs: {
url: "https://docs.joinmastodon.org/methods/accounts/#unpin",
},
tags: ["Accounts"],
middleware: [
auth({
auth: true,
scopes: ["write:accounts"],
permissions: [
RolePermissions.ManageOwnAccount,
RolePermissions.ViewAccounts,
],
}),
withUserParam,
] as const,
request: {
params: z.object({
id: AccountSchema.shape.id,
}),
},
responses: {
200: {
description:
"Successfully unendorsed, or account was already not endorsed",
content: {
"application/json": {
schema: RelationshipSchema,
},
},
},
404: accountNotFound,
...reusedResponses,
},
});
export default apiRoute((app) =>
app.openapi(route, async (context) => {
const { user } = context.get("auth");
const otherUser = context.get("user");
const foundRelationship = await Relationship.fromOwnerAndSubject(
user,
otherUser,
);
if (foundRelationship.data.endorsed) {
await foundRelationship.update({
endorsed: false,
});
}
return context.json(foundRelationship.toApi(), 200);
}),
);