2024-12-30 18:00:23 +01:00
|
|
|
import {
|
|
|
|
|
apiRoute,
|
|
|
|
|
idValidator,
|
|
|
|
|
parseUserAddress,
|
|
|
|
|
webfingerMention,
|
|
|
|
|
} from "@/api";
|
2025-02-05 21:49:39 +01:00
|
|
|
import { createRoute, z } from "@hono/zod-openapi";
|
2024-07-17 14:46:43 +02:00
|
|
|
import { getLogger } from "@logtape/logtape";
|
2024-08-26 19:06:49 +02:00
|
|
|
import type { ResponseError } from "@versia/federation";
|
2024-09-23 10:00:06 +02:00
|
|
|
import { WebFinger } from "@versia/federation/schemas";
|
2024-11-01 20:57:16 +01:00
|
|
|
import { User } from "@versia/kit/db";
|
2024-11-01 21:05:54 +01:00
|
|
|
import { Users } from "@versia/kit/tables";
|
2024-07-20 00:18:44 +02:00
|
|
|
import { and, eq, isNull } from "drizzle-orm";
|
2024-12-30 18:00:23 +01:00
|
|
|
import { ApiError } from "~/classes/errors/api-error";
|
2025-02-15 02:47:29 +01:00
|
|
|
import { config } from "~/config.ts";
|
2023-10-16 19:39:41 +02:00
|
|
|
|
2024-12-30 20:26:56 +01:00
|
|
|
const schemas = {
|
2024-05-06 09:16:33 +02:00
|
|
|
query: z.object({
|
2024-09-16 15:29:09 +02:00
|
|
|
resource: z
|
|
|
|
|
.string()
|
|
|
|
|
.trim()
|
|
|
|
|
.min(1)
|
|
|
|
|
.max(512)
|
|
|
|
|
.startsWith("acct:")
|
|
|
|
|
.regex(
|
|
|
|
|
webfingerMention,
|
|
|
|
|
"Invalid resource (should be acct:(id or username)@domain)",
|
|
|
|
|
),
|
2024-05-06 09:16:33 +02:00
|
|
|
}),
|
|
|
|
|
};
|
2024-04-10 05:45:19 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const route = createRoute({
|
|
|
|
|
method: "get",
|
|
|
|
|
path: "/.well-known/webfinger",
|
|
|
|
|
summary: "Get user information",
|
|
|
|
|
request: {
|
|
|
|
|
query: schemas.query,
|
|
|
|
|
},
|
2025-03-28 22:06:42 +01:00
|
|
|
tags: ["Federation"],
|
2024-09-16 15:29:09 +02:00
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "User information",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
2024-09-23 10:00:06 +02:00
|
|
|
schema: WebFinger,
|
2024-09-16 15:29:09 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-03-24 14:42:09 +01:00
|
|
|
404: ApiError.accountNotFound().schema,
|
2024-09-16 15:29:09 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-19 20:06:38 +02:00
|
|
|
export default apiRoute((app) =>
|
2024-09-16 15:29:09 +02:00
|
|
|
app.openapi(route, async (context) => {
|
|
|
|
|
const { resource } = context.req.valid("query");
|
2024-04-10 05:45:19 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const requestedUser = resource.split("acct:")[1];
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2025-02-13 01:31:15 +01:00
|
|
|
const host = config.http.base_url.host;
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2024-12-30 18:00:23 +01:00
|
|
|
const { username, domain } = parseUserAddress(requestedUser);
|
|
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
// Check if user is a local user
|
2024-12-30 18:00:23 +01:00
|
|
|
if (domain !== host) {
|
|
|
|
|
throw new ApiError(
|
|
|
|
|
404,
|
|
|
|
|
`User domain ${domain} does not match ${host}`,
|
|
|
|
|
);
|
2024-09-16 15:29:09 +02:00
|
|
|
}
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2024-12-30 18:00:23 +01:00
|
|
|
const isUuid = username.match(idValidator);
|
2024-04-10 06:22:57 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
const user = await User.fromSql(
|
|
|
|
|
and(
|
2024-12-30 18:00:23 +01:00
|
|
|
eq(isUuid ? Users.id : Users.username, username),
|
2024-09-16 15:29:09 +02:00
|
|
|
isNull(Users.instanceId),
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
if (!user) {
|
2025-03-24 14:42:09 +01:00
|
|
|
throw ApiError.accountNotFound();
|
2024-09-16 15:29:09 +02:00
|
|
|
}
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
let activityPubUrl = "";
|
2024-07-17 14:46:43 +02:00
|
|
|
|
2025-02-15 02:47:29 +01:00
|
|
|
if (config.federation.bridge) {
|
2024-09-16 15:29:09 +02:00
|
|
|
const manager = await User.getFederationRequester();
|
2024-07-17 14:46:43 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
try {
|
|
|
|
|
activityPubUrl = await manager.webFinger(
|
|
|
|
|
user.data.username,
|
2025-02-13 01:31:15 +01:00
|
|
|
config.http.base_url.host,
|
2024-09-16 15:29:09 +02:00
|
|
|
"application/activity+json",
|
2025-02-15 02:47:29 +01:00
|
|
|
config.federation.bridge.url.origin,
|
2024-09-16 15:29:09 +02:00
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
const error = e as ResponseError;
|
2024-07-17 14:46:43 +02:00
|
|
|
|
2024-11-24 23:13:29 +01:00
|
|
|
getLogger(["federation", "bridge"])
|
2024-09-16 15:29:09 +02:00
|
|
|
.error`Error from bridge: ${await error.response.data}`;
|
2024-07-17 14:46:43 +02:00
|
|
|
}
|
2024-09-16 15:29:09 +02:00
|
|
|
}
|
2024-07-17 14:46:43 +02:00
|
|
|
|
2024-09-16 15:29:09 +02:00
|
|
|
return context.json(
|
|
|
|
|
{
|
|
|
|
|
subject: `acct:${isUuid ? user.id : user.data.username}@${host}`,
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
links: [
|
2024-07-17 15:08:21 +02:00
|
|
|
// Keep the ActivityPub link first, because Misskey only searches
|
|
|
|
|
// for the first link with rel="self" and doesn't check the type.
|
2024-09-16 15:29:09 +02:00
|
|
|
activityPubUrl
|
|
|
|
|
? {
|
|
|
|
|
rel: "self",
|
|
|
|
|
type: "application/activity+json",
|
|
|
|
|
href: activityPubUrl,
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2024-05-06 09:16:33 +02:00
|
|
|
{
|
|
|
|
|
rel: "self",
|
|
|
|
|
type: "application/json",
|
|
|
|
|
href: new URL(
|
|
|
|
|
`/users/${user.id}`,
|
|
|
|
|
config.http.base_url,
|
|
|
|
|
).toString(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
rel: "avatar",
|
2025-02-12 23:25:22 +01:00
|
|
|
// Default avatars are SVGs
|
2024-09-16 15:29:09 +02:00
|
|
|
type:
|
2025-02-12 23:25:22 +01:00
|
|
|
user.avatar?.getPreferredMimeType() ??
|
|
|
|
|
"image/svg+xml",
|
2025-02-15 02:47:29 +01:00
|
|
|
href: user.getAvatarUrl(),
|
2024-05-06 09:16:33 +02:00
|
|
|
},
|
2024-09-16 15:29:09 +02:00
|
|
|
].filter(Boolean) as {
|
|
|
|
|
rel: string;
|
|
|
|
|
type: string;
|
|
|
|
|
href: string;
|
|
|
|
|
}[],
|
|
|
|
|
},
|
|
|
|
|
200,
|
|
|
|
|
);
|
|
|
|
|
}),
|
2024-08-19 20:06:38 +02:00
|
|
|
);
|