From 3fd98eb88ab12d8bdcad4a97529e093baeab8add Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 28 Sep 2023 23:14:00 +0200 Subject: [PATCH] [mastodon-client] GET /accounts/lookup --- .../src/server/api/mastodon/endpoints/account.ts | 16 ++++++++-------- .../src/server/api/mastodon/helpers/user.ts | 7 +++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index badcbcf75..ad8f5b159 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -76,15 +76,15 @@ export function apiAccountMastodon(router: Router): void { } }); router.get("/v1/accounts/lookup", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.search( - (ctx.request.query as any).acct, - "accounts", - ); - ctx.body = convertAccount(data.data.accounts[0]); + const args = normalizeUrlQuery(ctx.query); + const user = await UserHelpers.getUserFromAcct(args.acct); + if (user === null) { + ctx.status = 404; + return; + } + const account = await UserConverter.encode(user); + ctx.body = convertAccount(account); } catch (e: any) { console.error(e); console.error(e.response.data); diff --git a/packages/backend/src/server/api/mastodon/helpers/user.ts b/packages/backend/src/server/api/mastodon/helpers/user.ts index d7afd9020..ebfb4ecd9 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -33,6 +33,7 @@ import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { convertId, IdType } from "@/misc/convert-id.js"; import acceptFollowRequest from "@/services/following/requests/accept.js"; import { rejectFollowRequest } from "@/services/following/reject.js"; +import { IsNull } from "typeorm"; export type AccountCache = { locks: AsyncLock; @@ -136,6 +137,12 @@ export class UserHelpers { return this.getUserRelationshipTo(target.id, localUser.id); } + public static async getUserFromAcct(acct: string): Promise { + const split = acct.toLowerCase().split('@'); + if (split.length > 2) throw new Error('Invalid acct'); + return Users.findOneBy({usernameLower: split[0], host: split[1] ?? IsNull()}); + } + public static async getUserMutes(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise> { if (limit > 80) limit = 80;