[mastodon-client] DELETE /lists/:id

This commit is contained in:
Laura Hausmann 2023-10-02 18:22:23 +02:00
parent 267624c7a3
commit 8a22659cba
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
2 changed files with 27 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/serve
import { ListHelpers } from "@/server/api/mastodon/helpers/list.js";
import { UserConverter } from "@/server/api/mastodon/converters/user.js";
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
import { UserLists } from "@/models/index.js";
export function setupEndpointsList(router: Router): void {
router.get("/v1/lists", async (ctx, reply) => {
@ -86,19 +87,28 @@ export function setupEndpointsList(router: Router): void {
router.delete<{ 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.deleteList(
convertId(ctx.params.id, IdType.IceshrimpId),
);
ctx.body = 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 id = convertId(ctx.params.id, IdType.IceshrimpId);
const list = await UserLists.findOneBy({userId: user.id, id: id});
if (!list) {
ctx.status = 404;
return;
}
await ListHelpers.deleteList(user, list);
ctx.body = {};
} catch (e: any) {
ctx.status = 500;
ctx.body = { error: e.message };
}
},
);

View File

@ -2,6 +2,7 @@ import { ILocalUser, User } from "@/models/entities/user.js";
import { UserListJoinings, UserLists } from "@/models/index.js";
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";
export class ListHelpers {
public static async getLists(user: ILocalUser): Promise<MastodonEntity.List[]> {
@ -47,4 +48,9 @@ export class ListHelpers {
};
});
}
public static async deleteList(user: ILocalUser, list: UserList) {
if (user.id != list.userId) throw new Error("List is not owned by user");
await UserLists.delete(list.id);
}
}