[mastodon-client] GET /accounts/verify_credentials

This commit is contained in:
Laura Hausmann 2023-09-28 23:43:17 +02:00
parent 40f89213a5
commit bdc5b778f2
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 34 additions and 22 deletions

View file

@ -12,28 +12,17 @@ import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
export function apiAccountMastodon(router: Router): void { export function apiAccountMastodon(router: Router): void {
router.get("/v1/accounts/verify_credentials", async (ctx) => { 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 { try {
const data = await client.verifyAccountCredentials(); const auth = await authenticate(ctx.headers.authorization, null);
let acct = data.data; const user = auth[0] ?? null;
acct.id = convertId(acct.id, IdType.MastodonId);
acct.display_name = acct.display_name || acct.username; if (!user) {
acct.url = `${BASE_URL}/@${acct.url}`; ctx.status = 401;
acct.note = acct.note || ""; return;
acct.avatar_static = acct.avatar; }
acct.header = acct.header || "/static-assets/transparent.png";
acct.header_static = acct.header || "/static-assets/transparent.png"; const acct = await UserHelpers.verifyCredentials(user);
acct.source = { ctx.body = convertAccount(acct);
note: acct.note,
fields: acct.fields,
privacy: await client.getDefaultPostPrivacy(),
sensitive: false,
language: "",
};
console.log(acct);
ctx.body = acct;
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
console.error(e.response.data); console.error(e.response.data);

View file

@ -8,7 +8,7 @@ import {
NoteFavorites, NoteFavorites,
NoteReactions, NoteReactions,
Notes, Notes,
NoteWatchings, NoteWatchings, RegistryItems,
UserProfiles, UserProfiles,
Users Users
} from "@/models/index.js"; } 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 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"; import { IsNull } from "typeorm";
import { VisibilityConverter } from "@/server/api/mastodon/converters/visibility.js";
export type AccountCache = { export type AccountCache = {
locks: AsyncLock; locks: AsyncLock;
@ -137,6 +138,28 @@ export class UserHelpers {
return this.getUserRelationshipTo(target.id, localUser.id); return this.getUserRelationshipTo(target.id, localUser.id);
} }
public static async verifyCredentials(user: ILocalUser): Promise<MastodonEntity.Account> {
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<User | null> { public static async getUserFromAcct(acct: string): Promise<User | null> {
const split = acct.toLowerCase().split('@'); const split = acct.toLowerCase().split('@');
if (split.length > 2) throw new Error('Invalid acct'); if (split.length > 2) throw new Error('Invalid acct');