[mastodon-client] DELETE /lists/:id
This commit is contained in:
parent
267624c7a3
commit
8a22659cba
2 changed files with 27 additions and 11 deletions
|
|
@ -7,6 +7,7 @@ import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/serve
|
||||||
import { ListHelpers } from "@/server/api/mastodon/helpers/list.js";
|
import { ListHelpers } from "@/server/api/mastodon/helpers/list.js";
|
||||||
import { UserConverter } from "@/server/api/mastodon/converters/user.js";
|
import { UserConverter } from "@/server/api/mastodon/converters/user.js";
|
||||||
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
|
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
|
||||||
|
import { UserLists } from "@/models/index.js";
|
||||||
|
|
||||||
export function setupEndpointsList(router: Router): void {
|
export function setupEndpointsList(router: Router): void {
|
||||||
router.get("/v1/lists", async (ctx, reply) => {
|
router.get("/v1/lists", async (ctx, reply) => {
|
||||||
|
|
@ -86,19 +87,28 @@ export function setupEndpointsList(router: Router): void {
|
||||||
router.delete<{ Params: { id: string } }>(
|
router.delete<{ 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.deleteList(
|
const auth = await authenticate(ctx.headers.authorization, null);
|
||||||
convertId(ctx.params.id, IdType.IceshrimpId),
|
const user = auth[0] ?? undefined;
|
||||||
);
|
|
||||||
ctx.body = data.data;
|
if (!user) {
|
||||||
} catch (e: any) {
|
|
||||||
console.error(e);
|
|
||||||
console.error(e.response.data);
|
|
||||||
ctx.status = 401;
|
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 };
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { ILocalUser, User } from "@/models/entities/user.js";
|
||||||
import { UserListJoinings, UserLists } from "@/models/index.js";
|
import { UserListJoinings, UserLists } from "@/models/index.js";
|
||||||
import { LinkPaginationObject } from "@/server/api/mastodon/helpers/user.js";
|
import { LinkPaginationObject } from "@/server/api/mastodon/helpers/user.js";
|
||||||
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
|
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
|
||||||
|
import { UserList } from "@/models/entities/user-list.js";
|
||||||
|
|
||||||
export class ListHelpers {
|
export class ListHelpers {
|
||||||
public static async getLists(user: ILocalUser): Promise<MastodonEntity.List[]> {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue