[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 {
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);

View File

@ -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<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> {
const split = acct.toLowerCase().split('@');
if (split.length > 2) throw new Error('Invalid acct');