2025-05-01 16:27:34 +02:00
|
|
|
import {
|
|
|
|
|
Account as AccountSchema,
|
|
|
|
|
RolePermission,
|
|
|
|
|
} from "@versia/client/schemas";
|
2025-06-15 04:38:20 +02:00
|
|
|
import { ApiError } from "@versia/kit";
|
|
|
|
|
import { User } from "@versia/kit/db";
|
2025-03-29 03:30:06 +01:00
|
|
|
import { describeRoute } from "hono-openapi";
|
|
|
|
|
import { resolver } from "hono-openapi/zod";
|
2025-04-10 19:15:31 +02:00
|
|
|
import { apiRoute, auth, withUserParam } from "@/api";
|
2025-03-29 03:30:06 +01:00
|
|
|
|
|
|
|
|
export default apiRoute((app) =>
|
|
|
|
|
app.post(
|
|
|
|
|
"/api/v1/accounts/:id/refetch",
|
|
|
|
|
describeRoute({
|
|
|
|
|
summary: "Refetch account",
|
|
|
|
|
description:
|
|
|
|
|
"Refetch the given account's profile from the remote server",
|
|
|
|
|
tags: ["Accounts"],
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "Refetched account data",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: resolver(AccountSchema),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
400: {
|
|
|
|
|
description: "User is local",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: resolver(ApiError.zodSchema),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
404: ApiError.accountNotFound().schema,
|
|
|
|
|
401: ApiError.missingAuthentication().schema,
|
|
|
|
|
422: ApiError.validationFailed().schema,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
withUserParam,
|
|
|
|
|
auth({
|
|
|
|
|
auth: true,
|
|
|
|
|
scopes: ["write:accounts"],
|
|
|
|
|
permissions: [RolePermission.ViewAccounts],
|
|
|
|
|
}),
|
|
|
|
|
async (context) => {
|
|
|
|
|
const otherUser = context.get("user");
|
|
|
|
|
|
2025-04-08 18:13:30 +02:00
|
|
|
if (otherUser.local) {
|
2025-03-29 03:30:06 +01:00
|
|
|
throw new ApiError(400, "Cannot refetch a local user");
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 17:27:08 +02:00
|
|
|
const newUser = await User.fromVersia(otherUser.uri);
|
2025-03-29 03:30:06 +01:00
|
|
|
|
|
|
|
|
return context.json(newUser.toApi(false), 200);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|