diff --git a/packages/backend/src/server/api/mastodon/endpoints/list.ts b/packages/backend/src/server/api/mastodon/endpoints/list.ts index e56e67846..c247e7416 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/list.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/list.ts @@ -53,17 +53,28 @@ export function setupEndpointsList(router: Router): void { }, ); router.post("/v1/lists", 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.createList((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 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.createList(user, title) + .then(p => convertList(p)); } catch (e: any) { - console.error(e); - console.error(e.response.data); - ctx.status = 401; - ctx.body = e.response.data; + ctx.status = 400; + ctx.body = { error: e.message }; } }); router.put<{ Params: { id: string } }>( diff --git a/packages/backend/src/server/api/mastodon/helpers/list.ts b/packages/backend/src/server/api/mastodon/helpers/list.ts index 85cdb9339..8c53e28dc 100644 --- a/packages/backend/src/server/api/mastodon/helpers/list.ts +++ b/packages/backend/src/server/api/mastodon/helpers/list.ts @@ -4,6 +4,7 @@ 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"; import { pushUserToUserList } from "@/services/user-list/push.js"; +import { genId } from "@/misc/gen-id.js"; export class ListHelpers { public static async getLists(user: ILocalUser): Promise { @@ -79,4 +80,18 @@ export class ListHelpers { await pushUserToUserList(user, list); } } + + public static async createList(user: ILocalUser, title: string): Promise { + const list = await UserLists.insert({ + id: genId(), + createdAt: new Date(), + userId: user.id, + name: title, + }).then(async res => await UserLists.findOneByOrFail(res.identifiers[0])); + + return { + id: list.id, + title: list.name + }; + } }