mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): ♻️ Move from @hono/zod-openapi to hono-openapi
hono-openapi is easier to work with and generates better OpenAPI definitions
This commit is contained in:
parent
0576aff972
commit
58342e86e1
240 changed files with 9494 additions and 9575 deletions
|
|
@ -1,36 +1,38 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { describeRoute } from "hono-openapi";
|
||||
import { resolver } from "hono-openapi/zod";
|
||||
import { z } from "zod";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/.well-known/host-meta",
|
||||
summary: "Well-known host-meta",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Host-meta",
|
||||
content: {
|
||||
"application/xrd+xml": {
|
||||
schema: z.any(),
|
||||
export default apiRoute((app) =>
|
||||
app.get(
|
||||
"/.well-known/host-meta",
|
||||
describeRoute({
|
||||
summary: "Well-known host-meta",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Host-meta",
|
||||
content: {
|
||||
"application/xrd+xml": {
|
||||
schema: resolver(z.any()),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
(context) => {
|
||||
context.header("Content-Type", "application/xrd+xml");
|
||||
context.status(200);
|
||||
|
||||
return context.body(
|
||||
`<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" template="${new URL(
|
||||
"/.well-known/webfinger",
|
||||
config.http.base_url,
|
||||
).toString()}?resource={uri}"/></XRD>`,
|
||||
200,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: Hono doesn't type this response so this has a TS error, it's joever
|
||||
) as any;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, (context) => {
|
||||
context.header("Content-Type", "application/xrd+xml");
|
||||
context.status(200);
|
||||
|
||||
return context.body(
|
||||
`<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" template="${new URL(
|
||||
"/.well-known/webfinger",
|
||||
config.http.base_url,
|
||||
).toString()}?resource={uri}"/></XRD>`,
|
||||
200,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: Hono doesn't type this response so this has a TS error, it's joever
|
||||
) as any;
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,76 +1,80 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { Note, User } from "@versia/kit/db";
|
||||
import { describeRoute } from "hono-openapi";
|
||||
import { resolver } from "hono-openapi/zod";
|
||||
import { z } from "zod";
|
||||
import { config } from "~/config.ts";
|
||||
import manifest from "~/package.json";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/.well-known/nodeinfo/2.0",
|
||||
summary: "Well-known nodeinfo 2.0",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Nodeinfo 2.0",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
version: z.string(),
|
||||
software: z.object({
|
||||
name: z.string(),
|
||||
version: z.string(),
|
||||
}),
|
||||
protocols: z.array(z.string()),
|
||||
services: z.object({
|
||||
outbound: z.array(z.string()),
|
||||
inbound: z.array(z.string()),
|
||||
}),
|
||||
usage: z.object({
|
||||
users: z.object({
|
||||
total: z.number(),
|
||||
activeMonth: z.number(),
|
||||
activeHalfyear: z.number(),
|
||||
}),
|
||||
localPosts: z.number(),
|
||||
}),
|
||||
openRegistrations: z.boolean(),
|
||||
metadata: z.object({}),
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
const userCount = await User.getCount();
|
||||
const userActiveMonth = await User.getActiveInPeriod(
|
||||
1000 * 60 * 60 * 24 * 30,
|
||||
);
|
||||
const userActiveHalfyear = await User.getActiveInPeriod(
|
||||
1000 * 60 * 60 * 24 * 30 * 6,
|
||||
);
|
||||
const noteCount = await Note.getCount();
|
||||
|
||||
return context.json({
|
||||
version: "2.0",
|
||||
software: { name: "versia-server", version: manifest.version },
|
||||
protocols: ["versia"],
|
||||
services: { outbound: [], inbound: [] },
|
||||
usage: {
|
||||
users: {
|
||||
total: userCount,
|
||||
activeMonth: userActiveMonth,
|
||||
activeHalfyear: userActiveHalfyear,
|
||||
app.get(
|
||||
"/.well-known/nodeinfo/2.0",
|
||||
describeRoute({
|
||||
summary: "Well-known nodeinfo 2.0",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Nodeinfo 2.0",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(
|
||||
z.object({
|
||||
version: z.string(),
|
||||
software: z.object({
|
||||
name: z.string(),
|
||||
version: z.string(),
|
||||
}),
|
||||
protocols: z.array(z.string()),
|
||||
services: z.object({
|
||||
outbound: z.array(z.string()),
|
||||
inbound: z.array(z.string()),
|
||||
}),
|
||||
usage: z.object({
|
||||
users: z.object({
|
||||
total: z.number(),
|
||||
activeMonth: z.number(),
|
||||
activeHalfyear: z.number(),
|
||||
}),
|
||||
localPosts: z.number(),
|
||||
}),
|
||||
openRegistrations: z.boolean(),
|
||||
metadata: z.object({}),
|
||||
}),
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
localPosts: noteCount,
|
||||
},
|
||||
openRegistrations: config.registration.allow,
|
||||
metadata: {
|
||||
nodeName: config.instance.name,
|
||||
nodeDescription: config.instance.description,
|
||||
},
|
||||
});
|
||||
}),
|
||||
}),
|
||||
async (context) => {
|
||||
const userCount = await User.getCount();
|
||||
const userActiveMonth = await User.getActiveInPeriod(
|
||||
1000 * 60 * 60 * 24 * 30,
|
||||
);
|
||||
const userActiveHalfyear = await User.getActiveInPeriod(
|
||||
1000 * 60 * 60 * 24 * 30 * 6,
|
||||
);
|
||||
const noteCount = await Note.getCount();
|
||||
|
||||
return context.json({
|
||||
version: "2.0",
|
||||
software: { name: "versia-server", version: manifest.version },
|
||||
protocols: ["versia"],
|
||||
services: { outbound: [], inbound: [] },
|
||||
usage: {
|
||||
users: {
|
||||
total: userCount,
|
||||
activeMonth: userActiveMonth,
|
||||
activeHalfyear: userActiveHalfyear,
|
||||
},
|
||||
localPosts: noteCount,
|
||||
},
|
||||
openRegistrations: config.registration.allow,
|
||||
metadata: {
|
||||
nodeName: config.instance.name,
|
||||
nodeDescription: config.instance.description,
|
||||
},
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,43 +1,47 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { describeRoute } from "hono-openapi";
|
||||
import { resolver } from "hono-openapi/zod";
|
||||
import { z } from "zod";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/.well-known/nodeinfo",
|
||||
summary: "Well-known nodeinfo",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Nodeinfo links",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
links: z.array(
|
||||
z.object({
|
||||
rel: z.string(),
|
||||
href: z.string(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
export default apiRoute((app) =>
|
||||
app.get(
|
||||
"/.well-known/nodeinfo",
|
||||
describeRoute({
|
||||
summary: "Well-known nodeinfo",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Nodeinfo links",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(
|
||||
z.object({
|
||||
links: z.array(
|
||||
z.object({
|
||||
rel: z.string(),
|
||||
href: z.string(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
(context) => {
|
||||
return context.json({
|
||||
links: [
|
||||
{
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
||||
href: new URL(
|
||||
"/.well-known/nodeinfo/2.0",
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
},
|
||||
],
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, (context) => {
|
||||
return context.json({
|
||||
links: [
|
||||
{
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
||||
href: new URL(
|
||||
"/.well-known/nodeinfo/2.0",
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
},
|
||||
],
|
||||
});
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,58 +1,66 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { describeRoute } from "hono-openapi";
|
||||
import { resolver } from "hono-openapi/zod";
|
||||
import { z } from "zod";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/.well-known/openid-configuration",
|
||||
summary: "OpenID Configuration",
|
||||
tags: ["OpenID"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "OpenID Configuration",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
issuer: z.string(),
|
||||
authorization_endpoint: z.string(),
|
||||
token_endpoint: z.string(),
|
||||
userinfo_endpoint: z.string(),
|
||||
jwks_uri: z.string(),
|
||||
response_types_supported: z.array(z.string()),
|
||||
subject_types_supported: z.array(z.string()),
|
||||
id_token_signing_alg_values_supported: z.array(
|
||||
z.string(),
|
||||
),
|
||||
scopes_supported: z.array(z.string()),
|
||||
token_endpoint_auth_methods_supported: z.array(
|
||||
z.string(),
|
||||
),
|
||||
claims_supported: z.array(z.string()),
|
||||
}),
|
||||
export default apiRoute((app) =>
|
||||
app.get(
|
||||
"/.well-known/openid-configuration",
|
||||
describeRoute({
|
||||
summary: "OpenID Configuration",
|
||||
tags: ["OpenID"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "OpenID Configuration",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(
|
||||
z.object({
|
||||
issuer: z.string(),
|
||||
authorization_endpoint: z.string(),
|
||||
token_endpoint: z.string(),
|
||||
userinfo_endpoint: z.string(),
|
||||
jwks_uri: z.string(),
|
||||
response_types_supported: z.array(
|
||||
z.string(),
|
||||
),
|
||||
subject_types_supported: z.array(
|
||||
z.string(),
|
||||
),
|
||||
id_token_signing_alg_values_supported:
|
||||
z.array(z.string()),
|
||||
scopes_supported: z.array(z.string()),
|
||||
token_endpoint_auth_methods_supported:
|
||||
z.array(z.string()),
|
||||
claims_supported: z.array(z.string()),
|
||||
}),
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
(context) => {
|
||||
const baseUrl = config.http.base_url;
|
||||
return context.json(
|
||||
{
|
||||
issuer: baseUrl.origin.toString(),
|
||||
authorization_endpoint: `${baseUrl.origin}/oauth/authorize`,
|
||||
token_endpoint: `${baseUrl.origin}/oauth/token`,
|
||||
userinfo_endpoint: `${baseUrl.origin}/api/v1/accounts/verify_credentials`,
|
||||
jwks_uri: `${baseUrl.origin}/.well-known/jwks`,
|
||||
response_types_supported: ["code"],
|
||||
subject_types_supported: ["public"],
|
||||
id_token_signing_alg_values_supported: ["EdDSA"],
|
||||
scopes_supported: ["openid", "profile", "email"],
|
||||
token_endpoint_auth_methods_supported: [
|
||||
"client_secret_basic",
|
||||
],
|
||||
claims_supported: ["sub"],
|
||||
},
|
||||
200,
|
||||
);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, (context) => {
|
||||
const baseUrl = config.http.base_url;
|
||||
return context.json(
|
||||
{
|
||||
issuer: baseUrl.origin.toString(),
|
||||
authorization_endpoint: `${baseUrl.origin}/oauth/authorize`,
|
||||
token_endpoint: `${baseUrl.origin}/oauth/token`,
|
||||
userinfo_endpoint: `${baseUrl.origin}/api/v1/accounts/verify_credentials`,
|
||||
jwks_uri: `${baseUrl.origin}/.well-known/jwks`,
|
||||
response_types_supported: ["code"],
|
||||
subject_types_supported: ["public"],
|
||||
id_token_signing_alg_values_supported: ["EdDSA"],
|
||||
scopes_supported: ["openid", "profile", "email"],
|
||||
token_endpoint_auth_methods_supported: ["client_secret_basic"],
|
||||
claims_supported: ["sub"],
|
||||
},
|
||||
200,
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,83 +1,90 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { urlToContentFormat } from "@/content_types";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { InstanceMetadata as InstanceMetadataSchema } from "@versia/federation/schemas";
|
||||
import { User } from "@versia/kit/db";
|
||||
import { Users } from "@versia/kit/tables";
|
||||
import { asc } from "drizzle-orm";
|
||||
import { describeRoute } from "hono-openapi";
|
||||
import { resolver } from "hono-openapi/zod";
|
||||
import { config } from "~/config.ts";
|
||||
import pkg from "~/package.json";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/.well-known/versia",
|
||||
summary: "Get instance metadata",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Instance metadata",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: InstanceMetadataSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
// Get date of first user creation
|
||||
const firstUser = await User.fromSql(undefined, asc(Users.createdAt));
|
||||
|
||||
const publicKey = Buffer.from(
|
||||
await crypto.subtle.exportKey("spki", config.instance.keys.public),
|
||||
).toString("base64");
|
||||
|
||||
return context.json(
|
||||
{
|
||||
type: "InstanceMetadata" as const,
|
||||
compatibility: {
|
||||
extensions: [
|
||||
"pub.versia:custom_emojis",
|
||||
"pub.versia:instance_messaging",
|
||||
],
|
||||
versions: ["0.5.0"],
|
||||
},
|
||||
host: config.http.base_url.host,
|
||||
name: config.instance.name,
|
||||
description: config.instance.description,
|
||||
public_key: {
|
||||
key: publicKey,
|
||||
algorithm: "ed25519" as const,
|
||||
},
|
||||
software: {
|
||||
name: "Versia Server",
|
||||
version: pkg.version,
|
||||
},
|
||||
banner: config.instance.branding.banner
|
||||
? urlToContentFormat(config.instance.branding.banner)
|
||||
: undefined,
|
||||
logo: config.instance.branding.logo
|
||||
? urlToContentFormat(config.instance.branding.logo)
|
||||
: undefined,
|
||||
shared_inbox: new URL(
|
||||
"/inbox",
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
created_at: new Date(
|
||||
firstUser?.data.createdAt ?? 0,
|
||||
).toISOString(),
|
||||
extensions: {
|
||||
"pub.versia:instance_messaging": {
|
||||
endpoint: new URL(
|
||||
"/messaging",
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
app.get(
|
||||
"/.well-known/versia",
|
||||
describeRoute({
|
||||
summary: "Get instance metadata",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Instance metadata",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(InstanceMetadataSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
200,
|
||||
);
|
||||
}),
|
||||
}),
|
||||
async (context) => {
|
||||
// Get date of first user creation
|
||||
const firstUser = await User.fromSql(
|
||||
undefined,
|
||||
asc(Users.createdAt),
|
||||
);
|
||||
|
||||
const publicKey = Buffer.from(
|
||||
await crypto.subtle.exportKey(
|
||||
"spki",
|
||||
config.instance.keys.public,
|
||||
),
|
||||
).toString("base64");
|
||||
|
||||
return context.json(
|
||||
{
|
||||
type: "InstanceMetadata" as const,
|
||||
compatibility: {
|
||||
extensions: [
|
||||
"pub.versia:custom_emojis",
|
||||
"pub.versia:instance_messaging",
|
||||
],
|
||||
versions: ["0.5.0"],
|
||||
},
|
||||
host: config.http.base_url.host,
|
||||
name: config.instance.name,
|
||||
description: config.instance.description,
|
||||
public_key: {
|
||||
key: publicKey,
|
||||
algorithm: "ed25519" as const,
|
||||
},
|
||||
software: {
|
||||
name: "Versia Server",
|
||||
version: pkg.version,
|
||||
},
|
||||
banner: config.instance.branding.banner
|
||||
? urlToContentFormat(config.instance.branding.banner)
|
||||
: undefined,
|
||||
logo: config.instance.branding.logo
|
||||
? urlToContentFormat(config.instance.branding.logo)
|
||||
: undefined,
|
||||
shared_inbox: new URL(
|
||||
"/inbox",
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
created_at: new Date(
|
||||
firstUser?.data.createdAt ?? 0,
|
||||
).toISOString(),
|
||||
extensions: {
|
||||
"pub.versia:instance_messaging": {
|
||||
endpoint: new URL(
|
||||
"/messaging",
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
},
|
||||
},
|
||||
},
|
||||
200,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,143 +1,144 @@
|
|||
import {
|
||||
apiRoute,
|
||||
handleZodError,
|
||||
idValidator,
|
||||
parseUserAddress,
|
||||
webfingerMention,
|
||||
} from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
import type { ResponseError } from "@versia/federation";
|
||||
import { WebFinger } from "@versia/federation/schemas";
|
||||
import { User } from "@versia/kit/db";
|
||||
import { Users } from "@versia/kit/tables";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { describeRoute } from "hono-openapi";
|
||||
import { resolver, validator } from "hono-openapi/zod";
|
||||
import { z } from "zod";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
const schemas = {
|
||||
query: z.object({
|
||||
resource: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(512)
|
||||
.startsWith("acct:")
|
||||
.regex(
|
||||
webfingerMention,
|
||||
"Invalid resource (should be acct:(id or username)@domain)",
|
||||
),
|
||||
}),
|
||||
};
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
path: "/.well-known/webfinger",
|
||||
summary: "Get user information",
|
||||
request: {
|
||||
query: schemas.query,
|
||||
},
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "User information",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: WebFinger,
|
||||
},
|
||||
},
|
||||
},
|
||||
404: ApiError.accountNotFound().schema,
|
||||
},
|
||||
});
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
const { resource } = context.req.valid("query");
|
||||
|
||||
const requestedUser = resource.split("acct:")[1];
|
||||
|
||||
const host = config.http.base_url.host;
|
||||
|
||||
const { username, domain } = parseUserAddress(requestedUser);
|
||||
|
||||
// Check if user is a local user
|
||||
if (domain !== host) {
|
||||
throw new ApiError(
|
||||
404,
|
||||
`User domain ${domain} does not match ${host}`,
|
||||
);
|
||||
}
|
||||
|
||||
const isUuid = username.match(idValidator);
|
||||
|
||||
const user = await User.fromSql(
|
||||
and(
|
||||
eq(isUuid ? Users.id : Users.username, username),
|
||||
isNull(Users.instanceId),
|
||||
),
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
throw ApiError.accountNotFound();
|
||||
}
|
||||
|
||||
let activityPubUrl = "";
|
||||
|
||||
if (config.federation.bridge) {
|
||||
const manager = await User.getFederationRequester();
|
||||
|
||||
try {
|
||||
activityPubUrl = await manager.webFinger(
|
||||
user.data.username,
|
||||
config.http.base_url.host,
|
||||
"application/activity+json",
|
||||
config.federation.bridge.url.origin,
|
||||
);
|
||||
} catch (e) {
|
||||
const error = e as ResponseError;
|
||||
|
||||
getLogger(["federation", "bridge"])
|
||||
.error`Error from bridge: ${await error.response.data}`;
|
||||
}
|
||||
}
|
||||
|
||||
return context.json(
|
||||
{
|
||||
subject: `acct:${isUuid ? user.id : user.data.username}@${host}`,
|
||||
|
||||
links: [
|
||||
// Keep the ActivityPub link first, because Misskey only searches
|
||||
// for the first link with rel="self" and doesn't check the type.
|
||||
activityPubUrl
|
||||
? {
|
||||
rel: "self",
|
||||
type: "application/activity+json",
|
||||
href: activityPubUrl,
|
||||
}
|
||||
: undefined,
|
||||
{
|
||||
rel: "self",
|
||||
type: "application/json",
|
||||
href: new URL(
|
||||
`/users/${user.id}`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
app.get(
|
||||
"/.well-known/webfinger",
|
||||
describeRoute({
|
||||
summary: "Get user information",
|
||||
tags: ["Federation"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "User information",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(WebFinger),
|
||||
},
|
||||
},
|
||||
{
|
||||
rel: "avatar",
|
||||
// Default avatars are SVGs
|
||||
type:
|
||||
user.avatar?.getPreferredMimeType() ??
|
||||
"image/svg+xml",
|
||||
href: user.getAvatarUrl(),
|
||||
},
|
||||
].filter(Boolean) as {
|
||||
rel: string;
|
||||
type: string;
|
||||
href: string;
|
||||
}[],
|
||||
},
|
||||
404: ApiError.accountNotFound().schema,
|
||||
},
|
||||
200,
|
||||
);
|
||||
}),
|
||||
}),
|
||||
validator(
|
||||
"query",
|
||||
z.object({
|
||||
resource: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(512)
|
||||
.startsWith("acct:")
|
||||
.regex(
|
||||
webfingerMention,
|
||||
"Invalid resource (should be acct:(id or username)@domain)",
|
||||
),
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
async (context) => {
|
||||
const { resource } = context.req.valid("query");
|
||||
|
||||
const requestedUser = resource.split("acct:")[1];
|
||||
|
||||
const host = config.http.base_url.host;
|
||||
|
||||
const { username, domain } = parseUserAddress(requestedUser);
|
||||
|
||||
// Check if user is a local user
|
||||
if (domain !== host) {
|
||||
throw new ApiError(
|
||||
404,
|
||||
`User domain ${domain} does not match ${host}`,
|
||||
);
|
||||
}
|
||||
|
||||
const isUuid = username.match(idValidator);
|
||||
|
||||
const user = await User.fromSql(
|
||||
and(
|
||||
eq(isUuid ? Users.id : Users.username, username),
|
||||
isNull(Users.instanceId),
|
||||
),
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
throw ApiError.accountNotFound();
|
||||
}
|
||||
|
||||
let activityPubUrl = "";
|
||||
|
||||
if (config.federation.bridge) {
|
||||
const manager = await User.getFederationRequester();
|
||||
|
||||
try {
|
||||
activityPubUrl = await manager.webFinger(
|
||||
user.data.username,
|
||||
config.http.base_url.host,
|
||||
"application/activity+json",
|
||||
config.federation.bridge.url.origin,
|
||||
);
|
||||
} catch (e) {
|
||||
const error = e as ResponseError;
|
||||
|
||||
getLogger(["federation", "bridge"])
|
||||
.error`Error from bridge: ${await error.response.data}`;
|
||||
}
|
||||
}
|
||||
|
||||
return context.json(
|
||||
{
|
||||
subject: `acct:${isUuid ? user.id : user.data.username}@${host}`,
|
||||
|
||||
links: [
|
||||
// Keep the ActivityPub link first, because Misskey only searches
|
||||
// for the first link with rel="self" and doesn't check the type.
|
||||
activityPubUrl
|
||||
? {
|
||||
rel: "self",
|
||||
type: "application/activity+json",
|
||||
href: activityPubUrl,
|
||||
}
|
||||
: undefined,
|
||||
{
|
||||
rel: "self",
|
||||
type: "application/json",
|
||||
href: new URL(
|
||||
`/users/${user.id}`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
},
|
||||
{
|
||||
rel: "avatar",
|
||||
// Default avatars are SVGs
|
||||
type:
|
||||
user.avatar?.getPreferredMimeType() ??
|
||||
"image/svg+xml",
|
||||
href: user.getAvatarUrl(),
|
||||
},
|
||||
].filter(Boolean) as {
|
||||
rel: string;
|
||||
type: string;
|
||||
href: string;
|
||||
}[],
|
||||
},
|
||||
200,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue