server/api/api/v1/accounts/:id/refetch.ts
2025-03-22 18:04:47 +01:00

68 lines
1.7 KiB
TypeScript

import {
accountNotFound,
apiRoute,
auth,
reusedResponses,
withUserParam,
} from "@/api";
import { createRoute, z } from "@hono/zod-openapi";
import { Account as AccountSchema } from "@versia/client/schemas";
import { RolePermission } from "@versia/client/schemas";
import { ApiError } from "~/classes/errors/api-error";
import { ErrorSchema } from "~/types/api";
const route = createRoute({
method: "post",
path: "/api/v1/accounts/{id}/refetch",
summary: "Refetch account",
description: "Refetch the given account's profile from the remote server",
tags: ["Accounts"],
middleware: [
auth({
auth: true,
scopes: ["write:accounts"],
permissions: [RolePermission.ViewAccounts],
}),
withUserParam,
] as const,
request: {
params: z.object({
id: AccountSchema.shape.id,
}),
},
responses: {
200: {
description: "Refetched account data",
content: {
"application/json": {
schema: AccountSchema,
},
},
},
400: {
description: "User is local",
content: {
"application/json": {
schema: ErrorSchema,
},
},
},
404: accountNotFound,
...reusedResponses,
},
});
export default apiRoute((app) =>
app.openapi(route, async (context) => {
const otherUser = context.get("user");
if (otherUser.isLocal()) {
throw new ApiError(400, "Cannot refetch a local user");
}
const newUser = await otherUser.updateFromRemote();
return context.json(newUser.toApi(false), 200);
}),
);