From 113a1ebe5988a6ebbb07d381aea7669ac8c9cafb Mon Sep 17 00:00:00 2001 From: cutestnekoaqua Date: Mon, 13 Feb 2023 16:12:21 +0100 Subject: [PATCH] basic account lookup --- .../server/api/mastodon/endpoints/account.ts | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index ebe6dd2ddb..5c1a2820fc 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -1,4 +1,7 @@ +import { Users } from "@/models/index.js"; +import { resolveUser } from "@/remote/resolve-user.js"; import Router from "@koa/router"; +import { FindOptionsWhere, IsNull } from "typeorm"; import { getClient } from "../ApiMastodonCompatibleService.js"; import { toLimitToInt } from "./timeline.js"; @@ -68,9 +71,22 @@ export function apiAccountMastodon(router: Router): void { const accessTokens = ctx.headers.authorization; const client = getClient(BASE_URL, accessTokens); try { - const data = await client.getAccount( - `@${(ctx.query.acct || "").toString()}`, - ); + let userArray = ctx.query.acct?.toString().split("@"); + let userid; + if (userArray === undefined) { + ctx.status = 401; + ctx.body = { error: "no user specified"}; + return; + } + if (userArray.length === 1) { + const q: FindOptionsWhere = { usernameLower: userArray[0].toLowerCase(), host: IsNull() }; + + const user = await Users.findOneBy(q); + userid = user?.id; + } else { + userid = (await resolveUser(userArray[0], userArray[1])).id; + } + const data = await client.getAccount(userid ? userid : ''); ctx.body = data.data; } catch (e: any) { console.error(e);