mirror of
https://iceshrimp.dev/crimekillz/trashposs
synced 2024-11-25 02:09:05 +01:00
[mastodon-client] PUT /lists/:id
This commit is contained in:
parent
53c0d52fcd
commit
94d75585b4
@ -80,15 +80,33 @@ export function setupEndpointsList(router: Router): void {
|
||||
router.put<{ Params: { id: string } }>(
|
||||
"/v1/lists/:id",
|
||||
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.updateList(
|
||||
convertId(ctx.params.id, IdType.IceshrimpId),
|
||||
(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 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) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
|
@ -94,4 +94,16 @@ export class ListHelpers {
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user