mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
feat(api): ✨ Add refetching API
This commit is contained in:
parent
f081941474
commit
7c285ee14d
22
docs/api/federation.md
Normal file
22
docs/api/federation.md
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# Federation API
|
||||
|
||||
The Federation API contains a variety of endpoints for interacting with the Lysand remote network.
|
||||
|
||||
## Refetch User
|
||||
|
||||
```http
|
||||
POST /api/v1/accounts/:id/refetch
|
||||
```
|
||||
|
||||
Refetches the user's account from the remote network.
|
||||
|
||||
### Response
|
||||
|
||||
Returns the updated account object.
|
||||
```ts
|
||||
// 200 OK
|
||||
{
|
||||
id: string,
|
||||
... // Account object
|
||||
}
|
||||
```
|
||||
|
|
@ -23,6 +23,10 @@ For client developers. Please read [the documentation](./challenges.md).
|
|||
|
||||
For client developers. Please read [the documentation](./moderation.md).
|
||||
|
||||
## Federation API
|
||||
|
||||
For client developers. Please read [the documentation](./federation.md).
|
||||
|
||||
## Frontend API
|
||||
|
||||
For frontend developers. Please read [the documentation](./frontend.md).
|
||||
|
|
|
|||
55
server/api/api/v1/accounts/:id/refetch.ts
Normal file
55
server/api/api/v1/accounts/:id/refetch.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import { applyConfig, auth, handleZodError } from "@/api";
|
||||
import { errorResponse, jsonResponse } from "@/response";
|
||||
import type { Hono } from "@hono/hono";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
import { User } from "~/packages/database-interface/user";
|
||||
|
||||
export const meta = applyConfig({
|
||||
allowedMethods: ["POST"],
|
||||
ratelimits: {
|
||||
max: 4,
|
||||
duration: 60,
|
||||
},
|
||||
route: "/api/v1/accounts/:id/refetch",
|
||||
auth: {
|
||||
required: true,
|
||||
oauthPermissions: ["write:accounts"],
|
||||
},
|
||||
permissions: {
|
||||
required: [RolePermissions.ViewAccounts],
|
||||
},
|
||||
});
|
||||
|
||||
export const schemas = {
|
||||
param: z.object({
|
||||
id: z.string().uuid(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default (app: Hono) =>
|
||||
app.on(
|
||||
meta.allowedMethods,
|
||||
meta.route,
|
||||
zValidator("param", schemas.param, handleZodError),
|
||||
auth(meta.auth, meta.permissions),
|
||||
async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
const { user } = context.req.valid("header");
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
}
|
||||
|
||||
const otherUser = await User.fromId(id);
|
||||
|
||||
if (!otherUser) {
|
||||
return errorResponse("User not found", 404);
|
||||
}
|
||||
|
||||
const newUser = await otherUser.updateFromRemote();
|
||||
|
||||
return jsonResponse(newUser.toApi(false));
|
||||
},
|
||||
);
|
||||
Loading…
Reference in a new issue