diff --git a/packages/backend/src/server/api/mastodon/converters/visibility.ts b/packages/backend/src/server/api/mastodon/converters/visibility.ts index 37d91830b..6d9c44334 100644 --- a/packages/backend/src/server/api/mastodon/converters/visibility.ts +++ b/packages/backend/src/server/api/mastodon/converters/visibility.ts @@ -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 { diff --git a/packages/backend/src/server/api/mastodon/endpoints/misc.ts b/packages/backend/src/server/api/mastodon/endpoints/misc.ts index cedefa9fb..5d6eee479 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/misc.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/misc.ts @@ -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 }; } }); diff --git a/packages/backend/src/server/api/mastodon/helpers/misc.ts b/packages/backend/src/server/api/mastodon/helpers/misc.ts index eb3a1c521..7fc85c982 100644 --- a/packages/backend/src/server/api/mastodon/helpers/misc.ts +++ b/packages/backend/src/server/api/mastodon/helpers/misc.ts @@ -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 { @@ -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 { + 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); + } } \ No newline at end of file diff --git a/packages/backend/src/server/api/mastodon/helpers/user.ts b/packages/backend/src/server/api/mastodon/helpers/user.ts index 59fef45b1..5ff83ba20 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -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 { + public static async getDefaultNoteVisibility(user: ILocalUser): Promise { return RegistryItems.findOneBy({domain: IsNull(), userId: user.id, key: 'defaultNoteVisibility', scope: '{client,base}'}).then(p => p?.value ?? 'public') } }