diff --git a/packages/backend/src/server/api/mastodon/converters/note.ts b/packages/backend/src/server/api/mastodon/converters/note.ts index 60be5f749..70734f76f 100644 --- a/packages/backend/src/server/api/mastodon/converters/note.ts +++ b/packages/backend/src/server/api/mastodon/converters/note.ts @@ -17,11 +17,11 @@ import { populatePoll } from "@/models/repositories/note.js"; import { FileConverter } from "@/server/api/mastodon/converters/file.js"; export class NoteConverter { - public static async encode(note: Note, user?: ILocalUser): Promise { + public static async encode(note: Note, user: ILocalUser | null): Promise { const noteUser = note.user ?? await getUser(note.userId); if (!await Notes.isVisibleForMe(note, user?.id ?? null)) - throw new Error(); + throw new Error('Cannot encode note not visible for user'); const host = note.user?.host ?? null; @@ -49,7 +49,7 @@ export class NoteConverter { } }) : null; - const reply = note.reply ?? (note.replyId ? await getNote(note.replyId, user ?? null) : null); + const reply = note.reply ?? (note.replyId ? await getNote(note.replyId, user) : null); const isBookmarked = user ? await NoteFavorites.exist({ where: { @@ -109,7 +109,7 @@ export class NoteConverter { }; } - public static async encodeMany(notes: Note[], user?: ILocalUser): Promise { + public static async encodeMany(notes: Note[], user: ILocalUser | null): Promise { const encoded = notes.map(n => this.encode(n, user)); return Promise.all(encoded); } diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index 82cb82209..750ad1a7e 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -148,7 +148,7 @@ export function apiStatusMastodon(router: Router): void { router.get<{ Params: { id: string } }>("/v1/statuses/:id", async (ctx) => { try { const auth = await authenticate(ctx.headers.authorization, null); - const user = auth[0] ?? undefined; + const user = auth[0] ?? null; const noteId = convertId(ctx.params.id, IdType.IceshrimpId); const note = await getNote(noteId, user ?? null).then(n => n).catch(() => null); @@ -195,7 +195,7 @@ export function apiStatusMastodon(router: Router): void { const accessTokens = ctx.headers.authorization; try { const auth = await authenticate(ctx.headers.authorization, null); - const user = auth[0] ?? undefined; + const user = auth[0] ?? null; const id = convertId(ctx.params.id, IdType.IceshrimpId); const note = await getNote(id, user ?? null).then(n => n).catch(() => null); @@ -207,10 +207,10 @@ export function apiStatusMastodon(router: Router): void { } const ancestors = await NoteHelpers.getNoteAncestors(note, user, user ? 4096 : 60) - .then(n => NoteConverter.encodeMany(n)) + .then(n => NoteConverter.encodeMany(n, user)) .then(n => n.map(s => convertStatus(s))); const descendants = await NoteHelpers.getNoteDescendants(note, user, user ? 4096 : 40, user ? 4096 : 20) - .then(n => NoteConverter.encodeMany(n)) + .then(n => NoteConverter.encodeMany(n, user)) .then(n => n.map(s => convertStatus(s))); ctx.body = { diff --git a/packages/backend/src/server/api/mastodon/helpers/note.ts b/packages/backend/src/server/api/mastodon/helpers/note.ts index 0088857cd..0ee6d9c75 100644 --- a/packages/backend/src/server/api/mastodon/helpers/note.ts +++ b/packages/backend/src/server/api/mastodon/helpers/note.ts @@ -9,7 +9,7 @@ import querystring from "node:querystring"; import { getNote } from "@/server/api/common/getters.js"; export class NoteHelpers { - public static async getNoteDescendants(note: Note | string, user?: ILocalUser, limit: number = 10, depth: number = 2): Promise { + public static async getNoteDescendants(note: Note | string, user: ILocalUser | null, limit: number = 10, depth: number = 2): Promise { const noteId = typeof note === "string" ? note : note.id; const query = makePaginationQuery(Notes.createQueryBuilder("note")) .andWhere( @@ -29,16 +29,16 @@ export class NoteHelpers { return query.getMany(); } - public static async getNoteAncestors(rootNote: Note, user?: ILocalUser, limit: number = 10): Promise { + public static async getNoteAncestors(rootNote: Note, user: ILocalUser | null, limit: number = 10): Promise { const notes = new Array; for (let i = 0; i < limit; i++) { const currentNote = notes.at(-1) ?? rootNote; if (!currentNote.replyId) break; - const nextNote = await getNote(currentNote.replyId, user ?? null).catch((e) => { + const nextNote = await getNote(currentNote.replyId, user).catch((e) => { if (e.id === "9725d0ce-ba28-4dde-95a7-2cbb2c15de24") return null; throw e; }); - if (nextNote) notes.push(nextNote); + if (nextNote && await Notes.isVisibleForMe(nextNote, user?.id ?? null)) notes.push(nextNote); else break; }