mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-25 11:27:31 -07:00
[mastodon-client] POST /lists
This commit is contained in:
parent
eecd911bf6
commit
53c0d52fcd
2 changed files with 35 additions and 9 deletions
|
@ -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);
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
const auth = await authenticate(ctx.headers.authorization, null);
|
||||
const user = auth[0] ?? undefined;
|
||||
|
||||
if (!user) {
|
||||
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 } }>(
|
||||
|
|
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue