[mastodon-client] Fix handling of note visibility errors and anonymous calls
This commit is contained in:
parent
f384186b88
commit
15e698df46
3 changed files with 12 additions and 12 deletions
|
|
@ -17,11 +17,11 @@ import { populatePoll } from "@/models/repositories/note.js";
|
||||||
import { FileConverter } from "@/server/api/mastodon/converters/file.js";
|
import { FileConverter } from "@/server/api/mastodon/converters/file.js";
|
||||||
|
|
||||||
export class NoteConverter {
|
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);
|
const noteUser = note.user ?? await getUser(note.userId);
|
||||||
|
|
||||||
if (!await Notes.isVisibleForMe(note, user?.id ?? null))
|
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;
|
const host = note.user?.host ?? null;
|
||||||
|
|
||||||
|
|
@ -49,7 +49,7 @@ export class NoteConverter {
|
||||||
}
|
}
|
||||||
}) : null;
|
}) : 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({
|
const isBookmarked = user ? await NoteFavorites.exist({
|
||||||
where: {
|
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));
|
const encoded = notes.map(n => this.encode(n, user));
|
||||||
return Promise.all(encoded);
|
return Promise.all(encoded);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -148,7 +148,7 @@ export function apiStatusMastodon(router: Router): void {
|
||||||
router.get<{ Params: { id: string } }>("/v1/statuses/:id", async (ctx) => {
|
router.get<{ Params: { id: string } }>("/v1/statuses/:id", async (ctx) => {
|
||||||
try {
|
try {
|
||||||
const auth = await authenticate(ctx.headers.authorization, null);
|
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 noteId = convertId(ctx.params.id, IdType.IceshrimpId);
|
||||||
const note = await getNote(noteId, user ?? null).then(n => n).catch(() => null);
|
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;
|
const accessTokens = ctx.headers.authorization;
|
||||||
try {
|
try {
|
||||||
const auth = await authenticate(ctx.headers.authorization, null);
|
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 id = convertId(ctx.params.id, IdType.IceshrimpId);
|
||||||
const note = await getNote(id, user ?? null).then(n => n).catch(() => null);
|
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)
|
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)));
|
.then(n => n.map(s => convertStatus(s)));
|
||||||
const descendants = await NoteHelpers.getNoteDescendants(note, user, user ? 4096 : 40, user ? 4096 : 20)
|
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)));
|
.then(n => n.map(s => convertStatus(s)));
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import querystring from "node:querystring";
|
||||||
import { getNote } from "@/server/api/common/getters.js";
|
import { getNote } from "@/server/api/common/getters.js";
|
||||||
|
|
||||||
export class NoteHelpers {
|
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 noteId = typeof note === "string" ? note : note.id;
|
||||||
const query = makePaginationQuery(Notes.createQueryBuilder("note"))
|
const query = makePaginationQuery(Notes.createQueryBuilder("note"))
|
||||||
.andWhere(
|
.andWhere(
|
||||||
|
|
@ -29,16 +29,16 @@ export class NoteHelpers {
|
||||||
return query.getMany();
|
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>;
|
const notes = new Array<Note>;
|
||||||
for (let i = 0; i < limit; i++) {
|
for (let i = 0; i < limit; i++) {
|
||||||
const currentNote = notes.at(-1) ?? rootNote;
|
const currentNote = notes.at(-1) ?? rootNote;
|
||||||
if (!currentNote.replyId) break;
|
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;
|
if (e.id === "9725d0ce-ba28-4dde-95a7-2cbb2c15de24") return null;
|
||||||
throw e;
|
throw e;
|
||||||
});
|
});
|
||||||
if (nextNote) notes.push(nextNote);
|
if (nextNote && await Notes.isVisibleForMe(nextNote, user?.id ?? null)) notes.push(nextNote);
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue