From 3dffaf5594691a04aaf81ec9347ee6aed0de7c9c Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 29 Sep 2023 13:40:12 +0200 Subject: [PATCH] [mastodon-client] POST /statuses/:id/react/:name, /statuses/:id/unreact/:name --- .../server/api/mastodon/endpoints/status.ts | 54 +++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index f9f9ee6bd..df146b596 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -466,15 +466,26 @@ export function apiStatusMastodon(router: Router): void { router.post<{ Params: { id: string; name: string } }>( "/v1/statuses/:id/react/:name", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.reactStatus( - convertId(ctx.params.id, IdType.IceshrimpId), - ctx.params.name, - ); - 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.reactToNote(note, user, ctx.params.name) + .then(p => NoteConverter.encode(p, user)) + .then(p => convertStatus(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -486,15 +497,26 @@ export function apiStatusMastodon(router: Router): void { router.post<{ Params: { id: string; name: string } }>( "/v1/statuses/:id/unreact/:name", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.unreactStatus( - convertId(ctx.params.id, IdType.IceshrimpId), - ctx.params.name, - ); - 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;