[mastodon-client] POST /lists

This commit is contained in:
Laura Hausmann 2023-10-02 18:54:51 +02:00
parent eecd911bf6
commit 53c0d52fcd
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 35 additions and 9 deletions

View file

@ -53,17 +53,28 @@ export function setupEndpointsList(router: Router): void {
}, },
); );
router.post("/v1/lists", async (ctx, reply) => { 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 { try {
const data = await client.createList((ctx.request.body as any).title); const auth = await authenticate(ctx.headers.authorization, null);
ctx.body = convertList(data.data); const user = auth[0] ?? undefined;
} catch (e: any) {
console.error(e); if (!user) {
console.error(e.response.data);
ctx.status = 401; ctx.status = 401;
ctx.body = e.response.data; 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) {
ctx.status = 400;
ctx.body = { error: e.message };
} }
}); });
router.put<{ Params: { id: string } }>( router.put<{ Params: { id: string } }>(

View file

@ -4,6 +4,7 @@ import { LinkPaginationObject } from "@/server/api/mastodon/helpers/user.js";
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
import { UserList } from "@/models/entities/user-list.js"; import { UserList } from "@/models/entities/user-list.js";
import { pushUserToUserList } from "@/services/user-list/push.js"; import { pushUserToUserList } from "@/services/user-list/push.js";
import { genId } from "@/misc/gen-id.js";
export class ListHelpers { export class ListHelpers {
public static async getLists(user: ILocalUser): Promise<MastodonEntity.List[]> { public static async getLists(user: ILocalUser): Promise<MastodonEntity.List[]> {
@ -79,4 +80,18 @@ export class ListHelpers {
await pushUserToUserList(user, list); await pushUserToUserList(user, list);
} }
} }
public static async createList(user: ILocalUser, title: string): Promise<MastodonEntity.List> {
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
};
}
} }