[mastodon-client] POST /statuses/:id/bookmark, /statuses/:id/unbookmark

This commit is contained in:
Laura Hausmann 2023-09-29 14:24:06 +02:00
parent 5b7e0b8ede
commit c1df4bd579
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 69 additions and 15 deletions

View file

@ -414,14 +414,26 @@ export function apiStatusMastodon(router: Router): void {
router.post<{ Params: { id: string } }>( router.post<{ Params: { id: string } }>(
"/v1/statuses/:id/bookmark", "/v1/statuses/:id/bookmark",
async (ctx) => { async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.bookmarkStatus( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.IceshrimpId), const user = auth[0] ?? null;
);
ctx.body = convertStatus(data.data); if (!user) {
ctx.status = 401;
return;
}
const id = convertId(ctx.params.id, IdType.IceshrimpId);
const note = await getNote(id, user).catch(_ => null);
if (note === null) {
ctx.status = 404;
return;
}
ctx.body = await NoteHelpers.bookmarkNote(note, user)
.then(p => NoteConverter.encode(p, user))
.then(p => convertStatus(p));
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
ctx.status = 401; ctx.status = 401;
@ -433,14 +445,26 @@ export function apiStatusMastodon(router: Router): void {
router.post<{ Params: { id: string } }>( router.post<{ Params: { id: string } }>(
"/v1/statuses/:id/unbookmark", "/v1/statuses/:id/unbookmark",
async (ctx) => { async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.unbookmarkStatus( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.IceshrimpId), const user = auth[0] ?? null;
);
ctx.body = convertStatus(data.data); if (!user) {
ctx.status = 401;
return;
}
const id = convertId(ctx.params.id, IdType.IceshrimpId);
const note = await getNote(id, user).catch(_ => null);
if (note === null) {
ctx.status = 404;
return;
}
ctx.body = await NoteHelpers.unbookmarkNote(note, user)
.then(p => NoteConverter.encode(p, user))
.then(p => convertStatus(p));
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
ctx.status = 401; ctx.status = 401;

View file

@ -1,5 +1,5 @@
import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js"; import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js";
import { Metas, Notes, Users } from "@/models/index.js"; import { Metas, NoteFavorites, Notes, Users } from "@/models/index.js";
import { generateVisibilityQuery } from "@/server/api/common/generate-visibility-query.js"; import { generateVisibilityQuery } from "@/server/api/common/generate-visibility-query.js";
import { generateMutedUserQuery } from "@/server/api/common/generate-muted-user-query.js"; import { generateMutedUserQuery } from "@/server/api/common/generate-muted-user-query.js";
import { generateBlockedUserQuery } from "@/server/api/common/generate-block-query.js"; import { generateBlockedUserQuery } from "@/server/api/common/generate-block-query.js";
@ -10,6 +10,7 @@ import createReaction from "@/services/note/reaction/create.js";
import deleteReaction from "@/services/note/reaction/delete.js"; import deleteReaction from "@/services/note/reaction/delete.js";
import createNote from "@/services/note/create.js"; import createNote from "@/services/note/create.js";
import deleteNote from "@/services/note/delete.js"; import deleteNote from "@/services/note/delete.js";
import { genId } from "@/misc/gen-id.js";
export class NoteHelpers { export class NoteHelpers {
public static async getDefaultReaction(): Promise<string> { public static async getDefaultReaction(): Promise<string> {
@ -48,6 +49,35 @@ export class NoteHelpers {
.then(_ => getNote(note.id, user)); .then(_ => getNote(note.id, user));
} }
public static async bookmarkNote(note: Note, user: ILocalUser): Promise<Note> {
const bookmarked = await NoteFavorites.exist({
where: {
noteId: note.id,
userId: user.id,
},
});
if (!bookmarked) {
await NoteFavorites.insert({
id: genId(),
createdAt: new Date(),
noteId: note.id,
userId: user.id,
});
}
return note;
}
public static async unbookmarkNote(note: Note, user: ILocalUser): Promise<Note> {
return await NoteFavorites.findOneBy({
noteId: note.id,
userId: user.id,
})
.then(p => p !== null ? NoteFavorites.delete(p.id) : null)
.then(_ => note);
}
public static async getNoteDescendants(note: Note | string, user: ILocalUser | null, 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"))