[mastodon-client] PUT /lists/:id

This commit is contained in:
Laura Hausmann 2023-10-02 19:02:47 +02:00
parent 53c0d52fcd
commit 94d75585b4
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
2 changed files with 38 additions and 8 deletions

View File

@ -80,15 +80,33 @@ export function setupEndpointsList(router: Router): void {
router.put<{ Params: { id: string } }>( router.put<{ Params: { id: string } }>(
"/v1/lists/:id", "/v1/lists/:id",
async (ctx, reply) => { 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.updateList( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.IceshrimpId), const user = auth[0] ?? undefined;
(ctx.request.body as any).title,
); if (!user) {
ctx.body = convertList(data.data); 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;
}
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.updateList(user, list, title)
.then(p => convertList(p));
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
console.error(e.response.data); console.error(e.response.data);

View File

@ -94,4 +94,16 @@ export class ListHelpers {
title: list.name title: list.name
}; };
} }
public static async updateList(user: ILocalUser, list: UserList, title: string) {
if (user.id != list.userId) throw new Error("List is not owned by user");
const partial = {name: title};
const result = await UserLists.update(list.id, partial)
.then(async _ => await UserLists.findOneByOrFail({id: list.id}));
return {
id: result.id,
title: result.name
};
}
} }