[mastodon-client] GET /v1/preferenes

This commit is contained in:
Laura Hausmann 2023-10-05 14:16:28 +02:00
parent ec55071eb2
commit 4e177419eb
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
4 changed files with 35 additions and 14 deletions

View File

@ -1,5 +1,5 @@
type IceshrimpVisibility = "public" | "home" | "followers" | "specified" | "hidden";
type MastodonVisibility = "public" | "unlisted" | "private" | "direct";
export type IceshrimpVisibility = "public" | "home" | "followers" | "specified" | "hidden";
export type MastodonVisibility = "public" | "unlisted" | "private" | "direct";
export class VisibilityConverter {
public static encode(v: IceshrimpVisibility): MastodonVisibility {

View File

@ -100,17 +100,19 @@ export function setupEndpointsMisc(router: Router): void {
});
router.get("/v1/preferences", async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getPreferences();
ctx.body = data.data;
const auth = await authenticate(ctx.headers.authorization, null);
const user = auth[0] ?? null;
if (!user) {
ctx.status = 401;
return;
}
ctx.body = await MiscHelpers.getPreferences(user);
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
ctx.status = 500;
ctx.body = { error: e.message };
}
});

View File

@ -1,7 +1,7 @@
import config from "@/config/index.js";
import { FILE_TYPE_BROWSERSAFE, MAX_NOTE_TEXT_LENGTH } from "@/const.js";
import { fetchMeta } from "@/misc/fetch-meta.js";
import { AnnouncementReads, Announcements, Emojis, Instances, Notes, Users } from "@/models/index.js";
import { AnnouncementReads, Announcements, Emojis, Instances, Notes, UserProfiles, Users } from "@/models/index.js";
import { IsNull } from "typeorm";
import { awaitAll } from "@/prelude/await-all.js";
import { UserConverter } from "@/server/api/mastodon/converters/user.js";
@ -19,6 +19,7 @@ import { uniqBy } from "@/prelude/array.js";
import { EmojiConverter } from "@/server/api/mastodon/converters/emoji.js";
import { populateEmojis } from "@/misc/populate-emojis.js";
import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
import { VisibilityConverter } from "@/server/api/mastodon/converters/visibility.js";
export class MiscHelpers {
public static async getInstance(): Promise<MastodonEntity.Instance> {
@ -227,4 +228,22 @@ export class MiscHelpers {
return [];
//FIXME: This was already implemented in api/endpoints/hashtags/trend.ts, but the implementation is sketchy at best. Rewrite from scratch.
}
public static getPreferences(user: ILocalUser): Promise<MastodonEntity.Preferences> {
const profile = UserProfiles.findOneByOrFail({userId: user.id});
const sensitive = profile.then(p => p.alwaysMarkNsfw);
const language = profile.then(p => p.lang);
const privacy = UserHelpers.getDefaultNoteVisibility(user)
.then(p => VisibilityConverter.encode(p));
const res = {
"posting:default:visibility": privacy,
"posting:default:sensitive": sensitive,
"posting:default:language": language,
"reading:expand:media": "default" as "default" | "show_all" | "hide_all", //FIXME: see below
"reading:expand:spoilers": false //FIXME: store this on server instead of client
}
return awaitAll(res);
}
}

View File

@ -33,7 +33,7 @@ import { UserConverter } from "@/server/api/mastodon/converters/user.js";
import acceptFollowRequest from "@/services/following/requests/accept.js";
import { rejectFollowRequest } from "@/services/following/reject.js";
import { Brackets, IsNull } from "typeorm";
import { VisibilityConverter } from "@/server/api/mastodon/converters/visibility.js";
import { IceshrimpVisibility, VisibilityConverter } from "@/server/api/mastodon/converters/visibility.js";
export type AccountCache = {
locks: AsyncLock;
@ -484,7 +484,7 @@ export class UserHelpers {
};
}
public static getDefaultNoteVisibility(user: ILocalUser): Promise<string> {
public static async getDefaultNoteVisibility(user: ILocalUser): Promise<IceshrimpVisibility> {
return RegistryItems.findOneBy({domain: IsNull(), userId: user.id, key: 'defaultNoteVisibility', scope: '{client,base}'}).then(p => p?.value ?? 'public')
}
}