feat(api): Add refetching API

This commit is contained in:
Jesse Wierzbinski 2024-07-17 01:20:18 +02:00
parent f081941474
commit 7c285ee14d
No known key found for this signature in database
3 changed files with 81 additions and 0 deletions

22
docs/api/federation.md Normal file
View 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
}
```

View file

@ -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).

View 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));
},
);