[mastodon-client] POST /lists

This commit is contained in:
Laura Hausmann 2023-10-02 18:54:51 +02:00
parent eecd911bf6
commit 53c0d52fcd
Signed by: zotan
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) => {
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 } }>(

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 { 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<MastodonEntity.List[]> {
@ -79,4 +80,18 @@ export class ListHelpers {
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
};
}
}