From 7c285ee14df064c5a41d646421e38b91a7273dc6 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 17 Jul 2024 01:20:18 +0200 Subject: [PATCH] feat(api): :sparkles: Add refetching API --- docs/api/federation.md | 22 +++++++++ docs/api/index.md | 4 ++ server/api/api/v1/accounts/:id/refetch.ts | 55 +++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 docs/api/federation.md create mode 100644 server/api/api/v1/accounts/:id/refetch.ts diff --git a/docs/api/federation.md b/docs/api/federation.md new file mode 100644 index 00000000..eef6b6d9 --- /dev/null +++ b/docs/api/federation.md @@ -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 +} +``` \ No newline at end of file diff --git a/docs/api/index.md b/docs/api/index.md index dc079f72..e8f653f2 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -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). diff --git a/server/api/api/v1/accounts/:id/refetch.ts b/server/api/api/v1/accounts/:id/refetch.ts new file mode 100644 index 00000000..649b7036 --- /dev/null +++ b/server/api/api/v1/accounts/:id/refetch.ts @@ -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)); + }, + );