From 94d75585b475238ab739791b0cf67e6c61610443 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Mon, 2 Oct 2023 19:02:47 +0200 Subject: [PATCH] [mastodon-client] PUT /lists/:id --- .../src/server/api/mastodon/endpoints/list.ts | 34 ++++++++++++++----- .../src/server/api/mastodon/helpers/list.ts | 12 +++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/list.ts b/packages/backend/src/server/api/mastodon/endpoints/list.ts index c247e7416..9455cea7b 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/list.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/list.ts @@ -80,15 +80,33 @@ export function setupEndpointsList(router: Router): void { router.put<{ Params: { id: string } }>( "/v1/lists/:id", async (ctx, reply) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.updateList( - convertId(ctx.params.id, IdType.IceshrimpId), - (ctx.request.body as any).title, - ); - ctx.body = convertList(data.data); + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? undefined; + + if (!user) { + ctx.status = 401; + return; + } + + const id = convertId(ctx.params.id, IdType.IceshrimpId); + const list = await UserLists.findOneBy({userId: user.id, id: id}); + + if (!list) { + ctx.status = 404; + return; + } + + const body = ctx.request.body as any; + const title = (body.title ?? '').trim(); + if (title.length < 1) { + ctx.body = { error: "Title must not be empty" }; + ctx.status = 400; + return + } + + ctx.body = await ListHelpers.updateList(user, list, title) + .then(p => convertList(p)); } catch (e: any) { console.error(e); console.error(e.response.data); diff --git a/packages/backend/src/server/api/mastodon/helpers/list.ts b/packages/backend/src/server/api/mastodon/helpers/list.ts index 8c53e28dc..3cf34bb7c 100644 --- a/packages/backend/src/server/api/mastodon/helpers/list.ts +++ b/packages/backend/src/server/api/mastodon/helpers/list.ts @@ -94,4 +94,16 @@ export class ListHelpers { title: list.name }; } + + public static async updateList(user: ILocalUser, list: UserList, title: string) { + if (user.id != list.userId) throw new Error("List is not owned by user"); + const partial = {name: title}; + const result = await UserLists.update(list.id, partial) + .then(async _ => await UserLists.findOneByOrFail({id: list.id})); + + return { + id: result.id, + title: result.name + }; + } }