2025-04-09 02:15:00 +02:00
|
|
|
import { FederationRequester } from "@versia/sdk/http";
|
2025-06-15 04:38:20 +02:00
|
|
|
import { WebFingerSchema } from "@versia/sdk/schemas";
|
|
|
|
|
import { config } from "@versia-server/config";
|
2025-06-15 23:50:34 +02:00
|
|
|
import { ApiError } from "@versia-server/kit";
|
|
|
|
|
import { apiRoute, handleZodError } from "@versia-server/kit/api";
|
|
|
|
|
import { User } from "@versia-server/kit/db";
|
|
|
|
|
import { parseUserAddress } from "@versia-server/kit/parsers";
|
|
|
|
|
import { uuid, webfingerMention } from "@versia-server/kit/regex";
|
|
|
|
|
import { Users } from "@versia-server/kit/tables";
|
2025-06-22 18:43:03 +02:00
|
|
|
import { federationBridgeLogger } from "@versia-server/logging";
|
2024-07-20 00:18:44 +02:00
|
|
|
import { and, eq, isNull } from "drizzle-orm";
|
2025-03-29 03:30:06 +01:00
|
|
|
import { describeRoute } from "hono-openapi";
|
|
|
|
|
import { resolver, validator } from "hono-openapi/zod";
|
|
|
|
|
import { z } from "zod";
|
2023-10-16 19:39:41 +02:00
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
export default apiRoute((app) =>
|
|
|
|
|
app.get(
|
|
|
|
|
"/.well-known/webfinger",
|
|
|
|
|
describeRoute({
|
|
|
|
|
summary: "Get user information",
|
|
|
|
|
tags: ["Federation"],
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "User information",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
2025-04-08 16:01:10 +02:00
|
|
|
schema: resolver(WebFingerSchema),
|
2025-03-29 03:30:06 +01:00
|
|
|
},
|
|
|
|
|
},
|
2024-09-16 15:29:09 +02:00
|
|
|
},
|
2025-03-29 03:30:06 +01:00
|
|
|
404: ApiError.accountNotFound().schema,
|
2024-09-16 15:29:09 +02:00
|
|
|
},
|
2025-03-29 03:30:06 +01:00
|
|
|
}),
|
|
|
|
|
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}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2025-06-15 23:43:27 +02:00
|
|
|
const isUuid = username.match(uuid);
|
2024-12-30 18:00:23 +01:00
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
const user = await User.fromSql(
|
|
|
|
|
and(
|
|
|
|
|
eq(isUuid ? Users.id : Users.username, username),
|
|
|
|
|
isNull(Users.instanceId),
|
|
|
|
|
),
|
2024-12-30 18:00:23 +01:00
|
|
|
);
|
2024-04-10 06:22:57 +02:00
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
if (!user) {
|
|
|
|
|
throw ApiError.accountNotFound();
|
|
|
|
|
}
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2025-04-08 16:01:10 +02:00
|
|
|
let activityPubUrl: URL | null = null;
|
2024-07-17 14:46:43 +02:00
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
if (config.federation.bridge) {
|
|
|
|
|
try {
|
2025-04-09 02:15:00 +02:00
|
|
|
activityPubUrl = await FederationRequester.resolveWebFinger(
|
|
|
|
|
user.data.username,
|
|
|
|
|
config.http.base_url.host,
|
|
|
|
|
"application/activity+json",
|
|
|
|
|
config.federation.bridge.url.origin,
|
|
|
|
|
);
|
2025-03-29 03:30:06 +01:00
|
|
|
} catch (e) {
|
2025-04-08 16:01:10 +02:00
|
|
|
const error = e as ApiError;
|
2024-07-17 14:46:43 +02:00
|
|
|
|
2025-06-22 18:43:03 +02:00
|
|
|
federationBridgeLogger.error`Error from bridge: ${error.message}`;
|
2025-03-29 03:30:06 +01:00
|
|
|
}
|
2024-07-17 14:46:43 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
return context.json(
|
|
|
|
|
{
|
2025-04-08 21:54:55 +02:00
|
|
|
subject: `acct:${
|
|
|
|
|
isUuid ? user.id : user.data.username
|
|
|
|
|
}@${host}`,
|
2025-03-29 03:30:06 +01:00
|
|
|
|
|
|
|
|
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",
|
2025-04-08 16:01:10 +02:00
|
|
|
href: activityPubUrl.href,
|
2025-03-29 03:30:06 +01:00
|
|
|
}
|
|
|
|
|
: 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,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-08-19 20:06:38 +02:00
|
|
|
);
|