From bdc5b778f2846211ca31801e8314def836e9cae2 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 28 Sep 2023 23:43:17 +0200 Subject: [PATCH] [mastodon-client] GET /accounts/verify_credentials --- .../server/api/mastodon/endpoints/account.ts | 31 ++++++------------- .../src/server/api/mastodon/helpers/user.ts | 25 ++++++++++++++- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index da6e4713c..b00861065 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -12,28 +12,17 @@ import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; export function apiAccountMastodon(router: Router): void { router.get("/v1/accounts/verify_credentials", 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.verifyAccountCredentials(); - let acct = data.data; - acct.id = convertId(acct.id, IdType.MastodonId); - acct.display_name = acct.display_name || acct.username; - acct.url = `${BASE_URL}/@${acct.url}`; - acct.note = acct.note || ""; - acct.avatar_static = acct.avatar; - acct.header = acct.header || "/static-assets/transparent.png"; - acct.header_static = acct.header || "/static-assets/transparent.png"; - acct.source = { - note: acct.note, - fields: acct.fields, - privacy: await client.getDefaultPostPrivacy(), - sensitive: false, - language: "", - }; - console.log(acct); - ctx.body = acct; + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; + return; + } + + const acct = await UserHelpers.verifyCredentials(user); + ctx.body = convertAccount(acct); } 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 ebfb4ecd9..e09124b40 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -8,7 +8,7 @@ import { NoteFavorites, NoteReactions, Notes, - NoteWatchings, + NoteWatchings, RegistryItems, UserProfiles, Users } from "@/models/index.js"; @@ -34,6 +34,7 @@ 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"; +import { VisibilityConverter } from "@/server/api/mastodon/converters/visibility.js"; export type AccountCache = { locks: AsyncLock; @@ -137,6 +138,28 @@ export class UserHelpers { return this.getUserRelationshipTo(target.id, localUser.id); } + public static async verifyCredentials(user: ILocalUser): Promise { + const acct = UserConverter.encode(user); + const profile = UserProfiles.findOneByOrFail({userId: user.id}); + const privacy = RegistryItems.findOneBy({domain: IsNull(), userId: user.id, key: 'defaultNoteVisibility', scope: '{client,base}'}); + return acct.then(acct => { + const source = { + note: acct.note, + fields: acct.fields, + privacy: privacy.then(p => VisibilityConverter.encode(p?.value ?? 'public')), + sensitive: profile.then(p => p.alwaysMarkNsfw), + language: profile.then(p => p.lang ?? ''), + }; + + const result = { + ...acct, + source: awaitAll(source) + }; + + return awaitAll(result); + }); + } + public static async getUserFromAcct(acct: string): Promise { const split = acct.toLowerCase().split('@'); if (split.length > 2) throw new Error('Invalid acct');