[mastodon-client] Fix handling of note visibility errors and anonymous calls

This commit is contained in:
Laura Hausmann 2023-09-15 14:55:17 +02:00
parent f384186b88
commit 15e698df46
Signed by: zotan
GPG key ID: D044E84C5BE01605
3 changed files with 12 additions and 12 deletions

View file

@ -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<MastodonEntity.Status> {
public static async encode(note: Note, user: ILocalUser | null): Promise<MastodonEntity.Status> {
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<MastodonEntity.Status[]> {
public static async encodeMany(notes: Note[], user: ILocalUser | null): Promise<MastodonEntity.Status[]> {
const encoded = notes.map(n => this.encode(n, user));
return Promise.all(encoded);
}

View file

@ -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 = {

View file

@ -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<Note[]> {
public static async getNoteDescendants(note: Note | string, user: ILocalUser | null, limit: number = 10, depth: number = 2): Promise<Note[]> {
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<Note[]> {
public static async getNoteAncestors(rootNote: Note, user: ILocalUser | null, limit: number = 10): Promise<Note[]> {
const notes = new Array<Note>;
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;
}