fix(api): 🐛 Fix account lookup address parsing (again)

This commit is contained in:
Jesse Wierzbinski 2024-12-02 15:40:20 +01:00
parent 91da99c934
commit e4768620e2
No known key found for this signature in database
3 changed files with 12 additions and 22 deletions

View file

@ -1,4 +1,4 @@
import { apiRoute, applyConfig, auth, userAddressValidator } from "@/api";
import { apiRoute, applyConfig, auth, parseUserAddress } from "@/api";
import { createRoute } from "@hono/zod-openapi";
import { Instance, User } from "@versia/kit/db";
import { RolePermissions, Users } from "@versia/kit/tables";
@ -71,23 +71,14 @@ export default apiRoute((app) =>
const { user } = context.get("auth");
// Check if acct is matching format username@domain.com or @username@domain.com
const accountMatches = [...acct.trim().matchAll(userAddressValidator)];
if (accountMatches.length === 0) {
return context.json({ error: 'Invalid parameter "acct"' }, 422);
}
const [, username, instanceHost] = accountMatches[0];
const { username, domain } = parseUserAddress(acct);
if (!username) {
throw new Error("Invalid username");
}
// User is local
if (
!instanceHost ||
instanceHost === new URL(config.http.base_url).host
) {
if (!domain || domain === new URL(config.http.base_url).host) {
const account = await User.fromSql(
and(eq(Users.username, username), isNull(Users.instanceId)),
);
@ -104,13 +95,10 @@ export default apiRoute((app) =>
// User is remote
// Try to fetch it from database
const instance = await Instance.resolveFromHost(instanceHost);
const instance = await Instance.resolveFromHost(domain);
if (!instance) {
return context.json(
{ error: `Instance ${instanceHost} not found` },
404,
);
return context.json({ error: `Instance ${domain} not found` }, 404);
}
const account = await User.fromSql(
@ -127,7 +115,7 @@ export default apiRoute((app) =>
// Fetch from remote instance
const manager = await (user ?? User).getFederationRequester();
const uri = await User.webFinger(manager, username, instanceHost);
const uri = await User.webFinger(manager, username, domain);
const foundAccount = await User.resolve(uri);

View file

@ -8,7 +8,7 @@ import {
import { createRoute } from "@hono/zod-openapi";
import { Note, User, db } from "@versia/kit/db";
import { Instances, Notes, RolePermissions, Users } from "@versia/kit/tables";
import { and, eq, inArray, sql } from "drizzle-orm";
import { and, eq, inArray, isNull, sql } from "drizzle-orm";
import { z } from "zod";
import { searchManager } from "~/classes/search/search-manager";
import { config } from "~/packages/config-manager";
@ -136,7 +136,9 @@ export default apiRoute((app) =>
.where(
and(
eq(Users.username, username),
eq(Instances.baseUrl, domain),
domain
? eq(Instances.baseUrl, domain)
: isNull(Users.instanceId),
),
)
)[0]?.id;
@ -154,7 +156,7 @@ export default apiRoute((app) =>
);
}
if (resolve) {
if (resolve && domain) {
const manager = await (
self ?? User
).getFederationRequester();

View file

@ -130,7 +130,7 @@ export const parseUserAddress = (
address: string,
): {
username: string;
domain: string;
domain?: string;
} => {
let output = address;
// Remove leading @ if it exists