[mastodon-client] GET /accounts/lookup

This commit is contained in:
Laura Hausmann 2023-09-28 23:14:00 +02:00
parent 45d005fa1a
commit 3fd98eb88a
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 15 additions and 8 deletions

View file

@ -76,15 +76,15 @@ export function apiAccountMastodon(router: Router): void {
} }
}); });
router.get("/v1/accounts/lookup", async (ctx) => { 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 { try {
const data = await client.search( const args = normalizeUrlQuery(ctx.query);
(ctx.request.query as any).acct, const user = await UserHelpers.getUserFromAcct(args.acct);
"accounts", if (user === null) {
); ctx.status = 404;
ctx.body = convertAccount(data.data.accounts[0]); return;
}
const account = await UserConverter.encode(user);
ctx.body = convertAccount(account);
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
console.error(e.response.data); console.error(e.response.data);

View file

@ -33,6 +33,7 @@ import { UserConverter } from "@/server/api/mastodon/converters/user.js";
import { convertId, IdType } from "@/misc/convert-id.js"; import { convertId, IdType } from "@/misc/convert-id.js";
import acceptFollowRequest from "@/services/following/requests/accept.js"; import acceptFollowRequest from "@/services/following/requests/accept.js";
import { rejectFollowRequest } from "@/services/following/reject.js"; import { rejectFollowRequest } from "@/services/following/reject.js";
import { IsNull } from "typeorm";
export type AccountCache = { export type AccountCache = {
locks: AsyncLock; locks: AsyncLock;
@ -136,6 +137,12 @@ export class UserHelpers {
return this.getUserRelationshipTo(target.id, localUser.id); return this.getUserRelationshipTo(target.id, localUser.id);
} }
public static async getUserFromAcct(acct: string): Promise<User | null> {
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<LinkPaginationObject<MastodonEntity.MutedAccount[]>> { public static async getUserMutes(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise<LinkPaginationObject<MastodonEntity.MutedAccount[]>> {
if (limit > 80) limit = 80; if (limit > 80) limit = 80;