From 8a22659cbaaae14ae6c02c11f66550b254d34ce2 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Mon, 2 Oct 2023 18:22:23 +0200 Subject: [PATCH] [mastodon-client] DELETE /lists/:id --- .../src/server/api/mastodon/endpoints/list.ts | 32 ++++++++++++------- .../src/server/api/mastodon/helpers/list.ts | 6 ++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/list.ts b/packages/backend/src/server/api/mastodon/endpoints/list.ts index 82cb4d929..ad4641db5 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/list.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/list.ts @@ -7,6 +7,7 @@ import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/serve import { ListHelpers } from "@/server/api/mastodon/helpers/list.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; +import { UserLists } from "@/models/index.js"; export function setupEndpointsList(router: Router): void { router.get("/v1/lists", async (ctx, reply) => { @@ -86,19 +87,28 @@ export function setupEndpointsList(router: Router): void { router.delete<{ 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.deleteList( - convertId(ctx.params.id, IdType.IceshrimpId), - ); - ctx.body = 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; + } + + await ListHelpers.deleteList(user, list); + ctx.body = {}; } catch (e: any) { - console.error(e); - console.error(e.response.data); - ctx.status = 401; - ctx.body = e.response.data; + ctx.status = 500; + ctx.body = { error: e.message }; } }, ); diff --git a/packages/backend/src/server/api/mastodon/helpers/list.ts b/packages/backend/src/server/api/mastodon/helpers/list.ts index ff3d52767..4513a1c1f 100644 --- a/packages/backend/src/server/api/mastodon/helpers/list.ts +++ b/packages/backend/src/server/api/mastodon/helpers/list.ts @@ -2,6 +2,7 @@ import { ILocalUser, User } from "@/models/entities/user.js"; import { UserListJoinings, UserLists } from "@/models/index.js"; import { LinkPaginationObject } from "@/server/api/mastodon/helpers/user.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; +import { UserList } from "@/models/entities/user-list.js"; export class ListHelpers { public static async getLists(user: ILocalUser): Promise { @@ -47,4 +48,9 @@ export class ListHelpers { }; }); } + + public static async deleteList(user: ILocalUser, list: UserList) { + if (user.id != list.userId) throw new Error("List is not owned by user"); + await UserLists.delete(list.id); + } }