2023-10-01 15:20:58 +02:00
|
|
|
import Router from "@koa/router";
|
2023-10-01 17:52:59 +02:00
|
|
|
import { convertAccount, convertList, } from "../converters.js";
|
2023-10-01 15:20:58 +02:00
|
|
|
import { convertId, IdType } from "../../index.js";
|
|
|
|
import authenticate from "@/server/api/authenticate.js";
|
|
|
|
import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/server/api/mastodon/endpoints/timeline.js";
|
|
|
|
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";
|
2023-10-02 18:22:23 +02:00
|
|
|
import { UserLists } from "@/models/index.js";
|
2023-10-02 18:39:49 +02:00
|
|
|
import { NoteHelpers } from "@/server/api/mastodon/helpers/note.js";
|
|
|
|
import { getUser } from "@/server/api/common/getters.js";
|
2023-10-01 15:20:58 +02:00
|
|
|
|
|
|
|
export function setupEndpointsList(router: Router): void {
|
2023-10-01 17:52:59 +02:00
|
|
|
router.get("/v1/lists", async (ctx, reply) => {
|
|
|
|
try {
|
|
|
|
const auth = await authenticate(ctx.headers.authorization, null);
|
|
|
|
const user = auth[0] ?? undefined;
|
2023-10-01 15:20:58 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
if (!user) {
|
|
|
|
ctx.status = 401;
|
|
|
|
return;
|
|
|
|
}
|
2023-10-01 15:20:58 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
ctx.body = await ListHelpers.getLists(user)
|
|
|
|
.then(p => p.map(list => convertList(list)));
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
router.get<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
try {
|
|
|
|
const auth = await authenticate(ctx.headers.authorization, null);
|
|
|
|
const user = auth[0] ?? undefined;
|
2023-10-01 15:20:58 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
if (!user) {
|
|
|
|
ctx.status = 401;
|
|
|
|
return;
|
|
|
|
}
|
2023-10-01 15:20:58 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
const id = convertId(ctx.params.id, IdType.IceshrimpId);
|
2023-10-01 15:20:58 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
ctx.body = await ListHelpers.getList(user, id)
|
|
|
|
.then(p => convertList(p));
|
|
|
|
} catch (e: any) {
|
|
|
|
ctx.status = 404;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.post("/v1/lists", async (ctx, reply) => {
|
|
|
|
try {
|
2023-10-02 18:54:51 +02:00
|
|
|
const auth = await authenticate(ctx.headers.authorization, null);
|
|
|
|
const user = auth[0] ?? undefined;
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
ctx.status = 401;
|
|
|
|
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));
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
2023-10-02 18:54:51 +02:00
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = { error: e.message };
|
2023-10-01 17:52:59 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
router.put<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
try {
|
2023-10-02 19:02:47 +02:00
|
|
|
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));
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.delete<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
try {
|
2023-10-02 18:22:23 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
await ListHelpers.deleteList(user, list);
|
|
|
|
ctx.body = {};
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
2023-10-02 18:22:23 +02:00
|
|
|
ctx.status = 500;
|
|
|
|
ctx.body = { error: e.message };
|
2023-10-01 17:52:59 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.get<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id/accounts",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
try {
|
|
|
|
const auth = await authenticate(ctx.headers.authorization, null);
|
|
|
|
const user = auth[0] ?? undefined;
|
2023-10-01 15:20:58 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
if (!user) {
|
|
|
|
ctx.status = 401;
|
|
|
|
return;
|
|
|
|
}
|
2023-10-01 15:20:58 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
const id = convertId(ctx.params.id, IdType.IceshrimpId);
|
|
|
|
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query)));
|
|
|
|
const res = await ListHelpers.getListUsers(user, id, args.max_id, args.since_id, args.min_id, args.limit);
|
|
|
|
const accounts = await UserConverter.encodeMany(res.data);
|
|
|
|
ctx.body = accounts.map(account => convertAccount(account));
|
2023-10-02 22:24:04 +02:00
|
|
|
PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 40);
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
|
|
|
ctx.status = 404;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.post<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id/accounts",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
try {
|
2023-10-02 18:39:49 +02:00
|
|
|
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;
|
|
|
|
if (!body['account_ids']) {
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = { error: "Missing account_ids[] field" };
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ids = NoteHelpers.normalizeToArray(body['account_ids']).map(p => convertId(p, IdType.IceshrimpId));
|
|
|
|
const targets = await Promise.all(ids.map(p => getUser(p)));
|
|
|
|
await ListHelpers.addToList(user, list, targets);
|
|
|
|
ctx.body = {}
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
2023-10-02 18:39:49 +02:00
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = { error: e.message };
|
2023-10-01 17:52:59 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.delete<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id/accounts",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
try {
|
2023-10-02 19:06:02 +02:00
|
|
|
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;
|
|
|
|
if (!body['account_ids']) {
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = { error: "Missing account_ids[] field" };
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ids = NoteHelpers.normalizeToArray(body['account_ids']).map(p => convertId(p, IdType.IceshrimpId));
|
|
|
|
const targets = await Promise.all(ids.map(p => getUser(p)));
|
|
|
|
await ListHelpers.removeFromList(user, list, targets);
|
|
|
|
ctx.body = {}
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
2023-10-02 19:06:02 +02:00
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = { error: e.message };
|
2023-10-01 17:52:59 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-10-01 15:20:58 +02:00
|
|
|
}
|