mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
refactor(api): ♻️ Refactor user lookup endpoint
This commit is contained in:
parent
ca42df1dfd
commit
deee65ad6d
|
|
@ -1,9 +1,10 @@
|
||||||
import { apiRoute, applyConfig, auth, userAddressValidatorRemote } from "@/api";
|
import { apiRoute, applyConfig, auth, userAddressValidator } from "@/api";
|
||||||
import { createRoute } from "@hono/zod-openapi";
|
import { createRoute } from "@hono/zod-openapi";
|
||||||
import { User } from "@versia/kit/db";
|
import { Instance, User } from "@versia/kit/db";
|
||||||
import { RolePermissions, Users } from "@versia/kit/tables";
|
import { RolePermissions, Users } from "@versia/kit/tables";
|
||||||
import { eq } from "drizzle-orm";
|
import { and, eq, isNull } from "drizzle-orm";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { config } from "~/packages/config-manager";
|
||||||
import { ErrorSchema } from "~/types/api";
|
import { ErrorSchema } from "~/types/api";
|
||||||
|
|
||||||
export const meta = applyConfig({
|
export const meta = applyConfig({
|
||||||
|
|
@ -53,6 +54,14 @@ const route = createRoute({
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
422: {
|
||||||
|
description: "Invalid parameter",
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: ErrorSchema,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -62,43 +71,63 @@ export default apiRoute((app) =>
|
||||||
const { user } = context.get("auth");
|
const { user } = context.get("auth");
|
||||||
|
|
||||||
// Check if acct is matching format username@domain.com or @username@domain.com
|
// Check if acct is matching format username@domain.com or @username@domain.com
|
||||||
const accountMatches = acct?.trim().match(userAddressValidatorRemote);
|
const accountMatches = [...acct.trim().matchAll(userAddressValidator)];
|
||||||
|
|
||||||
if (accountMatches) {
|
if (accountMatches.length === 0) {
|
||||||
// Remove leading @ if it exists
|
return context.json({ error: 'Invalid parameter "acct"' }, 422);
|
||||||
if (accountMatches[0].startsWith("@")) {
|
|
||||||
accountMatches[0] = accountMatches[0].slice(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [username, domain] = accountMatches[0].split("@");
|
|
||||||
|
|
||||||
const manager = await (user ?? User).getFederationRequester();
|
|
||||||
|
|
||||||
const uri = await User.webFinger(manager, username, domain);
|
|
||||||
|
|
||||||
const foundAccount = await User.resolve(uri);
|
|
||||||
|
|
||||||
if (foundAccount) {
|
|
||||||
return context.json(foundAccount.toApi(), 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
return context.json({ error: "Account not found" }, 404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let username = acct;
|
const [, username, instanceHost] = accountMatches[0];
|
||||||
if (username.startsWith("@")) {
|
|
||||||
username = username.slice(1);
|
if (!username) {
|
||||||
|
throw new Error("Invalid username");
|
||||||
}
|
}
|
||||||
|
|
||||||
const account = await User.fromSql(eq(Users.username, username));
|
// User is local
|
||||||
|
if (
|
||||||
|
!instanceHost ||
|
||||||
|
instanceHost === new URL(config.http.base_url).host
|
||||||
|
) {
|
||||||
|
const account = await User.fromSql(
|
||||||
|
and(eq(Users.username, username), isNull(Users.instanceId)),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (account) {
|
||||||
|
return context.json(account.toApi(), 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.json(
|
||||||
|
{ error: `Account with username ${username} not found` },
|
||||||
|
404,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// User is remote
|
||||||
|
// Try to fetch it from database
|
||||||
|
const instance = await Instance.resolveFromHost(instanceHost);
|
||||||
|
|
||||||
|
const account = await User.fromSql(
|
||||||
|
and(
|
||||||
|
eq(Users.username, username),
|
||||||
|
eq(Users.instanceId, instance.id),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
if (account) {
|
if (account) {
|
||||||
return context.json(account.toApi(), 200);
|
return context.json(account.toApi(), 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
return context.json(
|
// Fetch from remote instance
|
||||||
{ error: `Account with username ${username} not found` },
|
const manager = await (user ?? User).getFederationRequester();
|
||||||
404,
|
|
||||||
);
|
const uri = await User.webFinger(manager, username, instanceHost);
|
||||||
|
|
||||||
|
const foundAccount = await User.resolve(uri);
|
||||||
|
|
||||||
|
if (foundAccount) {
|
||||||
|
return context.json(foundAccount.toApi(), 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.json({ error: "Account not found" }, 404);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue