From 93a4db4418d5d3dca116390399b443c3d5ba737c Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 29 Sep 2023 13:34:08 +0200 Subject: [PATCH] [mastodon-client] POST /statuses/:id/favourite, /statuses/:id/favourite --- .../server/api/mastodon/endpoints/status.ts | 68 +++++++++++++------ .../src/server/api/mastodon/helpers/note.ts | 14 +++- 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index 5983d3949..f9f9ee6bd 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -11,6 +11,8 @@ import { getNote } from "@/server/api/common/getters.js"; import authenticate from "@/server/api/authenticate.js"; import { NoteHelpers } from "@/server/api/mastodon/helpers/note.js"; import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; +import createReaction from "@/services/note/reaction/create.js"; +import deleteReaction from "@/services/note/reaction/delete.js"; function normalizeQuery(data: any) { const str = querystring.stringify(data); @@ -281,21 +283,37 @@ export function apiStatusMastodon(router: Router): void { router.post<{ Params: { id: string } }>( "/v1/statuses/:id/favourite", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); - const react = await NoteHelpers.getDefaultReaction(); try { - const a = (await client.createEmojiReaction( - convertId(ctx.params.id, IdType.IceshrimpId), - react, - )) as any; - //const data = await client.favouriteStatus(ctx.params.id) as any; - ctx.body = convertStatus(a.data); + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + 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; + } + + const reaction = await NoteHelpers.getDefaultReaction().catch(_ => null); + + if (reaction === null) { + ctx.status = 500; + return; + } + + ctx.body = await NoteHelpers.reactToNote(note, user, reaction) + .then(p => NoteConverter.encode(p, user)) + .then(p => convertStatus(p)); } catch (e: any) { console.error(e); console.error(e.response.data); - ctx.status = 401; + ctx.status = 400; ctx.body = e.response.data; } }, @@ -303,16 +321,26 @@ export function apiStatusMastodon(router: Router): void { router.post<{ Params: { id: string } }>( "/v1/statuses/:id/unfavourite", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); - const react = await NoteHelpers.getDefaultReaction(); try { - const data = await client.deleteEmojiReaction( - convertId(ctx.params.id, IdType.IceshrimpId), - react, - ); - ctx.body = convertStatus(data.data); + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + 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.removeReactFromNote(note, user) + .then(p => NoteConverter.encode(p, user)) + .then(p => convertStatus(p)); } catch (e: any) { console.error(e); ctx.status = 401; diff --git a/packages/backend/src/server/api/mastodon/helpers/note.ts b/packages/backend/src/server/api/mastodon/helpers/note.ts index e9a0e5c60..4d8e70b1c 100644 --- a/packages/backend/src/server/api/mastodon/helpers/note.ts +++ b/packages/backend/src/server/api/mastodon/helpers/note.ts @@ -5,9 +5,9 @@ import { generateMutedUserQuery } from "@/server/api/common/generate-muted-user- import { generateBlockedUserQuery } from "@/server/api/common/generate-block-query.js"; import { Note } from "@/models/entities/note.js"; import { ILocalUser } from "@/models/entities/user.js"; -import querystring from "node:querystring"; import { getNote } from "@/server/api/common/getters.js"; -import { ObjectLiteral, SelectQueryBuilder } from "typeorm"; +import createReaction from "@/services/note/reaction/create.js"; +import deleteReaction from "@/services/note/reaction/delete.js"; export class NoteHelpers { public static async getDefaultReaction(): Promise { @@ -17,6 +17,16 @@ export class NoteHelpers { .then(p => p[0].defaultReaction); } + public static async reactToNote(note: Note, user: ILocalUser, reaction: string): Promise { + await createReaction(user, note, reaction); + return getNote(note.id, user); + } + + public static async removeReactFromNote(note: Note, user: ILocalUser): Promise { + await deleteReaction(user, note); + return getNote(note.id, user); + } + 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"))