2025-02-13 02:34:44 +01:00
|
|
|
import { accountNotFound, apiRoute, auth, reusedResponses } from "@/api";
|
2025-02-05 21:49:39 +01:00
|
|
|
import { createRoute, z } from "@hono/zod-openapi";
|
2025-03-22 02:34:03 +01:00
|
|
|
import {
|
|
|
|
|
Account as AccountSchema,
|
|
|
|
|
Relationship as RelationshipSchema,
|
2025-03-22 18:04:47 +01:00
|
|
|
} from "@versia/client/schemas";
|
|
|
|
|
import { RolePermission } from "@versia/client/schemas";
|
2024-11-01 20:57:16 +01:00
|
|
|
import { Relationship, User } from "@versia/kit/db";
|
2024-12-30 18:00:23 +01:00
|
|
|
import { ApiError } from "~/classes/errors/api-error";
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
const route = createRoute({
|
|
|
|
|
method: "post",
|
|
|
|
|
path: "/api/v1/follow_requests/{account_id}/authorize",
|
2025-02-13 02:34:44 +01:00
|
|
|
summary: "Accept follow request",
|
|
|
|
|
externalDocs: {
|
|
|
|
|
url: "https://docs.joinmastodon.org/methods/follow_requests/#accept",
|
|
|
|
|
},
|
|
|
|
|
tags: ["Follows"],
|
2024-12-30 19:18:31 +01:00
|
|
|
middleware: [
|
|
|
|
|
auth({
|
|
|
|
|
auth: true,
|
2025-03-22 18:04:47 +01:00
|
|
|
permissions: [RolePermission.ManageOwnFollows],
|
2024-12-30 19:18:31 +01:00
|
|
|
}),
|
|
|
|
|
] as const,
|
2024-09-15 14:28:47 +02:00
|
|
|
request: {
|
2025-02-13 02:34:44 +01:00
|
|
|
params: z.object({
|
|
|
|
|
account_id: AccountSchema.shape.id,
|
|
|
|
|
}),
|
2024-09-15 14:28:47 +02:00
|
|
|
},
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
2025-02-13 02:34:44 +01:00
|
|
|
description:
|
|
|
|
|
"Your Relationship with this account should be updated so that you are followed_by this account.",
|
2024-09-15 14:28:47 +02:00
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
2025-02-12 23:25:22 +01:00
|
|
|
schema: RelationshipSchema,
|
2024-09-15 14:28:47 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-02-13 02:34:44 +01:00
|
|
|
404: accountNotFound,
|
|
|
|
|
...reusedResponses,
|
2024-09-15 14:28:47 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-19 20:06:38 +02:00
|
|
|
export default apiRoute((app) =>
|
2024-09-15 14:28:47 +02:00
|
|
|
app.openapi(route, async (context) => {
|
|
|
|
|
const { user } = context.get("auth");
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
const { account_id } = context.req.valid("param");
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
const account = await User.fromId(account_id);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
if (!account) {
|
2024-12-30 18:00:23 +01:00
|
|
|
throw new ApiError(404, "Account not found");
|
2024-09-15 14:28:47 +02:00
|
|
|
}
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
const oppositeRelationship = await Relationship.fromOwnerAndSubject(
|
|
|
|
|
account,
|
|
|
|
|
user,
|
|
|
|
|
);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
await oppositeRelationship.update({
|
|
|
|
|
requested: false,
|
|
|
|
|
following: true,
|
|
|
|
|
});
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
|
|
|
|
user,
|
|
|
|
|
account,
|
|
|
|
|
);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
// Check if accepting remote follow
|
|
|
|
|
if (account.isRemote()) {
|
|
|
|
|
// Federate follow accept
|
2024-11-01 20:42:32 +01:00
|
|
|
await user.sendFollowAccept(account);
|
2024-09-15 14:28:47 +02:00
|
|
|
}
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
return context.json(foundRelationship.toApi(), 200);
|
|
|
|
|
}),
|
2024-08-19 20:06:38 +02:00
|
|
|
);
|